Skip to content

Commit 087ce3a

Browse files
[12.x] Use PHP 8.4 array helpers (#56619)
* Use array_find_key helper in Arr::first * Use new array helpers in Support Collection functions * Ensure installation of Symfony PHP 8.4 polyfills --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 7eeeed0 commit 087ce3a

File tree

4 files changed

+9
-31
lines changed

4 files changed

+9
-31
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"symfony/mailer": "^7.2.0",
5353
"symfony/mime": "^7.2.0",
5454
"symfony/polyfill-php83": "^1.31",
55+
"symfony/polyfill-php84": "^1.31",
5556
"symfony/polyfill-php85": "^1.31",
5657
"symfony/process": "^7.2.0",
5758
"symfony/routing": "^7.2.0",

src/Illuminate/Collections/Arr.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -263,13 +263,9 @@ public static function first($array, ?callable $callback = null, $default = null
263263
return value($default);
264264
}
265265

266-
foreach ($array as $key => $value) {
267-
if ($callback($value, $key)) {
268-
return $value;
269-
}
270-
}
266+
$key = array_find_key($array, $callback);
271267

272-
return value($default);
268+
return $key !== null ? $array[$key] : value($default);
273269
}
274270

275271
/**

src/Illuminate/Collections/Collection.php

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,7 @@ public function contains($key, $operator = null, $value = null)
184184
{
185185
if (func_num_args() === 1) {
186186
if ($this->useAsCallable($key)) {
187-
$placeholder = new stdClass;
188-
189-
return $this->first($key, $placeholder) !== $placeholder;
187+
return array_any($this->items, $key);
190188
}
191189

192190
return in_array($key, $this->items);
@@ -598,13 +596,7 @@ public function has($key)
598596
{
599597
$keys = is_array($key) ? $key : func_get_args();
600598

601-
foreach ($keys as $value) {
602-
if (! array_key_exists($value, $this->items)) {
603-
return false;
604-
}
605-
}
606-
607-
return true;
599+
return array_all($keys, fn ($key) => array_key_exists($key, $this->items));
608600
}
609601

610602
/**
@@ -621,13 +613,7 @@ public function hasAny($key)
621613

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

624-
foreach ($keys as $value) {
625-
if (array_key_exists($value, $this->items)) {
626-
return true;
627-
}
628-
}
629-
630-
return false;
616+
return array_any($keys, fn ($key) => array_key_exists($key, $this->items));
631617
}
632618

633619
/**
@@ -1188,13 +1174,7 @@ public function search($value, $strict = false)
11881174
return array_search($value, $this->items, $strict);
11891175
}
11901176

1191-
foreach ($this->items as $key => $item) {
1192-
if ($value($item, $key)) {
1193-
return $key;
1194-
}
1195-
}
1196-
1197-
return false;
1177+
return array_find_key($this->items, $value) ?? false;
11981178
}
11991179

12001180
/**

src/Illuminate/Collections/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"php": "^8.2",
1818
"illuminate/conditionable": "^12.0",
1919
"illuminate/contracts": "^12.0",
20-
"illuminate/macroable": "^12.0"
20+
"illuminate/macroable": "^12.0",
21+
"symfony/polyfill-php84": "^1.31"
2122
},
2223
"autoload": {
2324
"psr-4": {

0 commit comments

Comments
 (0)