mirrored from git://develop.git.wordpress.org/
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
General: Introduce polyfills for new array related functions in PHP 8.4.
PHP 8.4 introduced four new functions to provide a common way to more easily perform common operations on arrays. - `array_find()`: https://www.php.net/manual/en/function.array-find.php - `array_find_key()`: https://www.php.net/manual/en/function.array-find-key.php - `array_all()`: https://www.php.net/manual/en/function.array-all.php - `array_any()`: https://www.php.net/manual/en/function.array-any.php These functions are now polyfilled making them available on all supported versions of PHP (currently 7.2+). Props Soean, swissspidy, TobiasBg, ayeshrajans, mukesh27, joemcgill. Fixes #62558. git-svn-id: https://develop.svn.wordpress.org/trunk@59783 602fd350-edb4-49c9-b593-d223f7449a82
- Loading branch information
Showing
5 changed files
with
410 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
/** | ||
* @group compat | ||
* | ||
* @covers ::array_all | ||
*/ | ||
class Test_Compat_arrayAll extends WP_UnitTestCase { | ||
|
||
/** | ||
* Test that array_all() is always available (either from PHP or WP). | ||
* | ||
* @ticket 62558 | ||
*/ | ||
public function test_array_all_availability() { | ||
$this->assertTrue( function_exists( 'array_all' ) ); | ||
} | ||
|
||
/** | ||
* @dataProvider data_array_all | ||
* | ||
* @ticket 62558 | ||
* | ||
* @param bool $expected The expected value. | ||
* @param array $arr The array. | ||
* @param callable $callback The callback. | ||
*/ | ||
public function test_array_all( bool $expected, array $arr, callable $callback ) { | ||
$this->assertSame( $expected, array_all( $arr, $callback ) ); | ||
} | ||
|
||
/** | ||
* Data provider. | ||
* | ||
* @return array[] | ||
*/ | ||
public function data_array_all(): array { | ||
return array( | ||
'empty array' => array( | ||
'expected' => true, | ||
'arr' => array(), | ||
'callback' => function ( $value ) { | ||
return 1 === $value; | ||
}, | ||
), | ||
'no match' => array( | ||
'expected' => false, | ||
'arr' => array( 2, 3, 4 ), | ||
'callback' => function ( $value ) { | ||
return 1 === $value; | ||
}, | ||
), | ||
'not all match' => array( | ||
'expected' => false, | ||
'arr' => array( 2, 3, 4 ), | ||
'callback' => function ( $value ) { | ||
return 0 === $value % 2; | ||
}, | ||
), | ||
'match' => array( | ||
'expected' => true, | ||
'arr' => array( 2, 4, 6 ), | ||
'callback' => function ( $value ) { | ||
return 0 === $value % 2; | ||
}, | ||
), | ||
'key match' => array( | ||
'expected' => true, | ||
'arr' => array( | ||
'a' => 2, | ||
'b' => 4, | ||
'c' => 6, | ||
), | ||
'callback' => function ( $value, $key ) { | ||
return strlen( $key ) === 1; | ||
}, | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
/** | ||
* @group compat | ||
* | ||
* @covers ::array_any | ||
*/ | ||
class Test_Compat_arrayAny extends WP_UnitTestCase { | ||
|
||
/** | ||
* Test that array_any() is always available (either from PHP or WP). | ||
* | ||
* @ticket 62558 | ||
*/ | ||
public function test_array_any_availability() { | ||
$this->assertTrue( function_exists( 'array_any' ) ); | ||
} | ||
|
||
/** | ||
* @dataProvider data_array_any | ||
* | ||
* @ticket 62558 | ||
* | ||
* @param bool $expected The expected value. | ||
* @param array $arr The array. | ||
* @param callable $callback The callback. | ||
*/ | ||
public function test_array_any( bool $expected, array $arr, callable $callback ) { | ||
$this->assertSame( $expected, array_any( $arr, $callback ) ); | ||
} | ||
|
||
/** | ||
* Data provider. | ||
* | ||
* @return array[] | ||
*/ | ||
public function data_array_any(): array { | ||
return array( | ||
'empty array' => array( | ||
'expected' => false, | ||
'arr' => array(), | ||
'callback' => function ( $value ) { | ||
return 1 === $value; | ||
}, | ||
), | ||
'no match' => array( | ||
'expected' => false, | ||
'arr' => array( 2, 3, 4 ), | ||
'callback' => function ( $value ) { | ||
return 1 === $value; | ||
}, | ||
), | ||
'match' => array( | ||
'expected' => true, | ||
'arr' => array( 2, 3, 4 ), | ||
'callback' => function ( $value ) { | ||
return 3 === $value; | ||
}, | ||
), | ||
'key match' => array( | ||
'expected' => true, | ||
'arr' => array( | ||
'a' => 2, | ||
'b' => 3, | ||
'c' => 4, | ||
), | ||
'callback' => function ( $value, $key ) { | ||
return 'c' === $key; | ||
}, | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
/** | ||
* @group compat | ||
* | ||
* @covers ::array_find | ||
*/ | ||
class Tests_Compat_arrayFind extends WP_UnitTestCase { | ||
|
||
/** | ||
* Test that array_find() is always available (either from PHP or WP). | ||
* | ||
* @ticket 62558 | ||
*/ | ||
public function test_array_find_availability() { | ||
$this->assertTrue( function_exists( 'array_find' ) ); | ||
} | ||
|
||
/** | ||
* @dataProvider data_array_find | ||
* | ||
* @ticket 62558 | ||
* | ||
* @param mixed $expected The expected value. | ||
* @param array $arr The array. | ||
* @param callable $callback The needle. | ||
*/ | ||
public function test_array_find( $expected, array $arr, callable $callback ) { | ||
$this->assertSame( $expected, array_find( $arr, $callback ) ); | ||
} | ||
|
||
/** | ||
* Data provider. | ||
* | ||
* @return array[] | ||
*/ | ||
public function data_array_find(): array { | ||
return array( | ||
'empty array' => array( | ||
'expected' => null, | ||
'arr' => array(), | ||
'callback' => function ( $value ) { | ||
return 1 === $value; | ||
}, | ||
), | ||
'no match' => array( | ||
'expected' => null, | ||
'arr' => array( 2, 3, 4 ), | ||
'callback' => function ( $value ) { | ||
return 1 === $value; | ||
}, | ||
), | ||
'match' => array( | ||
'expected' => 3, | ||
'arr' => array( 2, 3, 4 ), | ||
'callback' => function ( $value ) { | ||
return 3 === $value; | ||
}, | ||
), | ||
'key match' => array( | ||
'expected' => 3, | ||
'arr' => array( | ||
'a' => 2, | ||
'b' => 3, | ||
'c' => 4, | ||
), | ||
'callback' => function ( $value ) { | ||
return 3 === $value; | ||
}, | ||
), | ||
'two callback matches' => array( | ||
'expected' => 2, | ||
'arr' => array( 2, 3, 4 ), | ||
'callback' => function ( $value ) { | ||
return 0 === $value % 2; | ||
}, | ||
), | ||
|
||
); | ||
} | ||
} |
Oops, something went wrong.