Skip to content

Commit

Permalink
Fix PHP 5.2 notice by ensuring $memo is always an array (#7120)
Browse files Browse the repository at this point in the history
* Failing test case for PHP 5.2 throwing notice

* Use a scalar value, not null, to truly reproduce the issue

* Fix PHP 5.2 notice by ensuring `$memo` is always an array

`array_reduce()` only permits `$initial` to be a mixed variable as of PHP 5.3

* Add a comment to why this is here
  • Loading branch information
danielbachhuber committed Jun 5, 2018
1 parent e86d7b6 commit f67ec84
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,13 @@ function gutenberg_register_scripts_and_styles() {
* @return array Modified reduce accumulator.
*/
function gutenberg_preload_api_request( $memo, $path ) {

// array_reduce() doesn't support passing an array in PHP 5.2
// so we need to make sure we start with one.
if ( ! is_array( $memo ) ) {
$memo = array();
}

if ( empty( $path ) ) {
return $memo;
}
Expand Down
9 changes: 9 additions & 0 deletions phpunit/class-admin-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,13 @@ function test_gutenberg_revisions_restore() {
$link = apply_filters( 'wp_prepare_revision_for_js', array( 'restoreUrl' => 'http:https://test.com' ) );
$this->assertEquals( array( 'restoreUrl' => 'http:https://test.com' ), $link );
}

/**
* Ensure gutenberg_preload_api_request() works without notices in PHP 5.2.
*
* The array_reduce() function only accepts mixed variables starting with PHP 5.3.
*/
function test_preload_api_request_no_notices_php_52() {
$this->assertTrue( is_array( gutenberg_preload_api_request( 0, '/' ) ) );
}
}

0 comments on commit f67ec84

Please sign in to comment.