Skip to content

Commit e8ed95d

Browse files
[12.x] Fix PHP 8.5 null-key deprecations (#57137)
* Fix PHP 8.5 null-key deprecations * Fix getOrPut * Update InteractsWithIO.php * Update Collection.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 4e42ded commit e8ed95d

File tree

6 files changed

+24
-7
lines changed

6 files changed

+24
-7
lines changed

src/Illuminate/Collections/Arr.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ public static function exists($array, $key)
230230
return $array->offsetExists($key);
231231
}
232232

233-
if (is_float($key)) {
233+
if (is_float($key) || is_null($key)) {
234234
$key = (string) $key;
235235
}
236236

src/Illuminate/Collections/Collection.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,8 @@ public function forget($keys)
479479
*/
480480
public function get($key, $default = null)
481481
{
482+
$key ??= '';
483+
482484
if (array_key_exists($key, $this->items)) {
483485
return $this->items[$key];
484486
}
@@ -497,8 +499,8 @@ public function get($key, $default = null)
497499
*/
498500
public function getOrPut($key, $value)
499501
{
500-
if (array_key_exists($key, $this->items)) {
501-
return $this->items[$key];
502+
if (array_key_exists($key ?? '', $this->items)) {
503+
return $this->items[$key ?? ''];
502504
}
503505

504506
$this->offsetSet($key, $value = value($value));
@@ -539,6 +541,7 @@ public function groupBy($groupBy, $preserveKeys = false)
539541
is_bool($groupKey) => (int) $groupKey,
540542
$groupKey instanceof \UnitEnum => enum_value($groupKey),
541543
$groupKey instanceof \Stringable => (string) $groupKey,
544+
is_null($groupKey) => (string) $groupKey,
542545
default => $groupKey,
543546
};
544547

@@ -600,7 +603,7 @@ public function has($key)
600603
{
601604
$keys = is_array($key) ? $key : func_get_args();
602605

603-
return array_all($keys, fn ($key) => array_key_exists($key, $this->items));
606+
return array_all($keys, fn ($key) => array_key_exists($key ?? '', $this->items));
604607
}
605608

606609
/**
@@ -617,7 +620,7 @@ public function hasAny($key)
617620

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

620-
return array_any($keys, fn ($key) => array_key_exists($key, $this->items));
623+
return array_any($keys, fn ($key) => array_key_exists($key ?? '', $this->items));
621624
}
622625

623626
/**

src/Illuminate/Console/Concerns/InteractsWithIO.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,8 @@ protected function setVerbosity($level)
430430
*/
431431
protected function parseVerbosity($level = null)
432432
{
433+
$level ??= '';
434+
433435
if (isset($this->verbosityMap[$level])) {
434436
$level = $this->verbosityMap[$level];
435437
} elseif (! is_int($level)) {

src/Illuminate/Support/Testing/Fakes/NotificationFake.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public function assertNothingSentTo($notifiable)
195195
}
196196

197197
PHPUnit::assertEmpty(
198-
$this->notifications[get_class($notifiable)][$notifiable->getKey()] ?? [],
198+
$this->notifications[get_class($notifiable)][$notifiable->getKey() ?? ''] ?? [],
199199
'Notifications were sent unexpectedly.',
200200
);
201201
}

tests/Support/SupportCollectionTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2490,6 +2490,18 @@ public function testGetOrPut()
24902490
$this->assertSame('male', $data->get('gender'));
24912491
}
24922492

2493+
public function testGetOrPutWithNoKey()
2494+
{
2495+
$data = new Collection(['taylor', 'shawn']);
2496+
$this->assertSame('dayle', $data->getOrPut(null, 'dayle'));
2497+
$this->assertSame('john', $data->getOrPut(null, 'john'));
2498+
$this->assertSame(['taylor', 'shawn', 'dayle', 'john'], $data->all());
2499+
2500+
$data = new Collection(['taylor', '' => 'shawn']);
2501+
$this->assertSame('shawn', $data->getOrPut(null, 'dayle'));
2502+
$this->assertSame(['taylor', '' => 'shawn'], $data->all());
2503+
}
2504+
24932505
public function testPut()
24942506
{
24952507
$data = new Collection(['name' => 'taylor', 'email' => 'foo']);

tests/Support/SupportHelpersTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1552,7 +1552,7 @@ public function __construct(protected array $items = [])
15521552

15531553
public function offsetExists($offset): bool
15541554
{
1555-
return array_key_exists($offset, $this->items);
1555+
return array_key_exists($offset ?? '', $this->items);
15561556
}
15571557

15581558
public function offsetGet($offset): mixed

0 commit comments

Comments
 (0)