Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/wp-includes/compat.php
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,33 @@ function str_ends_with( $haystack, $needle ) {
}
}

if ( ! function_exists( 'array_is_list' ) ) {
/**
* Polyfill for `array_is_list()` function added in PHP 8.1.
*
* Determines if the given array is a list.
*
* An array is considered a list if its keys consist
* of consecutive numbers from 0 to count( $array ) - 1.
*
* This function returns `true` on empty arrays.
*
* @since 6.0.0
*
* @param array $arr The array.
* @return bool True if array is a list, false otherwise.
*/
function array_is_list( array $arr ) {
$i = 0;
foreach ( $arr as $k => $v ) {
if ( $k !== $i++ ) {
return false;
}
}
return true;
}
}

// IMAGETYPE_WEBP constant is only defined in PHP 7.1 or later.
if ( ! defined( 'IMAGETYPE_WEBP' ) ) {
define( 'IMAGETYPE_WEBP', 18 );
Expand Down
115 changes: 115 additions & 0 deletions tests/phpunit/tests/compat/arrayIsList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

/**
* @group compat
*
* @covers ::array_is_list
*/
class Tests_Compat_arrayIsList extends WP_UnitTestCase {

/**
* Test that array_is_list() is always available (either from PHP or WP).
*
* @ticket 55105
*/
public function test_array_is_list_availability() {
$this->assertTrue( function_exists( 'array_is_list' ) );
}

/**
* @dataProvider data_array_is_list
*
* @ticket 55105
*
* @param bool $expected Whether the array is a list.
* @param array $arr The array.
*/
public function test_array_is_list( $expected, $arr ) {
if ( ! function_exists( 'array_is_list' ) ) {
$this->markTestSkipped( 'array_is_list() is not available.' );
}

$this->assertSame( $expected, array_is_list( $arr ) );
}

/**
* Data provider.
*
* @return array[]
*/
public function data_array_is_list() {
return array(
'empty array' => array(
'expected' => true,
'arr' => array(),
),
'array( INF )' => array(
'expected' => true,
'arr' => array( INF ),
),
'consecutive int keys from 0' => array(
'expected' => true,
'arr' => array(
0 => 'one',
1 => 'two',
),
),
'consecutive float keys from 0' => array(
'expected' => true,
'arr' => array(
0.0 => 'one',
1.0 => 'two',
),
),
'consecutive str keys from 0' => array(
'expected' => true,
'arr' => array(
'0' => 'one',
'1' => 'two',
),
),
'consecutive int keys from 1' => array(
'expected' => false,
'arr' => array(
1 => 'one',
2 => 'two',
),
),
'consecutive float keys from 1' => array(
'expected' => false,
'arr' => array(
1.0 => 'one',
2.0 => 'two',
),
),
'consecutive str keys from 1' => array(
'expected' => false,
'arr' => array(
'1' => 'one',
'2' => 'two',
),
),
'non-consecutive int keys' => array(
'expected' => false,
'arr' => array(
1 => 'one',
0 => 'two',
),
),
'non-consecutive float keys' => array(
'expected' => false,
'arr' => array(
1.0 => 'one',
0.0 => 'two',
),
),
'non-consecutive string keys' => array(
'expected' => false,
'arr' => array(
'1' => 'one',
'0' => 'two',
),
),
);
}
}