-
Notifications
You must be signed in to change notification settings - Fork 11.7k
Fix Arr::array default to use empty array #57008
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
Conversation
| $items[$temp = new class { | ||
| }] = 'bar'; |
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.
These chages are not related for this PR.
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.
These changes came from running Laravel Pint for code style consistency, not from any functional change.
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.
Laravel uses StyleCI—adhering to StyleCI's config should suffice.
| * Get an array item from an array using "dot" notation. | ||
| */ | ||
| public static function array(ArrayAccess|array $array, string|int|null $key, ?array $default = null): array | ||
| public static function array(ArrayAccess|array $array, string|int|null $key, ?array $default = []): array |
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.
I think this change would be a breaking change in many projects.
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.
The method enforces a strict : array return type, so using [] as the default keeps the contract consistent. I’ve run the full test suite for this method, and everything passes fine.
|
It is a behavior change. Before it would fail for missing keys. With this change it no longer fails and returns an empty array for a missing key. $ php artisan tinker
Psy Shell v0.12.10 (PHP 8.3.25 — cli) by Justin Hileman
> Arr::array([], 'foo')
InvalidArgumentException Array value for key [foo] must be an array, NULL found.
> Arr::array([], 'foo', [])
= []
> I am not against the change; I think an array is a sane default. But it should be sent to |
|
I don't think a default value here makes any sense? If you get non array item it will throw, else it will always be an array so it will never use the default? Am I missing something? Nvm, spoke too soon, if you pass array as default it will return the array if not found 🫣 |
|
the default is actually useful in many cases: This way the method always respects its strict : array return type while still giving developers a safe default. |
100% agree. Yet some projects can be relying on the current behavior, for example, by handling the exception on a IMO it is bad to break other people's apps on patch releases. That is why I suggested you send this PR to the |
|
Thanks a lot for the clarification 🙏 That makes total sense — I understand the concern about not breaking existing apps on a patch release. |
This PR updates the Arr::array() method to use an empty array ([]) as the default return value when no key exists, instead of null.
Previously, the method signature allowed a null default (?array $default = null), but this created a mismatch with the strict : array return type. If a developer passed null as the default, the method would still throw an exception because null is not an array.