Repository navigation
Block Library: Fix argument types reported by PHPStan - #86095
Conversation
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.
🤖 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 |
…locks from the PHPStan baseline
…-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 ); |
There was a problem hiding this comment.
This makes sense, because user_id is a numeric-string.
…-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
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
There was a problem hiding this comment.
🟡 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
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
There was a problem hiding this comment.
🟢 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

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.typeerrors 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 relatedreturn.typeandproperty.nonObjecterrors. Together, these changes clear 16 of the 226 errors initially baselined in #66693.How?
Docblock fixes:
block_core_post_time_to_read_word_count()returns the result ofpreg_match_all(), so its@returnis nowintinstead ofstring, narrowed further with@phpstan-return int<0, max>. This resolves the_n()andnumber_format_i18n()argument errors and tworeturn.typeerrors. The$typeparameter is also narrowed with@phpstan-paramto the three values it is documented to accept, which is whatwp_get_word_count_type()returns. The runtime fallback to'words'is kept.block_core_gallery_render()receives aWP_Block, not anarray. The function already reads$block->contextand passes$blocktoblock_core_gallery_resolve_dynamic_source(), which expects aWP_Block. This also resolves threeproperty.nonObjecterrors.block_core_navigation_get_classic_menu_fallback_blocks()returnsserialize_blocks(), so its@returnis nowstring. This resolves thewp_insert_post()argument error and areturn.typeerror.apply_block_core_search_border_style()is called withnullfor a border applied to all sides, followed by each of the four sides, and it already handlesnull. So$sideis now documented asstring|null, and narrowed with@phpstan-paramto'top'|'right'|'bottom'|'left'|null.Comment dates:
get_comment_date( 'U', ... )was documented as returning a string, althoughmysql2date()returns an integer for the'U'format, andfalseon failure. Core now has a conditional return type for it (int|falsefor'U'and'G',string|falseotherwise), added in Comments: Add conditional return types toget_comment_date()andget_comment_time()wordpress-develop#15473 and committed in r65263. The WordPress stubs intools/phpstan/wordpress-stubsare regenerated from core trunk at r65263 to pick it up, so no cast is needed before passing the timestamp tohuman_time_diff()anddate_i18n().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'stimeelement.$comment->user_idis a numeric string, so it is cast tointbefore being passed toget_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 topackages/block-library/src/.comment-date/index.phpargument.typeParameter #1 $from of function human_time_diff expects int, string given.gallery/index.phpproperty.nonObjectCannot access property $context on array.gallery/index.phpargument.typeParameter #2 $block of function block_core_gallery_resolve_dynamic_source expects WP_Block, array given.latest-comments/index.phpargument.typeParameter #1 $author_id of function get_author_posts_url expects int, string given.latest-comments/index.phpargument.typeParameter #2 $timestamp_with_offset of function date_i18n expects bool|int, string given.navigation/index.phpreturn.typeFunction block_core_navigation_get_classic_menu_fallback_blocks() should return array but returns string.navigation/index.phpargument.typeParameter #1 $postarr of function wp_insert_post expects array{…}, array{post_content: non-empty-array, …} given.post-time-to-read/index.phpreturn.typeFunction block_core_post_time_to_read_word_count() should return string but returns int.post-time-to-read/index.phpreturn.typeFunction block_core_post_time_to_read_word_count() should return string but returns int<0, max>.post-time-to-read/index.phpargument.typeParameter #1 $number of function number_format_i18n expects float, string given.post-time-to-read/index.phpargument.typeParameter #3 $number of function _n expects int, string given.search/index.phpargument.typeParameter #3 $side of function apply_block_core_search_border_style expects string, null given.These are the additional errors fixed at level 10:
gallery/index.phpmissingType.iterableValueFunction block_core_gallery_render() has parameter $block with no value type specified in iterable type array.gallery/index.phpoffsetAccess.nonOffsetAccessibleCannot access offset 'galleryId' on mixed.navigation/index.phpmissingType.iterableValueFunction block_core_navigation_get_classic_menu_fallback_blocks() return type has no value type specified in iterable type array.post-time-to-read/index.phpbinaryOp.invalidBinary operation "*" between mixed and 0.8 results in an error.post-time-to-read/index.phpbinaryOp.invalidBinary operation "*" between mixed and 1.2 results in an error.post-time-to-read/index.phpargument.typeParameter #1 $number of function round expects float, mixed given.Some level 10 errors on these lines remain, since they are caused by
mixedvalues rather than by the types fixed here:binaryOp.invaliderrors for$total_words / $average_reading_rateare reworded rather than fixed (stringbecomesint<0, max>), since$average_reading_ratecomes from the block attributes and ismixed.wp_insert_post()error is still reported at level 10, now only becausepost_titleandpost_namearemixed.The only change in behavior is when a comment's date cannot be retrieved, such as when it is empty or a
get_comment_datefilter returnsfalse. Before, the Comment Date block's "human-diff" format showed "57 years ago" (the time since the Unix epoch), its other formats rendered an emptytimeelement, 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.typeerrors are not touched here: thenext_tag()errors are being discussed in #86088, and the others (Comment Author Avatar, Tag Cloud,lib/,style-engine, andwp-build) need a closer look because a fix could change behavior.Testing Instructions
composer phpstanand confirm it passes with no unmatched ignored errors.composer phpstan:baselinesand confirmtools/phpstan/baseline.neonhas no further changes.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
falsechecks, the@phpstan-paramand@phpstan-returnnarrowing, and this description's error tables) were made with Claude Code using Claude Opus 5.5 (medium effort).