Skip to content

Block Library: Fix small PHPStan errors in several blocks - #86101

Merged
westonruter merged 6 commits into
WordPress:trunkfrom
huzaifaalmesbah:fix/phpstan-block-library-misc
Nov 6, 2026
Merged

westonruter merged 6 commits into
WordPress:trunkfrom
huzaifaalmesbah:fix/phpstan-block-library-misc

Conversation

@huzaifaalmesbah

@huzaifaalmesbah huzaifaalmesbah commented Nov 1, 2026 •

Copy link
Copy Markdown
Member

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() on WP_Block::$inner_blocks (empty.property ×2)

  • Navigation (block_core_navigation_block_tree_has_block_type()) and Playlist: WP_Block::$inner_blocks is an empty array by default and a WP_Block_List when 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 use count( $block->inner_blocks ) > 0, which works for both, since WP_Block_List implements Countable. The result is unchanged, since iterating an empty list did nothing anyway.

isset() on properties that are always set (isset.property ×2)

  • Avatar: dropped isset( $comment->comment_author_url ); the '' !== $comment->comment_author_url check that follows still applies.
  • Term Template: dropped ! isset( $block->context ), since WP_Block::$context is always an array; empty( $block->context['termQuery'] ) still applies.

Variables only defined on some paths (variable.undefined ×2)

  • Search: $is_expandable_searchfield is initialized to false, for when the input tag is not found.
  • Navigation: $responsive_container_content_directives is 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 returns null explicitly, and its @return is WP_Block_List|null. Returning an empty WP_Block_List instead would change behavior, since the caller relies on empty( $inner_blocks ) to render the fallback.

Dead code (offsetAccess.notFound, empty.offset)

  • Post Time to Read: removed the shortcodes_regexp branch from block_core_post_time_to_read_word_count(). The $settings array 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

  1. Run composer phpstan and confirm it passes with no unmatched ignored errors.
  2. Run composer phpstan:baselines and confirm tools/phpstan/baseline.neon has no further changes.
  3. Optional smoke test on the front end:
    • Navigation: a menu with submenus, and a Navigation block whose menu is a draft (the fallback should render as before).
    • Search with the "button only" position, and the Avatar, Playlist, Term Template, and Post Time to Read blocks render as before.

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.

- 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.
@huzaifaalmesbah huzaifaalmesbah added the [Type] Code Quality Issues or PRs that relate to code quality label Nov 1, 2026
@github-actions github-actions Bot added the [Package] Block library /packages/block-library label Nov 1, 2026
@github-actions

github-actions Bot commented Nov 1, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

Preview in WordPress Playground

🎉 Props

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: huzaifaalmesbah <huzaifaalmesbah@git.wordpress.org>
Co-authored-by: westonruter <westonruter@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

Updated as activity occurs, without notifying anyone named here. Add the props-bot label to refresh.

…brary-misc

# Conflicts:
#	tools/phpstan/baseline.neon
@huzaifaalmesbah
huzaifaalmesbah marked this pull request as ready for review November 1, 2026 03:38
if ( ! $show_button && ! empty( $color_classes ) ) {
$input_classes[] = $color_classes;
}
$is_expandable_searchfield = false;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PhpStorm also identified the need for this:

Image

Comment on lines -61 to -64
// 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 );
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious about the history of this if statement. It seems it was added in #72091 but not used? Should it have been added to the $settings array? cc @t-hamano

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

…brary-misc

# Conflicts:
#	packages/block-library/src/comment-author-avatar/index.php
#	tools/phpstan/baseline.neon
@manzoorwanijk
manzoorwanijk removed their request for review November 6, 2026 01:37
$open_button_directives = '';
$responsive_container_directives = '';
$responsive_dialog_directives = '';
$responsive_container_content_directives = '';

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good. PhpStorm's static analysis is also fixed by this. It was reporting:

Image

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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 on WP_Block_List objects with count() 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 null rather than an empty WP_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

@westonruter
westonruter merged commit b6de3f8 into WordPress:trunk Nov 6, 2026
79 checks passed
@github-actions github-actions Bot added this to the Gutenberg 24.5 milestone Nov 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] Block library /packages/block-library [Type] Code Quality Issues or PRs that relate to code quality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants