diff --git a/src/Illuminate/Collections/Arr.php b/src/Illuminate/Collections/Arr.php index d9505e6a3568..4f941f939bb0 100644 --- a/src/Illuminate/Collections/Arr.php +++ b/src/Illuminate/Collections/Arr.php @@ -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; } diff --git a/src/Illuminate/Collections/Collection.php b/src/Illuminate/Collections/Collection.php index 02303837aeb6..0f1db82e8083 100644 --- a/src/Illuminate/Collections/Collection.php +++ b/src/Illuminate/Collections/Collection.php @@ -479,6 +479,8 @@ public function forget($keys) */ public function get($key, $default = null) { + $key ??= ''; + if (array_key_exists($key, $this->items)) { return $this->items[$key]; } @@ -497,8 +499,8 @@ public function get($key, $default = null) */ 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)); @@ -539,6 +541,7 @@ public function groupBy($groupBy, $preserveKeys = false) is_bool($groupKey) => (int) $groupKey, $groupKey instanceof \UnitEnum => enum_value($groupKey), $groupKey instanceof \Stringable => (string) $groupKey, + is_null($groupKey) => (string) $groupKey, default => $groupKey, }; @@ -600,7 +603,7 @@ public function has($key) { $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)); } /** @@ -617,7 +620,7 @@ public function hasAny($key) $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)); } /** diff --git a/src/Illuminate/Console/Concerns/InteractsWithIO.php b/src/Illuminate/Console/Concerns/InteractsWithIO.php index f4cf5daad08b..706d9379f7f4 100644 --- a/src/Illuminate/Console/Concerns/InteractsWithIO.php +++ b/src/Illuminate/Console/Concerns/InteractsWithIO.php @@ -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)) { diff --git a/src/Illuminate/Support/Testing/Fakes/NotificationFake.php b/src/Illuminate/Support/Testing/Fakes/NotificationFake.php index 9403e31b0e2e..9e6515ce6e4f 100644 --- a/src/Illuminate/Support/Testing/Fakes/NotificationFake.php +++ b/src/Illuminate/Support/Testing/Fakes/NotificationFake.php @@ -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.', ); } diff --git a/tests/Support/SupportCollectionTest.php b/tests/Support/SupportCollectionTest.php index b068473584ac..81a6a3fd7dbe 100755 --- a/tests/Support/SupportCollectionTest.php +++ b/tests/Support/SupportCollectionTest.php @@ -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']); diff --git a/tests/Support/SupportHelpersTest.php b/tests/Support/SupportHelpersTest.php index fd6a28cf5c50..7e04a98eff15 100644 --- a/tests/Support/SupportHelpersTest.php +++ b/tests/Support/SupportHelpersTest.php @@ -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