Repository navigation
Block Library: Fix small PHPStan errors in several blocks - #86101
Conversation
- Check `WP_Block_List` inner blocks with `count()` in the Navigation and Playlist blocks, since `empty()` is never true for an object. - Drop `isset()` checks on properties that are always set in the Avatar and Term Template blocks. - Initialize variables that were only defined on some paths in the Comment Author Avatar, Navigation, and Search blocks. - Return `null` explicitly, and document it, when the Navigation post is not published. - Remove the unused `shortcodes_regexp` branch from the Post Time to Read word count. Remove the 10 resolved entries from the PHPStan baseline. See WordPress#86088.
🤖 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 |
…brary-misc # Conflicts: # tools/phpstan/baseline.neon
| if ( ! $show_button && ! empty( $color_classes ) ) { | ||
| $input_classes[] = $color_classes; | ||
| } | ||
| $is_expandable_searchfield = false; |
| // If a shortcode regular expression has been provided use it to remove shortcodes. | ||
| if ( ! empty( $settings['shortcodes_regexp'] ) ) { | ||
| $text = preg_replace( $settings['shortcodes_regexp'], "\n", $text ); | ||
| } |
There was a problem hiding this comment.
This block was originally meant to count words via a new wp_word_count() function, which accepted a $settings parameter (including shortcodes). This code is a leftover from that. Since wp_word_count() was not shipped in core, #72091 moved the logic into a block-specific function without that parameter, so shortcodes_regexp is never set. This if statement can be safely removed.
…_blocks_from_navigation_post
…brary-misc # Conflicts: # packages/block-library/src/comment-author-avatar/index.php # tools/phpstan/baseline.neon
| $open_button_directives = ''; | ||
| $responsive_container_directives = ''; | ||
| $responsive_dialog_directives = ''; | ||
| $responsive_container_content_directives = ''; |
There was a problem hiding this comment.
🟡 Changes recommended
A critical navigation regression remains unresolved because returning an empty WP_Block_List prevents unpublished navigation posts from using the existing fallback menu.
1 open issue
What changed in this PR
This pull request fixes several baselined PHPStan issues in the block library while removing the corresponding baseline suppressions.
Changes:
- Replaces ineffective
empty()checks onWP_Block_Listobjects withcount()checks. - Removes redundant property checks and unreachable shortcode-processing logic.
- Initializes conditionally assigned variables.
- Updates the PHPStan baseline for the resolved findings.
- Requires the unpublished Navigation post path to return
nullrather than an emptyWP_Block_List, preserving fallback rendering.
Review outcome: This pull request requires changes.
| File | Summary |
|---|---|
tools/phpstan/baseline.neon |
Removes baseline entries for resolved PHPStan errors. |
packages/block-library/src/term-template/index.php |
Removes the redundant context-property check. |
packages/block-library/src/search/index.php |
Initializes the expandable-searchfield flag. |
packages/block-library/src/post-time-to-read/index.php |
Removes unreachable shortcode-processing logic. |
packages/block-library/src/playlist/index.php |
Uses count() to check playlist inner blocks. |
packages/block-library/src/navigation/index.php |
Updates block-list checks and directive initialization; the unpublished-post path must return null to preserve fallback rendering. |
packages/block-library/src/avatar/index.php |
Removes the redundant comment author URL check. |
Review effort: Lite
…et_inner_blocks_from_navigation_post" This reverts commit c7185da.
…ix/phpstan-block-library-misc



What?
See #86088
Fixes several small PHPStan errors in the block library, and removes the resolved entries from the PHPStan baseline.
Why?
Each of these is a check that PHPStan cannot verify, a variable that is only defined on some code paths, or dead code. This resolves 9 of the 226 errors initially baselined in #66693.
How?
empty()onWP_Block::$inner_blocks(empty.property×2)block_core_navigation_block_tree_has_block_type()) and Playlist:WP_Block::$inner_blocksis an empty array by default and aWP_Block_Listwhen the block has inner blocks.empty()on it is correct for the array but always false for the object, and PHPStan flags it. These now usecount( $block->inner_blocks ) > 0, which works for both, sinceWP_Block_ListimplementsCountable. The result is unchanged, since iterating an empty list did nothing anyway.isset()on properties that are always set (isset.property×2)isset( $comment->comment_author_url ); the'' !== $comment->comment_author_urlcheck that follows still applies.! isset( $block->context ), sinceWP_Block::$contextis always an array;empty( $block->context['termQuery'] )still applies.Variables only defined on some paths (
variable.undefined×2)$is_expandable_searchfieldis initialized tofalse, for when the input tag is not found.$responsive_container_content_directivesis initialized to''along with the other directive strings, for when the block is not interactive.Navigation post that is not published (
return.missing)WP_Navigation_Block_Renderer::get_inner_blocks_from_navigation_post()fell off the end without a return value when the post was not published. It now returnsnullexplicitly, and its@returnisWP_Block_List|null. Returning an emptyWP_Block_Listinstead would change behavior, since the caller relies onempty( $inner_blocks )to render the fallback.Dead code (
offsetAccess.notFound,empty.offset)shortcodes_regexpbranch fromblock_core_post_time_to_read_word_count(). The$settingsarray is defined locally in the function and never has that key, so the branch could not run.Removed the 9 corresponding entries from
tools/phpstan/baseline.neon.There is no change in rendered output.
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 identify the affected occurrences and draft the change. I reviewed the diff and take responsibility for it.