Repository navigation
Navigation: Resolve PHPStan baseline errors in the block nav menus theme opt-in - #86469
Conversation
…cuiting The `block-nav-menus` theme opt-in was disabled by replacing the `current_theme_supports()` checks with `if ( true )` and `if ( false )`, which PHPStan reports as always-true and always-false conditions, unreachable code, and a string return type that is never used. Restore the real `current_theme_supports()` check in `gutenberg_output_block_nav_menu()` and comment out its `pre_wp_nav_menu` hook instead, so the function is kept intact but never runs. In `gutenberg_remove_block_nav_menu_items()`, drop the dead early return and keep the intended opt-in check in a comment. Behavior is unchanged: block menu items are still always removed, and `wp_nav_menu()` still renders classic menus. Remove the four baseline entries that are now resolved. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: 9bb536c7-7399-4262-80ed-cebfc277b43b
The `type` and `content` properties on nav menu items are not declared on `WP_Post`; they are added at runtime by `wp_setup_nav_menu_item()`, which always sets `type` but sets `content` only for block menu items. Document them as object shape properties on the `WP_Post` parameters, with `content` optional and read with a `''` fallback. `type` is required in the shape rather than optional: an object shape whose properties are all optional is a supertype of `WP_Post`, so PHPStan reduces the intersection to plain `WP_Post` and the shape has no effect. Also document the `before` and `after` strings of the `wp_nav_menu()` arguments object. Remove the last two baseline entries for this file. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> Claude-Code-Session-Id: 9bb536c7-7399-4262-80ed-cebfc277b43b
🤖 PR meta 🤖🎉 PropsIf you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
Updated as activity occurs, without notifying anyone named here. Add the ⚡ PerformanceShow the resultsClient side metrics exclude the server response time. front-end-block-theme
front-end-classic-theme
media-processing
media-upload
post-editor
site-editor
🏁 Flaky testsSome tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information. Navigates the items list via UP/DOWN arrow keys in
|
There was a problem hiding this comment.
🟢 Approval recommended
The six identified PHPStan baseline issues were addressed with safer annotations and fallbacks while preserving the existing navigation behavior.
No open issues 🎉
What changed in this PR
This pull request resolves six PHPStan baseline errors in the navigation theme opt-in code while preserving the currently disabled block-nav-menus behavior.
Changes:
- Restores the theme-support condition and disables the inactive navigation rendering hook.
- Removes unreachable conditional code.
- Adds PHPStan object-shape annotations and safe fallbacks for dynamic menu-item properties.
- Removes the six resolved entries from the PHPStan baseline.
| File | Description |
|---|---|
tools/phpstan/baseline.neon |
Removes the resolved PHPStan baseline entries for the navigation theme opt-in. |
lib/experimental/navigation-theme-opt-in.php |
Updates navigation menu shims, removes dead code, and adds PHPStan annotations for dynamic menu properties. |
Review effort: Lite
huzaifaalmesbah
left a comment
There was a problem hiding this comment.
Looks good, thanks! Restoring the real theme support check and commenting out the hook keeps the behavior the same, and the content? shape with the ?? '' fallback is a nice touch.
…eme opt-in (WordPress#86469) * Navigation: Disable block nav menu output by unhooking, not short-circuiting The `block-nav-menus` theme opt-in was disabled by replacing the `current_theme_supports()` checks with `if ( true )` and `if ( false )`, which PHPStan reports as always-true and always-false conditions, unreachable code, and a string return type that is never used. Restore the real `current_theme_supports()` check in `gutenberg_output_block_nav_menu()` and comment out its `pre_wp_nav_menu` hook instead, so the function is kept intact but never runs. In `gutenberg_remove_block_nav_menu_items()`, drop the dead early return and keep the intended opt-in check in a comment. Behavior is unchanged: block menu items are still always removed, and `wp_nav_menu()` still renders classic menus. Remove the four baseline entries that are now resolved. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> * Navigation: Type the dynamic nav menu item properties The `type` and `content` properties on nav menu items are not declared on `WP_Post`; they are added at runtime by `wp_setup_nav_menu_item()`, which always sets `type` but sets `content` only for block menu items. Document them as object shape properties on the `WP_Post` parameters, with `content` optional and read with a `''` fallback. `type` is required in the shape rather than optional: an object shape whose properties are all optional is a supertype of `WP_Post`, so PHPStan reduces the intersection to plain `WP_Post` and the shape has no effect. Also document the `before` and `after` strings of the `wp_nav_menu()` arguments object. Remove the last two baseline entries for this file. Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com> --------- Co-authored-by: westonruter <westonruter@git.wordpress.org> Co-authored-by: huzaifaalmesbah <huzaifaalmesbah@git.wordpress.org> Co-authored-by: ramonjd <ramonopoly@git.wordpress.org>
What?
See #86088
Resolves all of the PHPStan baseline entries for
lib/experimental/navigation-theme-opt-in.phpby removing dead code and typing the dynamic nav menu item properties. Only one entry remains in the baseline, forlib/class-wp-rest-global-styles-controller-gutenberg.php.Why?
The
block-nav-menustheme opt-in was disabled in #34444 (see #34265) by replacing thecurrent_theme_supports()checks withif ( true )andif ( false ). PHPStan reports these as always-true and always-false conditions, unreachable code, and astringreturn type that is never used. The remaining errors were accesses toWP_Post::$typeandWP_Post::$content, which are not declared onWP_Postbut are added at runtime bywp_setup_nav_menu_item(). This addresses 6 of the errors initially baselined in #66693.How?
gutenberg_output_block_nav_menu()gets its real! current_theme_supports( 'block-nav-menus' )check back in place ofif ( true ), and itspre_wp_nav_menuhook is commented out instead. The function is kept intact but never runs. (if.alwaysTrue,deadCode.unreachable,return.unusedType)gutenberg_remove_block_nav_menu_items()drops theif ( false )early return. The intended opt-in check is kept in a comment for when the feature is re-enabled. (if.alwaysFalse)$itemparameters ofgutenberg_output_block_nav_menu_item()andgutenberg_output_block_menu_item_custom_fields()are typed asWP_Post&object{type: string, content?: string}.wp_setup_nav_menu_item()always setstype, butgutenberg_setup_block_nav_menu_item()setscontentonly for block menu items, socontentis optional and read with a?? ''fallback. (property.notFound)typehas to be required: an object shape whose properties are all optional is a supertype ofWP_Post, so PHPStan reduces the intersection to plainWP_Postand the shape has no effect.$argsparameter ofgutenberg_output_block_nav_menu_item()is typed asstdClass&object{before: string, after: string}, matching thewp_nav_menu()defaults.tools/phpstan/baseline.neon.There is no change in behavior: block menu items are still always removed, and
wp_nav_menu()still renders classic menus.Testing Instructions
composer phpstanand confirm it passes with no unmatched ignored errors.composer phpstan:baselinesand confirmtools/phpstan/baseline.neonhas no further changes.Testing Instructions for Keyboard
Not applicable. There are no UI changes.
Screenshots or screencast
Not applicable.
Use of AI Tools
I used Claude Code (Claude Opus 5.5, medium effort) to analyze the baselined errors, probe the PHPStan types, and draft the change and this description. I reviewed the diff and take responsibility for it.