-
Notifications
You must be signed in to change notification settings - Fork 11.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[5.2] Fix Arr::get($array, null) when $array is empty #12975
Conversation
I wasn't expecting we would go as far as empty array + null key. But we do. A better implementation would be to move up the |
Depends whether we want |
@@ -184,6 +184,9 @@ public function testGet() | |||
// Test $array not an array | |||
$this->assertEquals('default', Arr::get(null, 'foo', 'default')); | |||
$this->assertEquals('default', Arr::get(false, 'foo', 'default')); | |||
|
|||
// Test $array is empty |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be more like "Test $array is empty and $key is null"
Does this make sense at all? :p So the alternatives are:
|
Your pick, just please add a test for the choosen one :) |
Is this supposed to be an or check? Seems weird to be and? |
It means "falsy, but not an empty array". |
We should be ok with only:
Tests:
|
It should rather use
|
I tried with only |
I had edited my message, can you please check again? |
@vlakoff Yep, working properly with accessible. I added the tests you recommended, they make sense. |
$this->assertEquals('default', Arr::get(null, null, 'default')); | ||
|
||
// Test $array is empty and key is null | ||
$this->assertEquals([], Arr::get([], null)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use assertSame
here. I'm tired of loose typing enough :\
@thomasruiz You changed perms on the test file, you should revert them back to 644. |
To summarize: Previous behaviour:
Fixed behaviour:
|
Why is this so complicated? Arr::get has been the same for years and over the past month it seems like we have been screwing with it endlessly? Can we please stop and just make it work how it has worked for the past 4 years? |
Does this make it work how it used to work? |
I understand this can annoy you (you're not alone ;) Problem is, the framework is relying on edge, real tricky cases of Bad news, without surprise it easily broke, good news, it gets reinforced in the process. |
We do our best. |
Can we delay this to |
I'm pretty sure the code works as intended now. |
We are restoring previous behaviour for the case |
For codebase consistency, we might make a similar change in Apart from that, PR seems good to go. |
Why we need Arr means working on array (or array like) |
STOP ARGUING ABOUT WHAT YOU THINK IT SHOULD BE. This function has been the same since like Laravel 2.0 and has NEVER caused a problem until people started screwing around with it like a month ago. STOP! Just make it how it used to work in Laravel 1, 2, 3, 4, 5.1, 5.2 until people started messing with it. |
Oh, update my word, I mean it works GOOD now, other special cases (not designed) change is not the point as Taylor said |
Following #12958, #12946 and #12945.
Basically, in that particular use case, when the model is created,
attributes
andoriginal
are both set to empty arrays. InArr::get
, we haveif (! $array) { return $default; }
. However, in PHP, when an array is empty,(! $array) === true
.This PR just adds to the condition the
&& ! is_array($array)
to fix the problem.