Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/Illuminate/Support/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,11 +294,11 @@ public static function get($array, $key, $default = null)
}

foreach (explode('.', $key) as $segment) {
if (static::accessible($array) && static::exists($array, $segment)) {
$array = $array[$segment];
} else {
return value($default);
if (!static::accessible($array) || !static::exists($array, $segment)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is a change in this condition needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cause Previously it was both of them needs to be true. If any of them turns to false the condition check will fail. As we are inverting it, so we need to check either one is false or not. Hope you get it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see a few problems with this:

For me is more natural to read "if accessible and exists" as "if not accessible or not exists".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh... It's not a problem actually, it's may be your personal preference 😃 Anyway, to me it's more readable than the one with else. Also, early return is preferable I think and we try to follow it in other parts of Laravel code base.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well for me if the element is not accessible there are more chances that it does not exist, this will mean that the second condition will be always evaluated when not needed.

With the previous code, it was clear that after an array is not accessible we will return the default value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uha... Good point! 👍 Need to give it a shot! Thinking about it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By inverting the condition position we can solve this issue @jmarcher 🤔. See my last commit.

return value($default);
}

$array = $array[$segment];
}

return $array;
Expand Down Expand Up @@ -335,11 +335,11 @@ public static function has($array, $keys)
}

foreach (explode('.', $key) as $segment) {
if (static::accessible($subKeyArray) && static::exists($subKeyArray, $segment)) {
$subKeyArray = $subKeyArray[$segment];
} else {
return false;
if (!static::accessible($subKeyArray) || !static::exists($subKeyArray, $segment)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is a change in this condition needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the previous comment .

return false;
}

$subKeyArray = $subKeyArray[$segment];
}
}

Expand Down