Make WordPress Core

Changeset 43035 for branches/4.9


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

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

Props jrf, ayeshrajans, desrosj.
Merges [43034] to the 4.9 branch.
See #43583.

Location:
branches/4.9
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/4.9

  • branches/4.9/src/wp-includes/compat.php

    r41178 r43035  
    498498        require_once ABSPATH . WPINC . '/spl-autoload-compat.php';
    499499}
     500
     501if ( ! function_exists( 'is_countable' ) ) {
     502        /**
     503         * Polyfill for is_countable() function added in PHP 7.3.
     504         *
     505         * Verify that the content of a variable is an array or an object
     506         * implementing Countable.
     507         *
     508         * @since 4.9.6
     509         *
     510         * @param mixed $var The value to check.
     511         *
     512         * @return bool True if `$var` is countable, false otherwise.
     513         */
     514        function is_countable( $var ) {
     515                return ( is_array( $var ) || $var instanceof Countable );
     516        }
     517}
  • branches/4.9/tests/phpunit/tests/compat.php

    r32364 r43035  
    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.