Skip to content

Commit 4277272

Browse files
committed
Load: Ensure wp_using_ext_object_cache() always returns a boolean.
1 parent 051cae3 commit 4277272

2 files changed

Lines changed: 44 additions & 11 deletions

File tree

src/wp-includes/load.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -808,20 +808,21 @@ function wp_set_wpdb_vars() {
808808
* @return bool The current 'using' setting.
809809
*/
810810
function wp_using_ext_object_cache( $using = null ) {
811-
global $_wp_using_ext_object_cache;
811+
global $_wp_using_ext_object_cache;
812812

813-
// Save the current state to return later.
814-
$current_using = $_wp_using_ext_object_cache;
813+
// Save the current state to return later.
814+
$current_using = $_wp_using_ext_object_cache;
815815

816-
if ( null !== $using ) {
817-
$_wp_using_ext_object_cache = (bool) $using;
818-
}
816+
if ( null !== $using ) {
817+
$_wp_using_ext_object_cache = (bool) $using;
818+
}
819+
820+
if ( null === $_wp_using_ext_object_cache ) {
821+
// If the global is uninitialized, the value would be null, which violates the type signature.
822+
return false;
823+
}
819824

820-
/**
821-
* Ensure the returned value is always a boolean.
822-
* If the global is uninitialized, it could be null, which violates the type signature.
823-
*/
824-
return (bool) $current_using;
825+
return (bool) $current_using;
825826
}
826827

827828
/**
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/**
4+
* Tests for wp_using_ext_object_cache().
5+
*
6+
* @group load
7+
*
8+
* @covers ::wp_using_ext_object_cache
9+
*/
10+
class Tests_Load_wpUsingExtObjectCache extends WP_UnitTestCase {
11+
12+
private $orig_using_ext_cache;
13+
14+
public function set_up() {
15+
parent::set_up();
16+
global $_wp_using_ext_object_cache;
17+
18+
$this->orig_using_ext_cache = $_wp_using_ext_object_cache;
19+
}
20+
21+
public function tear_down() {
22+
global $_wp_using_ext_object_cache;
23+
24+
$_wp_using_ext_object_cache = $this->orig_using_ext_cache;
25+
parent::tear_down();
26+
}
27+
28+
public function test_should_always_return_boolean() {
29+
wp_using_ext_object_cache( 1 );
30+
$this->assertIsBool( wp_using_ext_object_cache() );
31+
}
32+
}

0 commit comments

Comments
 (0)