Skip to content

Block Library: Return values matching the documented types instead of empty returns - #86091

Merged
westonruter merged 1 commit into
WordPress:trunkfrom
huzaifaalmesbah:fix/phpstan-block-library-empty-returns
Nov 1, 2026
Merged

westonruter merged 1 commit into
WordPress:trunkfrom
huzaifaalmesbah:fix/phpstan-block-library-empty-returns

Conversation

@huzaifaalmesbah

@huzaifaalmesbah huzaifaalmesbah commented Nov 1, 2026 •

Copy link
Copy Markdown
Member

What?

See #86088

Replaces bare return; statements in block render callbacks and helper functions with return values that match their documented return types, and removes the resolved entries from the PHPStan baseline.

Why?

PHPStan reports return.empty when a function documented to return string or array uses a bare return;, which actually returns null. Together, these changes clear 21 of the 226 errors initially baselined in #66693 (all 20 return.empty errors plus one related return.type error).

How?

  • Render callbacks that return early now return '': Comment Reply Link, Comment Template, Comments Pagination, Comments Title, Icon, Page List, Post Comments Form, and Site Tagline. WP_Block::render() casts the callback result to a string, so the rendered output is unchanged.
  • block_core_page_list_render_nested_page_list() returns '', and block_core_page_list_nest_pages() returns array(). Callers only concatenate the markup or pass the pages on to the renderer, which already returns early for empty input.
  • block_core_navigation_get_menu_items_at_location() returns array(). Both callers check the result with empty(), so they behave the same.
  • The deprecated block_core_navigation_maybe_use_classic_menu_fallback() actually returns the result of block_core_navigation_get_most_recently_published_navigation(), so its @return is corrected to WP_Post|null and the early returns are now an explicit return null;.
  • Removed the 21 corresponding entries from tools/phpstan/baseline.neon.

Testing Instructions

  1. Run composer phpstan and confirm it passes with no unmatched ignored errors.
  2. Run the PHP unit tests for the affected blocks, e.g. npm run test:php -- --filter "Tests_Blocks_Render|page_list|navigation|comment".
  3. Optional smoke test on the front end:
    • Add a Page List block on a site with no published pages and confirm nothing is rendered.
    • Add a Comments block to a post with comments closed and no comments and confirm the Comments Title, Comments Pagination, and Comment Template render nothing, as before.
    • Add a Site Tagline block with an empty tagline and confirm nothing is rendered.

Testing Instructions for Keyboard

Not applicable. There are no UI changes.

Screenshots or screencast

Not applicable.

Use of AI Tools

I used Claude Code to identify the affected occurrences and draft the change. I reviewed the diff and take responsibility for it.

… empty returns

Several block render callbacks and helper functions used a bare `return;`, returning `null` although their documented return type is `string` or `array`. Return an empty string or an empty array instead, and document that the deprecated `block_core_navigation_maybe_use_classic_menu_fallback()` returns `WP_Post|null`. Remove the 21 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>

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.

@huzaifaalmesbah
huzaifaalmesbah marked this pull request as ready for review November 1, 2026 01:42
@westonruter
westonruter merged commit a20dc7d into WordPress:trunk Nov 1, 2026
108 checks passed
@github-actions github-actions Bot added this to the Gutenberg 24.4 milestone Nov 1, 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.

2 participants