Skip to content

Conversation

@ahmedsaed27
Copy link

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.

Comment on lines +1598 to +1599
$items[$temp = new class {
}] = 'bar';

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.

Copy link
Author

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.

Copy link
Contributor

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

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.

Copy link
Author

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.

@rodrigopedra
Copy link
Contributor

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 master instead, as many applications could be relying on the current behavior.

@donnysim
Copy link
Contributor

donnysim commented Sep 11, 2025

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 🫣

@ahmedsaed27
Copy link
Author

the default is actually useful in many cases:
– If the value is not found, the method will return the default array you provide.
– If the target is not accessible in Arr::get (not an array), then it will fall back to the default, which by contract is an array (often []).

This way the method always respects its strict : array return type while still giving developers a safe default.

@rodrigopedra
Copy link
Contributor

the default is actually useful in many cases

100% agree.

Yet some projects can be relying on the current behavior, for example, by handling the exception on a try..catch block to check for missing keys.

IMO it is bad to break other people's apps on patch releases.

That is why I suggested you send this PR to the master branch instead.

@ahmedsaed27
Copy link
Author

Thanks a lot for the clarification 🙏 That makes total sense — I understand the concern about not breaking existing apps on a patch release.
I’ll update this PR and target the master branch instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants