Skip to content

Commit e5cb560

Browse files
committed
Code Quality: Fix the booleanAnd.alwaysFalse PHPStan error.
In `themes.php`, PHPStan only knew `$parent_file` as the `'themes.php'` literal assigned earlier in the file, so it reported the `empty( $parent_file )` clause of the Appearance submenu condition as never true. That is a false positive: `wp-admin/menu-header.php`, loaded via `admin-header.php`, passes `$parent_file` through the `parent_file` filter, and it also sets `$self`, while `$submenu` comes from `wp-admin/menu.php`. None of these are visible to PHPStan across the `require` chain. The condition is therefore left unchanged, and the globals are instead declared with a file-scope `global` statement and `@global` tags. An inline `@var` would not suffice, since with `treatPhpDocTypesAsCertain` disabled the always-true and always-false rules use the native type, which only the `global` statement resets. This retires the `booleanAnd.alwaysFalse` baseline along with the remaining baseline entries for `themes.php`, and the `$submenu` array shape also resolves the `mixed` errors in the submenu loop at higher rule levels. Developed in WordPress#15478. Follow-up to r63019, r63023. Props bgturner, westonruter. See #65817. git-svn-id: https://develop.svn.wordpress.org/trunk@65282 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 7032470 commit e5cb560

6 files changed

Lines changed: 7 additions & 46 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/argument.unresolvableType.neon
2929
- tests/phpstan/baselines/assign.propertyType.neon
3030
- tests/phpstan/baselines/binaryOp.invalid.neon
31-
- tests/phpstan/baselines/booleanAnd.alwaysFalse.neon
3231
- tests/phpstan/baselines/booleanAnd.leftAlwaysTrue.neon
3332
- tests/phpstan/baselines/booleanNot.alwaysTrue.neon
3433
- tests/phpstan/baselines/class.notFound.neon

‎src/wp-admin/themes.php‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,13 @@
248248
wp_enqueue_script( 'updates' );
249249

250250
require_once ABSPATH . 'wp-admin/admin-header.php';
251+
252+
/**
253+
* @global string $self The current admin page, set in wp-admin/menu-header.php.
254+
* @global string $parent_file The parent menu file, filtered in wp-admin/menu-header.php.
255+
* @global array<string, array<int, array{ 0: string, 1: string, 2: string, 3?: string, 4?: string }>> $submenu The admin submenu items keyed by parent slug, set in wp-admin/menu.php.
256+
*/
257+
global $self, $parent_file, $submenu;
251258
?>
252259

253260
<div class="wrap">

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

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

‎tests/phpstan/baselines/booleanAnd.leftAlwaysTrue.neon‎

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@
1818

1919
parameters:
2020
ignoreErrors:
21-
-
22-
message: '#^Left side of && is always true\.$#'
23-
identifier: booleanAnd.leftAlwaysTrue
24-
count: 1
25-
path: ../../../src/wp-admin/themes.php
2621
-
2722
message: '#^Left side of && is always true\.$#'
2823
identifier: booleanAnd.leftAlwaysTrue

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

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@
1818

1919
parameters:
2020
ignoreErrors:
21-
-
22-
message: '#^Variable \$parent_file in empty\(\) always exists and is not falsy\.$#'
23-
identifier: empty.variable
24-
count: 1
25-
path: ../../../src/wp-admin/themes.php
2621
-
2722
message: '#^Variable \$search in empty\(\) always exists and is not falsy\.$#'
2823
identifier: empty.variable

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -398,16 +398,6 @@ parameters:
398398
identifier: variable.undefined
399399
count: 1
400400
path: ../../../src/wp-admin/theme-install.php
401-
-
402-
message: '#^Variable \$self might not be defined\.$#'
403-
identifier: variable.undefined
404-
count: 1
405-
path: ../../../src/wp-admin/themes.php
406-
-
407-
message: '#^Variable \$submenu might not be defined\.$#'
408-
identifier: variable.undefined
409-
count: 1
410-
path: ../../../src/wp-admin/themes.php
411401
-
412402
message: '#^Variable \$wp_version might not be defined\.$#'
413403
identifier: variable.undefined

0 commit comments

Comments
 (0)