Make WordPress Core

Changeset 43034


Ignore:
Timestamp:
04/30/2018 03:42:46 AM (8 years ago)
Author:
SergeyBiryukov
Message:

General: Introduce a polyfill for is_countable() function added in PHP 7.3.

Props jrf, ayeshrajans, desrosj.
See #43583.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/compat.php

    r42343 r43034  
    506506        require_once ABSPATH . WPINC . '/spl-autoload-compat.php';
    507507}
     508
     509if ( ! function_exists( 'is_countable' ) ) {
     510        /**
     511         * Polyfill for is_countable() function added in PHP 7.3.
     512         *
     513         * Verify that the content of a variable is an array or an object
     514         * implementing Countable.
     515         *
     516         * @since 4.9.6
     517         *
     518         * @param mixed $var The value to check.
     519         *
     520         * @return bool True if `$var` is countable, false otherwise.
     521         */
     522        function is_countable( $var ) {
     523                return ( is_array( $var ) || $var instanceof Countable );
     524        }
     525}
  • trunk/tests/phpunit/tests/compat.php

    r42343 r43034  
    187187                $this->assertEquals( array( 'foo' ), $json->decode( '["foo"]' ) );
    188188        }
     189
     190        /**
     191         * @ticket 43583
     192         */
     193        function test_is_countable_availability() {
     194                $this->assertTrue( function_exists( 'is_countable' ) );
     195        }
     196
     197        /**
     198         * Test is_countable() polyfill.
     199         *
     200         * @ticket 43583
     201         *
     202         * @dataProvider countable_variable_test_data
     203         */
     204        function test_is_countable_functionality( $variable, $is_countable ) {
     205                $this->assertEquals( is_countable( $variable ), $is_countable );
     206        }
     207
     208        /**
     209         * Data provider for test_is_countable_functionality().
     210         *
     211         * @ticket 43583
     212         *
     213         * @return array {
     214         *     @type array {
     215         *         @type mixed $variable     Variable to check.
     216         *         @type bool  $is_countable The expected return value of PHP 7.3 is_countable() function.
     217         *     }
     218         * }
     219         */
     220        public function countable_variable_test_data() {
     221                return array(
     222                        array( true, false ),
     223                        array( new stdClass(), false ),
     224                        array( new ArrayIteratorFake(), true ),
     225                        array( new CountableFake(), true ),
     226                        array( 16, false ),
     227                        array( null, false ),
     228                        array( array( 1, 2, 3 ), true ),
     229                        array( (array) 1, true ),
     230                        array( (object) array( 'foo', 'bar', 'baz' ), false ),
     231                );
     232        }
    189233}
    190234
     
    195239        }
    196240}
     241
     242class ArrayIteratorFake extends ArrayIterator {
     243}
     244
     245class CountableFake implements Countable {
     246        public function count() {
     247                return 16;
     248        }
     249}
Note: See TracChangeset for help on using the changeset viewer.