Skip to content
Merged
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 @@ -230,7 +230,7 @@ public static function exists($array, $key)
return $array->offsetExists($key);
}

if (is_float($key)) {
if (is_float($key) || is_null($key)) {
$key = (string) $key;
}

Expand Down
11 changes: 7 additions & 4 deletions src/Illuminate/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,8 @@
*/
public function get($key, $default = null)
{
$key ??= '';

Check failure on line 482 in src/Illuminate/Collections/Collection.php

View workflow job for this annotation

GitHub Actions / Source Code

Variable $key on left side of ??= always exists and is not nullable.

if (array_key_exists($key, $this->items)) {
return $this->items[$key];
}
Expand All @@ -497,8 +499,8 @@
*/
public function getOrPut($key, $value)
{
if (array_key_exists($key, $this->items)) {
return $this->items[$key];
if (array_key_exists($key ?? '', $this->items)) {
return $this->items[$key ?? ''];
}

$this->offsetSet($key, $value = value($value));
Expand Down Expand Up @@ -539,6 +541,7 @@
is_bool($groupKey) => (int) $groupKey,
$groupKey instanceof \UnitEnum => enum_value($groupKey),
$groupKey instanceof \Stringable => (string) $groupKey,
is_null($groupKey) => (string) $groupKey,
default => $groupKey,
};

Expand Down Expand Up @@ -600,7 +603,7 @@
{
$keys = is_array($key) ? $key : func_get_args();

return array_all($keys, fn ($key) => array_key_exists($key, $this->items));
return array_all($keys, fn ($key) => array_key_exists($key ?? '', $this->items));
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we want to consider null to be ''? If we have an array containing an item with '', it will be returned, when null is given, which can be unintentional.

Copy link
Contributor Author

@IonBazan IonBazan Sep 22, 2025

Choose a reason for hiding this comment

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

Using null as key currently has the same behaviour (PHP 8.4):

> $arr[null] = 'test'
= "test"

> $arr
= [
    "" => "test",
  ]

> $arr['']
= "test"

When it comes to Collection and Arr, I'm a bit on the fence. We can either silently cast it to string for the users to keep things working as before, or let the deprecation be triggered and expect an userland logic change.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the explanation 👍🏻

}

/**
Expand All @@ -617,7 +620,7 @@

$keys = is_array($key) ? $key : func_get_args();

return array_any($keys, fn ($key) => array_key_exists($key, $this->items));
return array_any($keys, fn ($key) => array_key_exists($key ?? '', $this->items));
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Console/Concerns/InteractsWithIO.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,8 @@ protected function setVerbosity($level)
*/
protected function parseVerbosity($level = null)
{
$level ??= '';

if (isset($this->verbosityMap[$level])) {
$level = $this->verbosityMap[$level];
} elseif (! is_int($level)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Support/Testing/Fakes/NotificationFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public function assertNothingSentTo($notifiable)
}

PHPUnit::assertEmpty(
$this->notifications[get_class($notifiable)][$notifiable->getKey()] ?? [],
$this->notifications[get_class($notifiable)][$notifiable->getKey() ?? ''] ?? [],
'Notifications were sent unexpectedly.',
);
}
Expand Down
12 changes: 12 additions & 0 deletions tests/Support/SupportCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2490,6 +2490,18 @@ public function testGetOrPut()
$this->assertSame('male', $data->get('gender'));
}

public function testGetOrPutWithNoKey()
{
$data = new Collection(['taylor', 'shawn']);
$this->assertSame('dayle', $data->getOrPut(null, 'dayle'));
$this->assertSame('john', $data->getOrPut(null, 'john'));
$this->assertSame(['taylor', 'shawn', 'dayle', 'john'], $data->all());

$data = new Collection(['taylor', '' => 'shawn']);
$this->assertSame('shawn', $data->getOrPut(null, 'dayle'));
$this->assertSame(['taylor', '' => 'shawn'], $data->all());
}

public function testPut()
{
$data = new Collection(['name' => 'taylor', 'email' => 'foo']);
Expand Down
2 changes: 1 addition & 1 deletion tests/Support/SupportHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1552,7 +1552,7 @@ public function __construct(protected array $items = [])

public function offsetExists($offset): bool
{
return array_key_exists($offset, $this->items);
return array_key_exists($offset ?? '', $this->items);
}

public function offsetGet($offset): mixed
Expand Down
Loading