Skip to content

Commit

Permalink
Code Modernisation: Fix known instances of array access on data types…
Browse files Browse the repository at this point in the history
… that can't be accessed as arrays.

PHP 7.4 addes a warning when trying access a null/bool/int/float/resource (everything but array, string and object) as if it were an array.

This change fixes all of these warnings visible in unit tests.

Props jrf.
See #47704.




git-svn-id: https://develop.svn.wordpress.org/trunk@45639 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
pento committed Jul 15, 2019
1 parent e6c750b commit 2da7f9f
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 20 deletions.
8 changes: 4 additions & 4 deletions src/wp-includes/class-wp-customize-manager.php
Expand Up @@ -5098,10 +5098,10 @@ public function register_controls() {
'label' => __( 'Logo' ),
'section' => 'title_tagline',
'priority' => 8,
'height' => $custom_logo_args[0]['height'],
'width' => $custom_logo_args[0]['width'],
'flex_height' => $custom_logo_args[0]['flex-height'],
'flex_width' => $custom_logo_args[0]['flex-width'],
'height' => isset( $custom_logo_args[0]['height'] ) ? $custom_logo_args[0]['height'] : null,
'width' => isset( $custom_logo_args[0]['width'] ) ? $custom_logo_args[0]['width'] : null,
'flex_height' => isset( $custom_logo_args[0]['flex-height'] ) ? $custom_logo_args[0]['flex-height'] : null,
'flex_width' => isset( $custom_logo_args[0]['flex-width'] ) ? $custom_logo_args[0]['flex-width'] : null,
'button_labels' => array(
'select' => __( 'Select logo' ),
'change' => __( 'Change logo' ),
Expand Down
Expand Up @@ -477,8 +477,11 @@ public function preview() {
*/
public function filter_wp_get_nav_menu_items( $items, $menu, $args ) {
$this_item = $this->value();
$current_nav_menu_term_id = $this_item['nav_menu_term_id'];
unset( $this_item['nav_menu_term_id'] );
$current_nav_menu_term_id = null;
if ( isset( $this_item['nav_menu_term_id'] ) ) {
$current_nav_menu_term_id = $this_item['nav_menu_term_id'];
unset( $this_item['nav_menu_term_id'] );
}

$should_filter = (
$menu->term_id === $this->original_nav_menu_term_id
Expand All @@ -493,7 +496,7 @@ public function filter_wp_get_nav_menu_items( $items, $menu, $args ) {
$should_remove = (
false === $this_item
||
true === $this_item['_invalid']
( isset( $this_item['_invalid'] ) && true === $this_item['_invalid'] )
||
(
$this->original_nav_menu_term_id === $menu->term_id
Expand Down
6 changes: 5 additions & 1 deletion src/wp-includes/meta.php
Expand Up @@ -523,7 +523,11 @@ function get_metadata( $meta_type, $object_id, $meta_key = '', $single = false )

if ( ! $meta_cache ) {
$meta_cache = update_meta_cache( $meta_type, array( $object_id ) );
$meta_cache = $meta_cache[ $object_id ];
if ( isset( $meta_cache[ $object_id ] ) ) {
$meta_cache = $meta_cache[ $object_id ];
} else {
$meta_cache = null;
}
}

if ( ! $meta_key ) {
Expand Down
6 changes: 4 additions & 2 deletions src/wp-includes/rest-api/class-wp-rest-request.php
Expand Up @@ -294,7 +294,9 @@ public function set_headers( $headers, $override = true ) {
*
* @since 4.4.0
*
* @return array Map containing 'value' and 'parameters' keys.
* @return array|null Map containing 'value' and 'parameters' keys
* or null when no valid content-type header was
* available.
*/
public function get_content_type() {
$value = $this->get_header( 'content-type' );
Expand Down Expand Up @@ -334,7 +336,7 @@ protected function get_parameter_order() {
$order = array();

$content_type = $this->get_content_type();
if ( $content_type['value'] === 'application/json' ) {
if ( isset( $content_type['value'] ) && 'application/json' === $content_type['value'] ) {
$order[] = 'JSON';
}

Expand Down
12 changes: 6 additions & 6 deletions src/wp-includes/theme.php
Expand Up @@ -2371,14 +2371,14 @@ function add_theme_support( $feature, ...$args ) {
* Merge post types with any that already declared their support
* for post thumbnails.
*/
if ( is_array( $args[0] ) && isset( $_wp_theme_features['post-thumbnails'] ) ) {
if ( isset( $args[0] ) && is_array( $args[0] ) && isset( $_wp_theme_features['post-thumbnails'] ) ) {
$args[0] = array_unique( array_merge( $_wp_theme_features['post-thumbnails'][0], $args[0] ) );
}

break;

case 'post-formats':
if ( is_array( $args[0] ) ) {
if ( isset( $args[0] ) && is_array( $args[0] ) ) {
$post_formats = get_post_format_slugs();
unset( $post_formats['standard'] );

Expand All @@ -2391,7 +2391,7 @@ function add_theme_support( $feature, ...$args ) {
if ( empty( $args[0] ) ) {
// Build an array of types for back-compat.
$args = array( 0 => array( 'comment-list', 'comment-form', 'search-form' ) );
} elseif ( ! is_array( $args[0] ) ) {
} elseif ( ! isset( $args[0] ) || ! is_array( $args[0] ) ) {
_doing_it_wrong( "add_theme_support( 'html5' )", __( 'You need to pass an array of types.' ), '3.6.1' );
return false;
}
Expand All @@ -2403,7 +2403,7 @@ function add_theme_support( $feature, ...$args ) {
break;

case 'custom-logo':
if ( ! is_array( $args ) ) {
if ( true === $args ) {
$args = array( 0 => array() );
}
$defaults = array(
Expand All @@ -2426,7 +2426,7 @@ function add_theme_support( $feature, ...$args ) {
return add_theme_support( 'custom-header', array( 'uploads' => true ) );

case 'custom-header':
if ( ! is_array( $args ) ) {
if ( true === $args ) {
$args = array( 0 => array() );
}

Expand Down Expand Up @@ -2516,7 +2516,7 @@ function add_theme_support( $feature, ...$args ) {
break;

case 'custom-background':
if ( ! is_array( $args ) ) {
if ( true === $args ) {
$args = array( 0 => array() );
}

Expand Down
5 changes: 4 additions & 1 deletion src/wp-includes/user.php
Expand Up @@ -2088,7 +2088,10 @@ function wp_update_user( $userdata ) {
$logged_in_cookie = wp_parse_auth_cookie( '', 'logged_in' );
/** This filter is documented in wp-includes/pluggable.php */
$default_cookie_life = apply_filters( 'auth_cookie_expiration', ( 2 * DAY_IN_SECONDS ), $ID, false );
$remember = ( ( $logged_in_cookie['expiration'] - time() ) > $default_cookie_life );
$remember = false;
if ( false !== $logged_in_cookie && ( $logged_in_cookie['expiration'] - time() ) > $default_cookie_life ) {
$remember = true;
}

wp_set_auth_cookie( $ID, $remember );
}
Expand Down
2 changes: 1 addition & 1 deletion src/wp-includes/wp-db.php
Expand Up @@ -3113,7 +3113,7 @@ protected function strip_invalid_text( $data ) {
foreach ( $data as $col => $value ) {
if ( ! empty( $value['db'] ) ) {
// We're going to need to truncate by characters or bytes, depending on the length value we have.
if ( 'byte' === $value['length']['type'] ) {
if ( isset( $value['length']['type'] ) && 'byte' === $value['length']['type'] ) {
// Using binary causes LEFT() to truncate by bytes.
$charset = 'binary';
} else {
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/tests/query/generatePostdata.php
Expand Up @@ -24,7 +24,7 @@ public function test_setup_by_fake_post() {
$data = generate_postdata( $fake->ID );

// Fails because there's no post with this ID.
$this->assertNotSame( $fake->ID, $data['id'] );
$this->assertFalse( $data );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/tests/term/termExists.php
Expand Up @@ -150,7 +150,7 @@ public function test_term_exists_taxonomy_nonempty_parent_0_should_return_false_

_unregister_taxonomy( 'foo' );

$this->assertSame( null, $found['term_id'] );
$this->assertSame( null, $found );
}

public function test_term_exists_taxonomy_nonempty_parent_nonempty_match_name() {
Expand Down

0 comments on commit 2da7f9f

Please sign in to comment.