Skip to content

Block Library: Fix argument types reported by PHPStan - #86095

Merged
westonruter merged 15 commits into
WordPress:trunkfrom
huzaifaalmesbah:fix/phpstan-argument-types
Nov 5, 2026
Merged

westonruter merged 15 commits into
WordPress:trunkfrom
huzaifaalmesbah:fix/phpstan-argument-types

Conversation

@huzaifaalmesbah

@huzaifaalmesbah huzaifaalmesbah commented Nov 1, 2026 •

Copy link
Copy Markdown
Member

What?

See #86088

Fixes argument types reported by PHPStan in several blocks, mostly by correcting docblocks that did not match the code, and removes the resolved entries from the PHPStan baseline.

Why?

PHPStan reports argument.type errors when a value's documented type does not match what the receiving function expects. In most of these cases the code was right and the docblock was wrong. Fixing the docblocks also resolves related return.type and property.nonObject errors. Together, these changes clear 16 of the 226 errors initially baselined in #66693.

How?

Docblock fixes:

  • Post Time to Read: block_core_post_time_to_read_word_count() returns the result of preg_match_all(), so its @return is now int instead of string, narrowed further with @phpstan-return int<0, max>. This resolves the _n() and number_format_i18n() argument errors and two return.type errors. The $type parameter is also narrowed with @phpstan-param to the three values it is documented to accept, which is what wp_get_word_count_type() returns. The runtime fallback to 'words' is kept.
  • Gallery: block_core_gallery_render() receives a WP_Block, not an array. The function already reads $block->context and passes $block to block_core_gallery_resolve_dynamic_source(), which expects a WP_Block. This also resolves three property.nonObject errors.
  • Navigation: the deprecated block_core_navigation_get_classic_menu_fallback_blocks() returns serialize_blocks(), so its @return is now string. This resolves the wp_insert_post() argument error and a return.type error.
  • Search: apply_block_core_search_border_style() is called with null for a border applied to all sides, followed by each of the four sides, and it already handles null. So $side is now documented as string|null, and narrowed with @phpstan-param to 'top'|'right'|'bottom'|'left'|null.

Comment dates:

  • get_comment_date( 'U', ... ) was documented as returning a string, although mysql2date() returns an integer for the 'U' format, and false on failure. Core now has a conditional return type for it (int|false for 'U' and 'G', string|false otherwise), added in Comments: Add conditional return types to get_comment_date() and get_comment_time() wordpress-develop#15473 and committed in r65263. The WordPress stubs in tools/phpstan/wordpress-stubs are regenerated from core trunk at r65263 to pick it up, so no cast is needed before passing the timestamp to human_time_diff() and date_i18n().
  • Since the type now includes false, it is checked for. The Comment Date block renders nothing when the comment's date cannot be retrieved, whatever the format, and Latest Comments leaves out the comment's time element.
  • Latest Comments: $comment->user_id is a numeric string, so it is cast to int before being passed to get_author_posts_url().

Removed the 16 corresponding entries from tools/phpstan/baseline.neon.

PHPStan errors fixed

These are the errors removed from tools/phpstan/baseline.neon, which are reported at the level CI runs (level 5). Paths are relative to packages/block-library/src/.

File Identifier Error Count
comment-date/index.php argument.type Parameter #1 $from of function human_time_diff expects int, string given. 1
gallery/index.php property.nonObject Cannot access property $context on array. 3
gallery/index.php argument.type Parameter #2 $block of function block_core_gallery_resolve_dynamic_source expects WP_Block, array given. 1
latest-comments/index.php argument.type Parameter #1 $author_id of function get_author_posts_url expects int, string given. 1
latest-comments/index.php argument.type Parameter #2 $timestamp_with_offset of function date_i18n expects bool|int, string given. 1
navigation/index.php return.type Function block_core_navigation_get_classic_menu_fallback_blocks() should return array but returns string. 1
navigation/index.php argument.type Parameter #1 $postarr of function wp_insert_post expects array{…}, array{post_content: non-empty-array, …} given. 1
post-time-to-read/index.php return.type Function block_core_post_time_to_read_word_count() should return string but returns int. 1
post-time-to-read/index.php return.type Function block_core_post_time_to_read_word_count() should return string but returns int<0, max>. 1
post-time-to-read/index.php argument.type Parameter #1 $number of function number_format_i18n expects float, string given. 2
post-time-to-read/index.php argument.type Parameter #3 $number of function _n expects int, string given. 2
search/index.php argument.type Parameter #3 $side of function apply_block_core_search_border_style expects string, null given. 1
Total 16

These are the additional errors fixed at level 10:

File Identifier Error Count
gallery/index.php missingType.iterableValue Function block_core_gallery_render() has parameter $block with no value type specified in iterable type array. 1
gallery/index.php offsetAccess.nonOffsetAccessible Cannot access offset 'galleryId' on mixed. 1
navigation/index.php missingType.iterableValue Function block_core_navigation_get_classic_menu_fallback_blocks() return type has no value type specified in iterable type array. 1
post-time-to-read/index.php binaryOp.invalid Binary operation "*" between mixed and 0.8 results in an error. 1
post-time-to-read/index.php binaryOp.invalid Binary operation "*" between mixed and 1.2 results in an error. 1
post-time-to-read/index.php argument.type Parameter #1 $number of function round expects float, mixed given. 1
Total 6

Some level 10 errors on these lines remain, since they are caused by mixed values rather than by the types fixed here:

  • Post Time to Read: three binaryOp.invalid errors for $total_words / $average_reading_rate are reworded rather than fixed (string becomes int<0, max>), since $average_reading_rate comes from the block attributes and is mixed.
  • Navigation: the wp_insert_post() error is still reported at level 10, now only because post_title and post_name are mixed.

The only change in behavior is when a comment's date cannot be retrieved, such as when it is empty or a get_comment_date filter returns false. Before, the Comment Date block's "human-diff" format showed "57 years ago" (the time since the Unix epoch), its other formats rendered an empty time element, and Latest Comments showed the current date. Now the Comment Date block renders nothing and Latest Comments leaves the date out.

The Comment Date block's "human-diff" format is also off by the site's timezone offset. That bug predates this PR, so it is fixed separately in #86398.

The remaining argument.type errors are not touched here: the next_tag() errors are being discussed in #86088, and the others (Comment Author Avatar, Tag Cloud, lib/, style-engine, and wp-build) need a closer look because a fix could change behavior.

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:
    • Post Time to Read: check that the reading time, word count, and character count display as before.
    • Comment Date with the "human-diff" format and Latest Comments with dates shown: check that dates display as before, and that the comment author links to the author's posts.
    • Gallery: check that it renders as before.
  4. Optional check of a missing date: add add_filter( 'get_comment_date', '__return_false' ); to a plugin, then check that the Comment Date block renders nothing in every format, and that Latest Comments lists the comments without dates.

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.

The later changes (regenerating the stubs, the false checks, the @phpstan-param and @phpstan-return narrowing, and this description's error tables) were made with Claude Code using Claude Opus 5.5 (medium effort).

Correct docblocks that did not match the values the functions take or return: the Post Time to Read word count returns an int, the Gallery render callback receives a `WP_Block`, the deprecated Navigation classic menu fallback returns serialized blocks as a string, and the Search border style helper accepts a null side. Cast the comment timestamp and user ID to int where Comment Date and Latest Comments pass them to functions that expect an int. Remove the 15 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>

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 02:33
…-types

# Conflicts:
#	tools/phpstan/baseline.neon
…-types

# Conflicts:
#	tools/phpstan/baseline.neon
$author_url = get_comment_author_url( $comment );
if ( empty( $author_url ) && ! empty( $comment->user_id ) ) {
$author_url = get_author_posts_url( $comment->user_id );
$author_url = get_author_posts_url( (int) $comment->user_id );

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.

This makes sense, because user_id is a numeric-string.

westonruter and others added 2 commits November 4, 2026 13:05
…-types

Resolve the conflict in the PHPStan baseline by regenerating it.

Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com>
Claude-Code-Session-Id: bb6ac735-361d-4cf9-8285-f96ee1a308af
The count comes from `preg_match_all()`, so it can never be negative.

Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com>
Claude-Code-Session-Id: bb6ac735-361d-4cf9-8285-f96ee1a308af
westonruter and others added 3 commits November 4, 2026 13:51
This picks up the conditional return types on `get_comment_date()` and `get_comment_time()`, which type the 'U' and 'G' formats as `int|false`.

Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com>
Claude-Code-Session-Id: bb6ac735-361d-4cf9-8285-f96ee1a308af
With the regenerated stubs, `get_comment_date( 'U' )` is typed as `int|false`, so the casts are no longer needed to satisfy the `int` parameters of `human_time_diff()` and `date_i18n()`. The casts also turned a false result into 0, dating the comment to 1970.

The Comment Date block now renders nothing when the timestamp is false in the human-diff format, instead of passing false to `human_time_diff()` and showing a time difference from 1970. With the comment already known to exist, false means its date is missing or was filtered out, so there is no time to show.

Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com>
Claude-Code-Session-Id: bb6ac735-361d-4cf9-8285-f96ee1a308af
When `get_comment_date()` returns false, `date_i18n()` falls back to the current time, so the Latest Comments block showed today's date, and an empty `datetime` attribute, for a comment whose date is missing or was filtered out. Leave the `<time>` element out for that comment instead, as the Comment Date block now renders nothing in the same case.

Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com>
Claude-Code-Session-Id: bb6ac735-361d-4cf9-8285-f96ee1a308af

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

Two active moderate findings remain because numeric-string timestamps are passed to APIs requiring integers in the comment-date and latest-comments changes.

2 open issues
What changed in this PR

Fixes PHPStan argument and return-type findings across Block Library server-side rendering code by correcting PHPDoc annotations, adding scalar casts and timestamp validation, updating WordPress stub metadata, and removing resolved baseline entries.

Changes:

  • Corrects documented types for Search, Post Time to Read, Navigation, and Gallery.
  • Adds integer casts for comment timestamps and author IDs.
  • Validates unavailable comment timestamps before formatting.
  • Updates WordPress stubs and removes resolved PHPStan baseline entries.
  • Two moderate PHPStan issues remain in Comment Date and Latest Comments where timestamps still need integer casts.
File Summary
tools/​phpstan/​wordpress-stubs/​composer.json Updates the WordPress stubs source revision and package metadata.
tools/​phpstan/​baseline.neon Removes resolved PHPStan error suppressions.
packages/​block-library/​src/​search/​index.php Documents the Search border side parameter as nullable.
packages/​block-library/​src/​post-time-to-read/​index.php Corrects the word-count return type documentation.
packages/​block-library/​src/​navigation/​index.php Documents the serialized string return value of the classic menu fallback helper.
packages/​block-library/​src/​latest-comments/​index.php Casts author IDs and handles comment-date values; the timestamp passed to date_i18n() still requires an integer cast.
packages/​block-library/​src/​gallery/​index.php Corrects the gallery render callback parameter type to WP_Block.
packages/​block-library/​src/​comment-date/​index.php Handles unavailable comment timestamps; the timestamp passed to human_time_diff() still requires an integer cast.

Review effort: Lite

westonruter and others added 5 commits November 5, 2026 14:22
The `$type` parameter is documented to accept only 'words', 'characters_excluding_spaces', or 'characters_including_spaces', which is also what `wp_get_word_count_type()` is typed to return. The runtime fallback to 'words' is kept for callers that are not statically analyzed.

Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com>
Claude-Code-Session-Id: bb6ac735-361d-4cf9-8285-f96ee1a308af
`apply_block_core_search_border_styles()` passes null for a border applied to all sides, followed by each of 'top', 'right', 'bottom', and 'left'. The parameter cannot be omitted, since the style collections passed by reference follow it, so describe null as meaning all sides rather than calling the parameter optional.

Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com>
Claude-Code-Session-Id: bb6ac735-361d-4cf9-8285-f96ee1a308af
…able

Only the human-diff format returned an empty string when the comment's date could not be retrieved. Other formats still rendered the wrapper around an empty `time` element with an empty `datetime` attribute. The `'c'` date is now checked before the wrapper is built, and the date in the chosen format is checked too, so the block renders nothing for any format.

Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com>
Claude-Code-Session-Id: bb6ac735-361d-4cf9-8285-f96ee1a308af
The 'U' format of `get_comment_date()` comes from `mysql2date()`, which returns the timestamp plus the site's timezone offset rather than a Unix timestamp. Since `human_time_diff()` compares it with the current UTC time, a comment posted just now on a site at UTC+2 was shown as posted two hours ago. The timestamp is now read from the comment's GMT time, which yields the actual Unix timestamp.

Since `get_comment_time()` returns an empty string when the comment cannot be found, anything other than an integer causes the block to render nothing, as before.

Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com>
Claude-Code-Session-Id: bb6ac735-361d-4cf9-8285-f96ee1a308af
…set"

This reverts commit ae662d2. The timezone offset is a pre-existing bug unrelated to the PHPStan argument types, so its fix is moved to a separate pull request.

Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com>
Claude-Code-Session-Id: bb6ac735-361d-4cf9-8285-f96ee1a308af

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.

🟢 Approval recommended

The supplied review evidence identifies no active issues, with the listed PHPStan typing and date-handling fixes addressing the reported errors and removing their baseline suppressions.

No open issues 🎉

2 resolved since last review

Review effort: Lite

@westonruter westonruter added [Block] Gallery Affects the Gallery Block - used to display groups of images [Block] Latest Comments Affects the Latest Comments Block [Block] Navigation Affects the Navigation Block [Block] Search Affects the Search Block - used to display a search field [Block] Comment Date Affects the Comment Date Block [Block] Time to Read Affects the Time to Read Block labels Nov 5, 2026
@westonruter
westonruter merged commit f71f9b7 into WordPress:trunk Nov 5, 2026
75 checks passed
@github-actions github-actions Bot added this to the Gutenberg 24.5 milestone Nov 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Block] Comment Date Affects the Comment Date Block [Block] Gallery Affects the Gallery Block - used to display groups of images [Block] Latest Comments Affects the Latest Comments Block [Block] Navigation Affects the Navigation Block [Block] Search Affects the Search Block - used to display a search field [Block] Time to Read Affects the Time to Read Block [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.

3 participants