Make WordPress Core

Ticket #55105: 55105.diff

File 55105.diff, 3.8 KB (added by costdev, 3 years ago)
  • src/wp-includes/compat.php

    diff --git a/src/wp-includes/compat.php b/src/wp-includes/compat.php
    index ac78f7f9a3..3544a43a85 100644
    a b if ( ! function_exists( 'str_ends_with' ) ) { 
    478478        }
    479479}
    480480
     481if ( ! function_exists( 'array_is_list' ) ) {
     482        /**
     483         * Polyfill for `array_is_list()` function added in PHP 8.1.
     484         *
     485         * Determines if the given array is a list.
     486         *
     487         * An array is considered a list if its keys consist
     488         * of consecutive numbers from 0 to count( $array ) - 1.
     489         *
     490         * This function returns `true` on empty arrays.
     491         *
     492         * @since 6.0.0
     493         *
     494         * @param array $arr The array.
     495         * @return bool True if array is a list, false otherwise.
     496         */
     497        function array_is_list( array $arr ) {
     498                $i = 0;
     499                foreach ( $arr as $k => $v ) {
     500                        if ( $k !== $i++ ) {
     501                                return false;
     502                        }
     503                }
     504                return true;
     505        }
     506}
     507
    481508// IMAGETYPE_WEBP constant is only defined in PHP 7.1 or later.
    482509if ( ! defined( 'IMAGETYPE_WEBP' ) ) {
    483510        define( 'IMAGETYPE_WEBP', 18 );
  • new file tests/phpunit/tests/compat/arrayIsList.php

    diff --git a/tests/phpunit/tests/compat/arrayIsList.php b/tests/phpunit/tests/compat/arrayIsList.php
    new file mode 100644
    index 0000000000..92f37fc6ac
    - +  
     1<?php
     2
     3/**
     4 * @group compat
     5 *
     6 * @covers ::array_is_list
     7 */
     8class Tests_Compat_arrayIsList extends WP_UnitTestCase {
     9
     10        /**
     11         * Test that array_is_list() is always available (either from PHP or WP).
     12         *
     13         * @ticket 55105
     14         */
     15        public function test_array_is_list_availability() {
     16                $this->assertTrue( function_exists( 'array_is_list' ) );
     17        }
     18
     19        /**
     20         * @dataProvider data_array_is_list
     21         *
     22         * @ticket 55105
     23         *
     24         * @param bool  $expected Whether the array is a list.
     25         * @param array $arr      The array.
     26         */
     27        public function test_array_is_list( $expected, $arr ) {
     28                if ( ! function_exists( 'array_is_list' ) ) {
     29                        $this->markTestSkipped( 'array_is_list() is not available.' );
     30                }
     31
     32                $this->assertSame( $expected, array_is_list( $arr ) );
     33        }
     34
     35        /**
     36         * Data provider.
     37         *
     38         * @return array[]
     39         */
     40        public function data_array_is_list() {
     41                return array(
     42                        'empty array'                   => array(
     43                                'expected' => true,
     44                                'arr'      => array(),
     45                        ),
     46                        'array(NAN)'                    => array(
     47                                'expected' => true,
     48                                'arr'      => array( NAN ),
     49                        ),
     50                        'array( INF )'                  => array(
     51                                'expected' => true,
     52                                'arr'      => array( INF ),
     53                        ),
     54                        'consecutive int keys from 0'   => array(
     55                                'expected' => true,
     56                                'arr'      => array(
     57                                        0 => 'one',
     58                                        1 => 'two',
     59                                ),
     60                        ),
     61                        'consecutive float keys from 0' => array(
     62                                'expected' => true,
     63                                'arr'      => array(
     64                                        0.0 => 'one',
     65                                        1.0 => 'two',
     66                                ),
     67                        ),
     68                        'consecutive str keys from 0'   => array(
     69                                'expected' => true,
     70                                'arr'      => array(
     71                                        '0' => 'one',
     72                                        '1' => 'two',
     73                                ),
     74                        ),
     75                        'consecutive int keys from 1'   => array(
     76                                'expected' => false,
     77                                'arr'      => array(
     78                                        1 => 'one',
     79                                        2 => 'two',
     80                                ),
     81                        ),
     82                        'consecutive float keys from 1' => array(
     83                                'expected' => false,
     84                                'arr'      => array(
     85                                        1.0 => 'one',
     86                                        2.0 => 'two',
     87                                ),
     88                        ),
     89                        'consecutive str keys from 1'   => array(
     90                                'expected' => false,
     91                                'arr'      => array(
     92                                        '1' => 'one',
     93                                        '2' => 'two',
     94                                ),
     95                        ),
     96                        'non-consecutive int keys'      => array(
     97                                'expected' => false,
     98                                'arr'      => array(
     99                                        1 => 'one',
     100                                        0 => 'two',
     101                                ),
     102                        ),
     103                        'non-consecutive float keys'    => array(
     104                                'expected' => false,
     105                                'arr'      => array(
     106                                        1.0 => 'one',
     107                                        0.0 => 'two',
     108                                ),
     109                        ),
     110                        'non-consecutive string keys'   => array(
     111                                'expected' => false,
     112                                'arr'      => array(
     113                                        '1' => 'one',
     114                                        '0' => 'two',
     115                                ),
     116                        ),
     117                );
     118        }
     119}