Make WordPress Core

Changeset 43037 for branches/4.9


Ignore:
Timestamp:
04/30/2018 04:16:43 AM (8 years ago)
Author:
SergeyBiryukov
Message:

General: Introduce a polyfill for is_iterable() function added in PHP 7.1.

Props jrf, schlessera, desrosj.
Merges [43036] to the 4.9 branch.
See #43619.

Location:
branches/4.9
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/4.9

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

    r43035 r43037  
    504504         *
    505505         * Verify that the content of a variable is an array or an object
    506          * implementing Countable.
     506         * implementing the Countable interface.
    507507         *
    508508         * @since 4.9.6
     
    516516        }
    517517}
     518
     519if ( ! function_exists( 'is_iterable' ) ) {
     520        /**
     521         * Polyfill for is_iterable() function added in PHP 7.1.
     522         *
     523         * Verify that the content of a variable is an array or an object
     524         * implementing the Traversable interface.
     525         *
     526         * @since 4.9.6
     527         *
     528         * @param mixed $var The value to check.
     529         *
     530         * @return bool True if `$var` is iterable, false otherwise.
     531         */
     532        function is_iterable( $var ) {
     533                return ( is_array( $var ) || $var instanceof Traversable );
     534        }
     535}
  • branches/4.9/tests/phpunit/tests/compat.php

    r43035 r43037  
    231231                );
    232232        }
     233
     234        /**
     235         * @ticket 43619
     236         */
     237        function test_is_iterable_availability() {
     238                $this->assertTrue( function_exists( 'is_iterable' ) );
     239        }
     240
     241        /**
     242         * Test is_iterable() polyfill.
     243         *
     244         * @ticket 43619
     245         *
     246         * @dataProvider iterable_variable_test_data
     247         */
     248        function test_is_iterable_functionality( $variable, $is_iterable ) {
     249                $this->assertEquals( is_iterable( $variable ), $is_iterable );
     250        }
     251
     252        /**
     253         * Data provider for test_is_iterable_functionality().
     254         *
     255         * @ticket 43619
     256         *
     257         * @return array {
     258         *     @type array {
     259         *         @type mixed $variable    Variable to check.
     260         *         @type bool  $is_iterable The expected return value of PHP 7.1 is_iterable() function.
     261         *     }
     262         * }
     263         */
     264        public function iterable_variable_test_data() {
     265                return array(
     266                        array( array(), true ),
     267                        array( array( 1, 2, 3 ), true ),
     268                        array( new ArrayIterator( array( 1, 2, 3 ) ), true ),
     269                        array( 1, false ),
     270                        array( 3.14, false ),
     271                        array( new stdClass(), false ),
     272                );
     273        }
    233274}
    234275
Note: See TracChangeset for help on using the changeset viewer.