Repository navigation
Comments: Add conditional return types to get_comment_date() and get_comment_time() - #15473
westonruter wants to merge 4 commits into
Conversation
…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
|
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: 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:
|
There was a problem hiding this comment.
🟢 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|falsereturn 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
Test using WordPress PlaygroundThe 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
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
…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
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
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
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
✅ Committed in r65263 (a87c255).
get_comment_date()andget_comment_time()return the result ofmysql2date(), which is an integer for the'U'and'G'formats andfalsewhen the date is missing or cannot be parsed. Yet both were documented as returning only a string, so callers likehuman_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|falsereturn type on both functions, with a@phpstan-returnconditional on$formatmatching the ones already onmysql2date()and the post date functions (get_the_date(),get_the_time(),get_post_time(),get_the_modified_date(),get_the_modified_time(), andget_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_dateandget_comment_timefilters now documentfalseas well. Sinceapply_filters()is typed from the hook's@param, leaving it out made PHPStan reportreturn.unusedTypefor the new return types.get_comment_date()also had no guard for a nonexistent comment: it read$comment->comment_dateon null, emitting an "Attempt to read property on null" warning on PHP 8, beforemysql2date()returned false. It now uses false as the date without touching the null comment, but still applies theget_comment_datefilter, following the back-compat pattern ofget_the_modified_date()andget_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$commentparameter is now documented asWP_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 theget_comment_timefilter. Before r55284 (Core-52322) it behaved likeget_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(), andget_feed_build_date()always return a string, even for'U'.At level 10, a few unchanged callers that pass the result straight into a
stringparameter (the comment feed templates'mysql2date( …, get_comment_time( 'Y-m-d H:i:s', true, false ), false )) now surface the pre-existingfalsecase asargument.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.