Skip to content

Commit b67aa85

Browse files
committed
Code Quality: Resolve the empty.variable PHPStan errors.
In `WP_Query::parse_search()` the search clause can never be empty after its loop, since at least one search term is always populated before the loop is reached, and in `WP_Query::get_posts()` every queried post type appends a clause to `$status_type_clauses`, so the checks guarding both are removed. In `wp_unique_term_slug()` the loop over parent terms is now a `do`/`while`, because the parent is already known to be non-empty before the first iteration, and the separate check for the end of the ancestry duplicated the loop condition. Behavior is unchanged in all three cases. Removing these empties the baseline, so it is removed. The term object passed to `wp_unique_term_slug()` is also documented with an object shape, which requires the `taxonomy` property that PHPStan otherwise reports as undefined on a plain `object`. Developed in #16242. Follow-up to r51276, r63019. Props sunasty, westonruter, mukesh27. See #66739. git-svn-id: https://develop.svn.wordpress.org/trunk@65789 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 0f1f20a commit b67aa85

3 files changed

Lines changed: 8 additions & 48 deletions

File tree

‎src/wp-includes/class-wp-query.php‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,11 +1554,9 @@ protected function parse_search( &$query_vars ) {
15541554
$searchand = ' AND ';
15551555
}
15561556

1557-
if ( ! empty( $search ) ) {
1558-
$search = " AND ({$search}) ";
1559-
if ( ! is_user_logged_in() ) {
1560-
$search .= " AND ({$wpdb->posts}.post_password = '') ";
1561-
}
1557+
$search = " AND ({$search}) ";
1558+
if ( ! is_user_logged_in() ) {
1559+
$search .= " AND ({$wpdb->posts}.post_password = '') ";
15621560
}
15631561

15641562
return $search;
@@ -2784,9 +2782,7 @@ public function get_posts() {
27842782
$status_type_clauses[] = $type_where;
27852783
}
27862784

2787-
if ( ! empty( $status_type_clauses ) ) {
2788-
$where .= ' AND (' . implode( ' OR ', $status_type_clauses ) . ')';
2789-
}
2785+
$where .= ' AND (' . implode( ' OR ', $status_type_clauses ) . ')';
27902786
} else {
27912787
$where .= ' AND 1=0 ';
27922788
}

‎src/wp-includes/taxonomy.php‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3271,6 +3271,8 @@ function wp_remove_object_terms( $object_id, $terms, $taxonomy ) {
32713271
* @param string $slug The string that will be tried for a unique slug.
32723272
* @param object $term The term object that the `$slug` will belong to.
32733273
* @return string Will return a true unique slug.
3274+
*
3275+
* @phpstan-param object{ taxonomy: string, parent?: int, term_id?: int } $term
32743276
*/
32753277
function wp_unique_term_slug( $slug, $term ) {
32763278
global $wpdb;
@@ -3290,7 +3292,7 @@ function wp_unique_term_slug( $slug, $term ) {
32903292
$parent_suffix = '';
32913293
if ( $needs_suffix && is_taxonomy_hierarchical( $term->taxonomy ) && ! empty( $term->parent ) ) {
32923294
$the_parent = $term->parent;
3293-
while ( ! empty( $the_parent ) ) {
3295+
do {
32943296
$parent_term = get_term( $the_parent, $term->taxonomy );
32953297
if ( is_wp_error( $parent_term ) || empty( $parent_term ) ) {
32963298
break;
@@ -3300,11 +3302,8 @@ function wp_unique_term_slug( $slug, $term ) {
33003302
break;
33013303
}
33023304

3303-
if ( empty( $parent_term->parent ) ) {
3304-
break;
3305-
}
33063305
$the_parent = $parent_term->parent;
3307-
}
3306+
} while ( ! empty( $the_parent ) );
33083307
}
33093308

33103309
// If we didn't get a unique slug, try appending a number to make it unique.

‎tests/phpstan/baselines/empty.variable.neon‎

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)