Make WordPress Core


Ignore:
Timestamp:
04/05/2020 03:00:44 AM (5 years ago)
Author:
SergeyBiryukov
Message:

Coding Standards: Use strict type check for in_array() and array_search() where strings are involved.

This reduces the number of WordPress.PHP.StrictInArray.MissingTrueStrict issues from 486 to 50.

Includes minor code layout fixes for better readability.

See #49542.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-query.php

    r47509 r47550  
    21572157
    21582158                $post_status_join = true;
    2159             } elseif ( in_array( 'attachment', (array) $post_type ) ) {
     2159            } elseif ( in_array( 'attachment', (array) $post_type, true ) ) {
    21602160                $post_status_join = true;
    21612161            }
     
    21782178                    }
    21792179
    2180                     if ( ! in_array( $queried_taxonomy, array( 'category', 'post_tag' ) ) ) {
     2180                    if ( ! in_array( $queried_taxonomy, array( 'category', 'post_tag' ), true ) ) {
    21812181                        $q['taxonomy'] = $queried_taxonomy;
    21822182
     
    24642464            $p_status = array();
    24652465            $e_status = array();
    2466             if ( in_array( 'any', $q_status ) ) {
     2466            if ( in_array( 'any', $q_status, true ) ) {
    24672467                foreach ( get_post_stati( array( 'exclude_from_search' => true ) ) as $status ) {
    2468                     if ( ! in_array( $status, $q_status ) ) {
     2468                    if ( ! in_array( $status, $q_status, true ) ) {
    24692469                        $e_status[] = "{$wpdb->posts}.post_status <> '$status'";
    24702470                    }
     
    24722472            } else {
    24732473                foreach ( get_post_stati() as $status ) {
    2474                     if ( in_array( $status, $q_status ) ) {
     2474                    if ( in_array( $status, $q_status, true ) ) {
    24752475                        if ( 'private' == $status ) {
    24762476                            $p_status[] = "{$wpdb->posts}.post_status = '$status'";
     
    30843084
    30853085            // If the post_status was specifically requested, let it pass through.
    3086             if ( ! in_array( $status, $q_status ) ) {
     3086            if ( ! in_array( $status, $q_status, true ) ) {
    30873087                $post_status_obj = get_post_status_object( $status );
    30883088
     
    35653565     */
    35663566    public function __get( $name ) {
    3567         if ( in_array( $name, $this->compat_fields ) ) {
     3567        if ( in_array( $name, $this->compat_fields, true ) ) {
    35683568            return $this->$name;
    35693569        }
     
    35793579     */
    35803580    public function __isset( $name ) {
    3581         if ( in_array( $name, $this->compat_fields ) ) {
     3581        if ( in_array( $name, $this->compat_fields, true ) ) {
    35823582            return isset( $this->$name );
    35833583        }
     
    35943594     */
    35953595    public function __call( $name, $arguments ) {
    3596         if ( in_array( $name, $this->compat_methods ) ) {
     3596        if ( in_array( $name, $this->compat_methods, true ) ) {
    35973597            return $this->$name( ...$arguments );
    35983598        }
     
    36333633        $post_type_object = get_post_type_object( $post_type );
    36343634
    3635         return in_array( $post_type_object->name, (array) $post_types );
     3635        return in_array( $post_type_object->name, (array) $post_types, true );
    36363636    }
    36373637
     
    36583658        $post_obj = $this->get_queried_object();
    36593659
    3660         if ( in_array( (string) $post_obj->ID, $attachment ) ) {
     3660        if ( in_array( (string) $post_obj->ID, $attachment, true ) ) {
    36613661            return true;
    3662         } elseif ( in_array( $post_obj->post_title, $attachment ) ) {
     3662        } elseif ( in_array( $post_obj->post_title, $attachment, true ) ) {
    36633663            return true;
    3664         } elseif ( in_array( $post_obj->post_name, $attachment ) ) {
     3664        } elseif ( in_array( $post_obj->post_name, $attachment, true ) ) {
    36653665            return true;
    36663666        }
     
    36933693        $author = array_map( 'strval', (array) $author );
    36943694
    3695         if ( in_array( (string) $author_obj->ID, $author ) ) {
     3695        if ( in_array( (string) $author_obj->ID, $author, true ) ) {
    36963696            return true;
    3697         } elseif ( in_array( $author_obj->nickname, $author ) ) {
     3697        } elseif ( in_array( $author_obj->nickname, $author, true ) ) {
    36983698            return true;
    3699         } elseif ( in_array( $author_obj->user_nicename, $author ) ) {
     3699        } elseif ( in_array( $author_obj->user_nicename, $author, true ) ) {
    37003700            return true;
    37013701        }
     
    37293729        $category = array_map( 'strval', (array) $category );
    37303730
    3731         if ( in_array( (string) $cat_obj->term_id, $category ) ) {
     3731        if ( in_array( (string) $cat_obj->term_id, $category, true ) ) {
    37323732            return true;
    3733         } elseif ( in_array( $cat_obj->name, $category ) ) {
     3733        } elseif ( in_array( $cat_obj->name, $category, true ) ) {
    37343734            return true;
    3735         } elseif ( in_array( $cat_obj->slug, $category ) ) {
     3735        } elseif ( in_array( $cat_obj->slug, $category, true ) ) {
    37363736            return true;
    37373737        }
     
    37653765        $tag = array_map( 'strval', (array) $tag );
    37663766
    3767         if ( in_array( (string) $tag_obj->term_id, $tag ) ) {
     3767        if ( in_array( (string) $tag_obj->term_id, $tag, true ) ) {
    37683768            return true;
    3769         } elseif ( in_array( $tag_obj->name, $tag ) ) {
     3769        } elseif ( in_array( $tag_obj->name, $tag, true ) ) {
    37703770            return true;
    3771         } elseif ( in_array( $tag_obj->slug, $tag ) ) {
     3771        } elseif ( in_array( $tag_obj->slug, $tag, true ) ) {
    37723772            return true;
    37733773        }
     
    38133813
    38143814        // Check that the taxonomy matches.
    3815         if ( ! ( isset( $queried_object->taxonomy ) && count( $tax_array ) && in_array( $queried_object->taxonomy, $tax_array ) ) ) {
     3815        if ( ! ( isset( $queried_object->taxonomy ) && count( $tax_array ) && in_array( $queried_object->taxonomy, $tax_array, true ) ) ) {
    38163816            return false;
    38173817        }
     
    38843884            $qv = get_default_feed();
    38853885        }
    3886         return in_array( $qv, (array) $feeds );
     3886        return in_array( $qv, (array) $feeds, true );
    38873887    }
    38883888
     
    40054005        $page = array_map( 'strval', (array) $page );
    40064006
    4007         if ( in_array( (string) $page_obj->ID, $page ) ) {
     4007        if ( in_array( (string) $page_obj->ID, $page, true ) ) {
    40084008            return true;
    4009         } elseif ( in_array( $page_obj->post_title, $page ) ) {
     4009        } elseif ( in_array( $page_obj->post_title, $page, true ) ) {
    40104010            return true;
    4011         } elseif ( in_array( $page_obj->post_name, $page ) ) {
     4011        } elseif ( in_array( $page_obj->post_name, $page, true ) ) {
    40124012            return true;
    40134013        } else {
     
    41124112        $post = array_map( 'strval', (array) $post );
    41134113
    4114         if ( in_array( (string) $post_obj->ID, $post ) ) {
     4114        if ( in_array( (string) $post_obj->ID, $post, true ) ) {
    41154115            return true;
    4116         } elseif ( in_array( $post_obj->post_title, $post ) ) {
     4116        } elseif ( in_array( $post_obj->post_title, $post, true ) ) {
    41174117            return true;
    4118         } elseif ( in_array( $post_obj->post_name, $post ) ) {
     4118        } elseif ( in_array( $post_obj->post_name, $post, true ) ) {
    41194119            return true;
    41204120        } else {
     
    41574157        $post_obj = $this->get_queried_object();
    41584158
    4159         return in_array( $post_obj->post_type, (array) $post_types );
     4159        return in_array( $post_obj->post_type, (array) $post_types, true );
    41604160    }
    41614161
Note: See TracChangeset for help on using the changeset viewer.