Skip to content

Commit 20efa08

Browse files
committed
[PHP 8.5] Add array_first and array_last
Adds polyfills for the PHP 8.5's `array_first` and `array_last` functions. The polyfills would have been a bit cleaner if we used the `array_key_first` and `array_key_last` functions, but they were added in PHP 7.3, and are not available on PHP 7.2. This adds the same tests from php-src patch. - [RFC: `array_first()` and `array_last()`](https://wiki.php.net/rfc/array_first_last) - [php-src commit](php/php-src@168343d) - [PHP.Watch polyfill for PHP 8.0+](https://php.watch/versions/8.5/array_first-array_last)
1 parent c4ee386 commit 20efa08

File tree

5 files changed

+62
-0
lines changed

5 files changed

+62
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ Polyfills are provided for:
7474
- the `CURL_HTTP_VERSION_3` and `CURL_HTTP_VERSION_3ONLY` constants introduced in PHP 8.4;
7575
- the `get_error_handler` and `get_exception_handler` functions introduced in PHP 8.5;
7676
- the `NoDiscard` attribute introduced in PHP 8.5;
77+
- the `array_first` and `array_last` functions introduced in PHP 8.5;
7778

7879
It is strongly recommended to upgrade your PHP version and/or install the missing
7980
extensions whenever possible. This polyfill should be used only when there is no

src/Php85/Php85.php

+17
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,21 @@ public static function get_exception_handler(): ?callable
3333

3434
return $handler;
3535
}
36+
37+
public static function array_first(array $array)
38+
{
39+
foreach ($array as $value) {
40+
return $value;
41+
}
42+
return null;
43+
}
44+
45+
public static function array_last(array $array)
46+
{
47+
if ($array === []) {
48+
return null;
49+
}
50+
51+
return end($array);
52+
}
3653
}

src/Php85/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ This component provides features added to PHP 8.5 core:
55

66
- [`get_error_handler` and `get_exception_handler`](https://wiki.php.net/rfc/get-error-exception-handler)
77
- [`NoDiscard`](https://wiki.php.net/rfc/marking_return_value_as_important)
8+
- [`array_first` and `array_last`](https://wiki.php.net/rfc/array_first_last)
89

910
More information can be found in the
1011
[main Polyfill README](https://github.com/symfony/polyfill/blob/main/README.md).

src/Php85/bootstrap.php

+8
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,11 @@ function get_error_handler(): ?callable { return p\Php85::get_error_handler(); }
2222
if (!function_exists('get_exception_handler')) {
2323
function get_exception_handler(): ?callable { return p\Php85::get_exception_handler(); }
2424
}
25+
26+
if (!function_exists('array_first')) {
27+
function array_first(array $array) { return p\Php85::array_first($array); }
28+
}
29+
30+
if (!function_exists('array_last')) {
31+
function array_last(array $array) { return p\Php85::array_last($array); }
32+
}

tests/Php85/Php85Test.php

+35
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,41 @@ public static function provideHandler()
7878
$handler = new TestHandlerInvokable();
7979
yield [$handler, $handler];
8080
}
81+
82+
public function testArrayFirstArrayLast(): void
83+
{
84+
$this->assertNull(array_first([]));
85+
$this->assertNull(array_last([]));
86+
87+
$array = [1, 2, 3];
88+
unset($array[0], $array[1], $array[2]);
89+
90+
$this->assertNull(array_first([]));
91+
$this->assertNull(array_last([]));
92+
93+
94+
$this->assertSame("single element", array_first(["single element"]));
95+
$this->assertSame("single element", array_last(["single element"]));
96+
97+
$str = "hello world";
98+
$this->assertSame($str, array_first([&$str, 1]));
99+
$this->assertSame(1, array_last([&$str, 1]));
100+
101+
$this->assertSame(1, array_first([1, &$str]));
102+
$this->assertSame($str, array_last([1, &$str]));
103+
104+
$this->assertSame(1, array_first([1 => 1, 0 => 0, 3 => 3, 2 => 2]));
105+
$this->assertSame(2, array_last([1 => 1, 0 => 0, 3 => 3, 2 => 2]));
106+
107+
$this->assertSame([], array_first([100 => []]));
108+
$this->assertSame([], array_last([100 => []]));
109+
110+
$this->assertEquals(new \stdClass(), array_first([new \stdClass, false]));
111+
$this->assertFalse(array_last([new \stdClass, false]));
112+
113+
$this->assertTrue(array_first([true, new \stdClass]));
114+
$this->assertEquals(new \stdClass(), array_last([true, new \stdClass]));
115+
}
81116
}
82117

83118
class TestHandler

0 commit comments

Comments
 (0)