Skip to content
Closed
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/Illuminate/Collections/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static function add($array, $key, $value)
/**
* 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.

{
$value = Arr::get($array, $key, $default);

Expand Down
10 changes: 9 additions & 1 deletion tests/Support/SupportArrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,13 @@ public function testItGetsAnArray()
Arr::array($test_array, 'string');
}

public function testItReturnsEmptyArrayForMissingKeyByDefault()
{
$data = ['name' => 'Taylor'];

$this->assertSame([], Arr::array($data, 'missing_key'));
}

public function testHas()
{
$array = ['products.desk' => ['price' => 100]];
Expand Down Expand Up @@ -1588,7 +1595,8 @@ public function testFrom()
$this->assertSame($subject, Arr::from($items));

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

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.

$this->assertSame(['bar'], Arr::from($items));

$this->expectException(InvalidArgumentException::class);
Expand Down