Skip to content

Comments: Add conditional return types to get_comment_date() and get_comment_time() - #15473

Closed
westonruter wants to merge 4 commits into
WordPress:trunkfrom
westonruter:fix/comment-date-conditional-return
Closed

westonruter wants to merge 4 commits into
WordPress:trunkfrom
westonruter:fix/comment-date-conditional-return

Conversation

@westonruter

@westonruter westonruter commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

✅ Committed in r65263 (a87c255).


get_comment_date() and get_comment_time() return the result of mysql2date(), which is an integer for the 'U' and 'G' formats and false when the date is missing or cannot be parsed. Yet both were documented as returning only a string, so callers like human_time_diff( get_comment_date( 'U', $comment ) ) in the Comment Date block had to cast the result to satisfy PHPStan (see WordPress/gutenberg#86095 (comment)).

This documents the actual string|int|false return type on both functions, with a @phpstan-return conditional on $format matching the ones already on mysql2date() and the post date functions (get_the_date(), get_the_time(), get_post_time(), get_the_modified_date(), get_the_modified_time(), and get_post_modified_time()):

  • get_comment_date(): ( $format is 'U'|'G' ? int|false : string|false )
  • get_comment_time(): ( $format is 'U'|'G' ? int|false|'' : string|false ), since it also returns an empty string when the comment does not exist.

The get_comment_date and get_comment_time filters now document false as well. Since apply_filters() is typed from the hook's @param, leaving it out made PHPStan report return.unusedType for the new return types.

get_comment_date() also had no guard for a nonexistent comment: it read $comment->comment_date on null, emitting an "Attempt to read property on null" warning on PHP 8, before mysql2date() returned false. It now uses false as the date without touching the null comment, but still applies the get_comment_date filter, following the back-compat pattern of get_the_modified_date() and get_the_modified_time(). Those functions briefly skipped their filters on failure in r37738, which was reverted for back-compat in r37866 (Core-37059). The filter's $comment parameter is now documented as WP_Comment|null, and a test covers the filter receiving false and a null comment.

get_comment_time() is left as is, even though it now handles a nonexistent comment differently: it returns an empty string without applying the get_comment_time filter. Before r55284 (Core-52322) it behaved like get_comment_date() does here, passing false through the filter (after the same warning), but the early return added there has been the behavior since WordPress 6.2, so restoring the filter is not a back-compat fix and is out of scope. A follow-up could align it with the modified-date pattern by returning false and applying the filter, which would also drop the '' from its conditional return type.

These were the only date functions found lacking a conditional return: date_i18n(), wp_date(), and get_feed_build_date() always return a string, even for 'U'.

At level 10, a few unchanged callers that pass the result straight into a string parameter (the comment feed templates' mysql2date( …, get_comment_time( 'Y-m-d H:i:s', true, false ), false )) now surface the pre-existing false case as argument.type. Nothing new is reported at the configured level 5.

Trac ticket: Core-65817

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 5.5 (medium effort)
Used for: Applying the suggested patch, surveying the other date functions, verifying with PHPStan, and drafting this description; reviewed by me.


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

westonruter and others added 2 commits November 4, 2026 13:08
…et_comment_time()`.

Both functions return the result of `mysql2date()`, which is an integer for the 'U' and 'G' formats and false when the date is missing or cannot be parsed, yet they were documented as returning only a string. This forced callers such as `human_time_diff( get_comment_date( 'U' ) )` to cast the result.

Document the actual `string|int|false` return type, with a `@phpstan-return` conditional on `$format` that matches the ones on `mysql2date()` and the post date functions (`get_the_date()`, `get_post_time()`, etc.). `get_comment_time()` also returns an empty string when the comment does not exist. The `get_comment_date` and `get_comment_time` filters now document false too, since `apply_filters()` is typed from them.

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

Co-Authored-By: Claude Opus 5.5 (medium effort) <noreply@anthropic.com>
Claude-Code-Session-Id: bb6ac735-361d-4cf9-8285-f96ee1a308af
@westonruter
westonruter requested a lite review from Copilot November 4, 2026 20:23
@westonruter
westonruter marked this pull request as ready for review November 4, 2026 20:24
@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs.

Core Committers: Use this line as a base for the props when committing in SVN:

Props westonruter.

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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues remain, and the documentation changes align the annotated return types and filter parameters with the described behavior.

No open issues 🎉

What changed in this PR

Updates the documentation for get_comment_date() and get_comment_time() so their return types accurately reflect mysql2date() behavior and support PHPStan inference.

Changes:

  • Documents string|int|false return values.
  • Adds conditional PHPStan return types for 'U' and 'G' formats.
  • Documents the empty-string case for get_comment_time().
  • Updates filter parameter documentation to include false.
File Description
src/​wp-includes/​comment-template.php Adds conditional return-type annotations and updates related filter parameter documentation.

Review effort: Lite

@github-actions

github-actions Bot commented Nov 4, 2026

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

westonruter and others added 2 commits November 4, 2026 13:29
…mment.

Previously a missing comment led to reading `comment_date` on null, which emits a warning on PHP 8, and to passing null to the `get_comment_date` filter, whose `$comment` parameter is documented as `WP_Comment`. Return false early instead, as `get_the_date()` does for a missing post and as `get_comment_time()` does (with an empty string) for a missing comment.

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

The early return added in the previous commit skipped the filter, which has always run for a nonexistent comment. Follow the back-compat pattern of `get_the_modified_date()` and `get_the_modified_time()` from r37866 instead: use false as the date and still apply the filter, now documenting that its `$comment` parameter can be null.

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

Choose a reason for hiding this comment

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

🟢 Approval recommended

The review identifies no active issues, with regression coverage and documentation updates supporting the intended missing-comment behavior and conditional return types.

No open issues 🎉

Review effort: Lite

wporg-sync pushed a commit that referenced this pull request Nov 4, 2026
Both `get_comment_date()` and `get_comment_time()` return the result of `mysql2date()`, which is an integer for the 'U' and 'G' formats and false on failure, yet they were documented as returning only a string. Callers such as the Comment Date block therefore had to cast `get_comment_date( 'U', $comment )` before passing it to `human_time_diff()`. They now declare the same conditional return on `$format` as `mysql2date()` and the post date functions, and the `get_comment_date` and `get_comment_time` filters document false as well, since the return type of `apply_filters()` is derived from them.

A nonexistent comment no longer causes `get_comment_date()` to read a property on null. For back-compat, false is still passed through the `get_comment_date` filter, as is done in `get_the_modified_date()` and `get_the_modified_time()`, so the filter's `$comment` parameter is now documented as possibly null. Since r55284, `get_comment_time()` has returned an empty string for a nonexistent comment without applying its filter, and this is left unchanged.

Developed in #15473.
Follow-up to r37866, r55284, r63941.

See #65817.


git-svn-id: https://develop.svn.wordpress.org/trunk@65263 602fd350-edb4-49c9-b593-d223f7449a82
@westonruter westonruter closed this Nov 4, 2026
wporg-sync pushed a commit to WordPress/WordPress that referenced this pull request Nov 4, 2026
Both `get_comment_date()` and `get_comment_time()` return the result of `mysql2date()`, which is an integer for the 'U' and 'G' formats and false on failure, yet they were documented as returning only a string. Callers such as the Comment Date block therefore had to cast `get_comment_date( 'U', $comment )` before passing it to `human_time_diff()`. They now declare the same conditional return on `$format` as `mysql2date()` and the post date functions, and the `get_comment_date` and `get_comment_time` filters document false as well, since the return type of `apply_filters()` is derived from them.

A nonexistent comment no longer causes `get_comment_date()` to read a property on null. For back-compat, false is still passed through the `get_comment_date` filter, as is done in `get_the_modified_date()` and `get_the_modified_time()`, so the filter's `$comment` parameter is now documented as possibly null. Since r55284, `get_comment_time()` has returned an empty string for a nonexistent comment without applying its filter, and this is left unchanged.

Developed in WordPress/wordpress-develop#15473.
Follow-up to r37866, r55284, r63941.

See #65817.

Built from https://develop.svn.wordpress.org/trunk@65263


git-svn-id: http://core.svn.wordpress.org/trunk@64412 1a063a9b-81f0-0310-95a4-ce76da25c4cd
arshidkv12 pushed a commit to arshidkv12/wordpress-develop that referenced this pull request Nov 21, 2026
Both `get_comment_date()` and `get_comment_time()` return the result of `mysql2date()`, which is an integer for the 'U' and 'G' formats and false on failure, yet they were documented as returning only a string. Callers such as the Comment Date block therefore had to cast `get_comment_date( 'U', $comment )` before passing it to `human_time_diff()`. They now declare the same conditional return on `$format` as `mysql2date()` and the post date functions, and the `get_comment_date` and `get_comment_time` filters document false as well, since the return type of `apply_filters()` is derived from them.

A nonexistent comment no longer causes `get_comment_date()` to read a property on null. For back-compat, false is still passed through the `get_comment_date` filter, as is done in `get_the_modified_date()` and `get_the_modified_time()`, so the filter's `$comment` parameter is now documented as possibly null. Since r55284, `get_comment_time()` has returned an empty string for a nonexistent comment without applying its filter, and this is left unchanged.

Developed in WordPress#15473.
Follow-up to r37866, r55284, r63941.

See #65817.


git-svn-id: https://develop.svn.wordpress.org/trunk@65263 602fd350-edb4-49c9-b593-d223f7449a82
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants