Skip to content

Commit 07feb01

Browse files
General: In the is_countable() polyfill, if the provided object implements SimpleXMLElement or ResourceBundle, consider it countable.
Props ayeshrajans, jrf, desrosj. Fixes #43583. git-svn-id: https://develop.svn.wordpress.org/trunk@43220 602fd350-edb4-49c9-b593-d223f7449a82
1 parent a322b94 commit 07feb01

2 files changed

Lines changed: 43 additions & 3 deletions

File tree

src/wp-includes/compat.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,11 @@ function array_replace_recursive( $base = array(), $replacements = array() ) {
520520
* @return bool True if `$var` is countable, false otherwise.
521521
*/
522522
function is_countable( $var ) {
523-
return ( is_array( $var ) || $var instanceof Countable );
523+
return ( is_array( $var )
524+
|| $var instanceof Countable
525+
|| $var instanceof SimpleXMLElement
526+
|| $var instanceof ResourceBundle
527+
);
524528
}
525529
}
526530

tests/phpunit/tests/compat.php

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,8 @@ function test_json_encode_decode() {
188188
}
189189

190190
/**
191+
* Test that is_countable() is always available (either from PHP or WP).
192+
*
191193
* @ticket 43583
192194
*/
193195
function test_is_countable_availability() {
@@ -200,9 +202,12 @@ function test_is_countable_availability() {
200202
* @ticket 43583
201203
*
202204
* @dataProvider countable_variable_test_data
205+
*
206+
* @param mixed $variable Variable to check.
207+
* @param bool $is_countable The expected return value of PHP 7.3 is_countable() function.
203208
*/
204209
function test_is_countable_functionality( $variable, $is_countable ) {
205-
$this->assertEquals( is_countable( $variable ), $is_countable );
210+
$this->assertSame( is_countable( $variable ), $is_countable );
206211
}
207212

208213
/**
@@ -232,6 +237,34 @@ public function countable_variable_test_data() {
232237
}
233238

234239
/**
240+
* Test is_countable() polyfill for ResourceBundle.
241+
*
242+
* @ticket 43583
243+
*/
244+
function test_is_countable_ResourceBundle() {
245+
if ( ! class_exists( 'ResourceBundle' ) ) {
246+
$this->markTestSkipped( 'The intl extension is not loaded. ResourceBundle not tested for is_countable().' );
247+
}
248+
249+
$this->assertTrue( is_countable( new ResourceBundle( 'en', null ) ) );
250+
}
251+
252+
/**
253+
* Test is_countable() polyfill for SimpleXMLElement.
254+
*
255+
* @ticket 43583
256+
*/
257+
function test_is_countable_SimpleXMLElement() {
258+
if ( ! class_exists( 'SimpleXMLElement' ) ) {
259+
$this->markTestSkipped( 'The xml extension is not loaded. SimpleXMLElement not tested for is_countable().' );
260+
}
261+
262+
$this->assertTrue( is_countable( new SimpleXMLElement( '<xml><tag>1</tag><tag>2</tag></xml>' ) ) );
263+
}
264+
265+
/**
266+
* Test that is_iterable() is always available (either from PHP or WP).
267+
*
235268
* @ticket 43619
236269
*/
237270
function test_is_iterable_availability() {
@@ -244,9 +277,12 @@ function test_is_iterable_availability() {
244277
* @ticket 43619
245278
*
246279
* @dataProvider iterable_variable_test_data
280+
*
281+
* @param mixed $variable Variable to check.
282+
* @param bool $is_iterable The expected return value of PHP 7.1 is_iterable() function.
247283
*/
248284
function test_is_iterable_functionality( $variable, $is_iterable ) {
249-
$this->assertEquals( is_iterable( $variable ), $is_iterable );
285+
$this->assertSame( is_iterable( $variable ), $is_iterable );
250286
}
251287

252288
/**

0 commit comments

Comments
 (0)