Skip to content

Commit 15e95f3

Browse files
Menus: Simplify redundant conditions in wp-includes/nav-menu.php
Simplifies redundant conditional logic: 1. In `wp_update_nav_menu_object()`: The preceding `if ( ! $_menu || is_wp_error( $_menu ) )` block handles non-existing menu creation and returns, so $_menu is guaranteed truthy. Dropped the redundant `! $_menu` check before `isset( $_menu->term_id )`. 2. In `wp_update_nav_menu_item()`: In the `elseif ( 0 === (int) $args['menu-item-position'] )` block, `$menu_id` is guaranteed non-zero because the preceding `if ( 0 === $menu_id )` block already handled `$menu_id === 0`. Removed the redundant `if ( 0 !== $menu_id )` check. 3. In `wp_map_nav_menu_locations()`: $slug is an alphanumeric theme location key string; comparing non-string types with $slug is strictly always true. Simplified `is_numeric(...) && ... !== $slug` to `! is_string(...)`. This resolves all `notIdentical.alwaysTrue` and `booleanNot.alwaysFalse` occurrences in `wp-includes/nav-menu.php`. Developed in WordPress#14931. Props huzaifaalmesbah. See #65817. git-svn-id: https://develop.svn.wordpress.org/trunk@65159 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 392df19 commit 15e95f3

4 files changed

Lines changed: 4 additions & 44 deletions

File tree

‎phpstan.neon.dist‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ includes:
2828
- tests/phpstan/baselines/binaryOp.invalid.neon
2929
- tests/phpstan/baselines/booleanAnd.alwaysFalse.neon
3030
- tests/phpstan/baselines/booleanAnd.leftAlwaysTrue.neon
31-
- tests/phpstan/baselines/booleanNot.alwaysFalse.neon
3231
- tests/phpstan/baselines/booleanNot.alwaysTrue.neon
3332
- tests/phpstan/baselines/class.notFound.neon
3433
- tests/phpstan/baselines/deadCode.unreachable.neon

‎src/wp-includes/nav-menu.php‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ function wp_update_nav_menu_object( $menu_id = 0, $menu_data = array() ) {
374374
return (int) $_menu['term_id'];
375375
}
376376

377-
if ( ! $_menu || ! isset( $_menu->term_id ) ) {
377+
if ( ! isset( $_menu->term_id ) ) {
378378
return 0;
379379
}
380380

@@ -458,11 +458,7 @@ function wp_update_nav_menu_item( $menu_id = 0, $menu_item_db_id = 0, $menu_item
458458
if ( 0 === $menu_id ) {
459459
$args['menu-item-position'] = 1;
460460
} elseif ( 0 === (int) $args['menu-item-position'] ) {
461-
$menu_items = array();
462-
463-
if ( 0 !== $menu_id ) {
464-
$menu_items = (array) wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) );
465-
}
461+
$menu_items = (array) wp_get_nav_menu_items( $menu_id, array( 'post_status' => 'publish,draft' ) );
466462

467463
$last_item = array_pop( $menu_items );
468464

@@ -1281,7 +1277,7 @@ function wp_map_nav_menu_locations( $new_nav_menu_locations, $old_nav_menu_locat
12811277
// ...actually match!
12821278
if ( is_string( $new_location ) && false === stripos( $new_location, $slug ) && false === stripos( $slug, $new_location ) ) {
12831279
continue;
1284-
} elseif ( is_numeric( $new_location ) && $new_location !== $slug ) {
1280+
} elseif ( ! is_string( $new_location ) ) {
12851281
continue;
12861282
}
12871283

@@ -1294,7 +1290,7 @@ function wp_map_nav_menu_locations( $new_nav_menu_locations, $old_nav_menu_locat
12941290
// ... have a match as well.
12951291
if ( is_string( $location ) && false === stripos( $location, $slug ) && false === stripos( $slug, $location ) ) {
12961292
continue;
1297-
} elseif ( is_numeric( $location ) && $location !== $slug ) {
1293+
} elseif ( ! is_string( $location ) ) {
12981294
continue;
12991295
}
13001296

‎tests/phpstan/baselines/booleanNot.alwaysFalse.neon‎

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

‎tests/phpstan/baselines/notIdentical.alwaysTrue.neon‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,6 @@ parameters:
2323
identifier: notIdentical.alwaysTrue
2424
count: 1
2525
path: ../../../src/wp-includes/functions.php
26-
-
27-
message: '#^Strict comparison using \!\=\= between 0 and int\<min, \-1\>\|int\<1, max\> will always evaluate to true\.$#'
28-
identifier: notIdentical.alwaysTrue
29-
count: 1
30-
path: ../../../src/wp-includes/nav-menu.php
31-
-
32-
message: '#^Strict comparison using \!\=\= between float\|int\|numeric\-string and ''bottom''\|''footer''\|''header''\|''main''\|''menu\-1''\|''menu\-2''\|''navigation''\|''primary''\|''secondary''\|''social''\|''subsidiary''\|''top'' will always evaluate to true\.$#'
33-
identifier: notIdentical.alwaysTrue
34-
count: 2
35-
path: ../../../src/wp-includes/nav-menu.php
3626
-
3727
message: '#^Strict comparison using \!\=\= between false and int will always evaluate to true\.$#'
3828
identifier: notIdentical.alwaysTrue

0 commit comments

Comments
 (0)