From 170e2df1674910ed3e419df7cf02557425268cd9 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Mon, 30 Apr 2018 03:46:07 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/branches/4.9@43035 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/compat.php | 18 ++++++++++++ tests/phpunit/tests/compat.php | 53 ++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/src/wp-includes/compat.php b/src/wp-includes/compat.php index 7e37c30762..a0976c1ef2 100644 --- a/src/wp-includes/compat.php +++ b/src/wp-includes/compat.php @@ -497,3 +497,21 @@ endif; if ( ! function_exists( 'spl_autoload_register' ) ) { require_once ABSPATH . WPINC . '/spl-autoload-compat.php'; } + +if ( ! function_exists( 'is_countable' ) ) { + /** + * Polyfill for is_countable() function added in PHP 7.3. + * + * Verify that the content of a variable is an array or an object + * implementing Countable. + * + * @since 4.9.6 + * + * @param mixed $var The value to check. + * + * @return bool True if `$var` is countable, false otherwise. + */ + function is_countable( $var ) { + return ( is_array( $var ) || $var instanceof Countable ); + } +} diff --git a/tests/phpunit/tests/compat.php b/tests/phpunit/tests/compat.php index 5aa749225d..1e2039b4ea 100644 --- a/tests/phpunit/tests/compat.php +++ b/tests/phpunit/tests/compat.php @@ -186,6 +186,50 @@ EOT; $this->assertEquals( '["foo"]', $json->encodeUnsafe( array( 'foo' ) ) ); $this->assertEquals( array( 'foo' ), $json->decode( '["foo"]' ) ); } + + /** + * @ticket 43583 + */ + function test_is_countable_availability() { + $this->assertTrue( function_exists( 'is_countable' ) ); + } + + /** + * Test is_countable() polyfill. + * + * @ticket 43583 + * + * @dataProvider countable_variable_test_data + */ + function test_is_countable_functionality( $variable, $is_countable ) { + $this->assertEquals( is_countable( $variable ), $is_countable ); + } + + /** + * Data provider for test_is_countable_functionality(). + * + * @ticket 43583 + * + * @return array { + * @type array { + * @type mixed $variable Variable to check. + * @type bool $is_countable The expected return value of PHP 7.3 is_countable() function. + * } + * } + */ + public function countable_variable_test_data() { + return array( + array( true, false ), + array( new stdClass(), false ), + array( new ArrayIteratorFake(), true ), + array( new CountableFake(), true ), + array( 16, false ), + array( null, false ), + array( array( 1, 2, 3 ), true ), + array( (array) 1, true ), + array( (object) array( 'foo', 'bar', 'baz' ), false ), + ); + } } /* used in test_mb_substr_phpcore */ @@ -194,3 +238,12 @@ class classA { return "Class A object"; } } + +class ArrayIteratorFake extends ArrayIterator { +} + +class CountableFake implements Countable { + public function count() { + return 16; + } +}