Skip to content

Follow-ups for WP_Locale weekday and month parameter types - #1

Open
westonruter wants to merge 6 commits into
rootzero00:docs/64522-weekday-rangefrom
westonruter:docs/64522-weekday-range-followups-pr
Open

westonruter wants to merge 6 commits into
rootzero00:docs/64522-weekday-rangefrom
westonruter:docs/64522-weekday-range-followups-pr

Conversation

@westonruter

Copy link
Copy Markdown

Follow-up changes for WordPress#14827, proposed here because "Allow edits by maintainers" is disabled on that PR. These are 6 commits on top of fefbbea, the current head of docs/64522-weekday-range:

  • Clamp the start of the week in get_calendar().
  • Handle a missing post weekday in the_weekday() and the_weekday_date().
  • Narrow the get_post_time() return type by date format.
  • Pass the weekday number to WP_Locale::get_weekday() as an int in wp_date().
  • Return false from wp_date() when the timestamp cannot be parsed, with tests.
  • Keep the full month number type for WP_Locale::get_month().

See the comment on WordPress#14827 for the reasoning behind each change and the PHPStan error breakdown. Merging this PR adds the commits to that PR.

Trac ticket: https://core.trac.wordpress.org/ticket/64522

🤖 Generated with Claude Code

westonruter and others added 6 commits October 25, 2026 10:30
The `start_of_week` option is cast to an int but otherwise unchecked, so a
negative value makes `( $wdcount + $week_begins ) % 7` negative, and a value
above 6 is out of range for `WP_Locale::get_weekday()`. Either leads to an
undefined array key. Clamping it to 0-6 keeps the weekday headers valid and
satisfies the new `int<0, 6>` type of `WP_Locale::get_weekday()`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…kday_date().

`get_post_time()` returns false when the post date cannot be parsed. Both
functions passed that straight to `WP_Locale::get_weekday()`, where it was
coerced to the array key 0, so the post was silently labeled Sunday. Now
nothing is output in that case, and the filterable string result is cast and
clamped to 0-6 to match the `int<0, 6>` type of `WP_Locale::get_weekday()`.

This removes the corresponding entry from the `argument.type` PHPStan baseline.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
For each format character whose output has a fixed range, such as `w` for
`'0'` through `'6'` or `m` for `'01'` through `'12'`, the conditional return
type now gives that range, mirroring what PHPStan infers for
`DateTimeInterface::format()`. Formats with an open-ended numeric output give
`numeric-string`. Formats that `wp_date()` translates (`D`, `l`, `M`, `F`, `a`,
and `A`) and the PHP 8.2+ `X` and `x` remain `string`.

With `w` narrowed, the weekday number passed to `WP_Locale::get_weekday()` in
the_weekday() and the_weekday_date() already satisfies `int<0, 6>` after an
int cast, so the `clamp()` calls are no longer needed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…int in wp_date().

`DateTimeInterface::format( 'w' )` returns a string from `'0'` to `'6'`, which
worked only because the string was coerced to an integer array key. Casting it
to an int matches the `int<0, 6>` parameter type and removes the last
`get_weekday()` entry from the `argument.type` PHPStan baseline.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…arsed.

`wp_date()` only checked the timestamp with `is_numeric()`, which accepts
values that `date_create( '@' . $timestamp )` rejects, such as `'1e3'`, `1e20`,
`' 5'`, and `'.5'`. For those, `date_create()` returned false and the following
`setTimezone()` call caused a fatal error. Now false is returned, as documented
for invalid timestamp input and as already done for non-numeric input.

This also resolves the PHPStan `method.nonObject` errors on every use of
`$datetime` in the function.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Unlike the other `WP_Locale` getters, `get_month()` returns an empty string for
a month number that is not found, which was added so that callers like
`single_month_title()` can pass unvalidated query var values. Narrowing its
parameter to valid month numbers told callers to validate input the method
already handles, surfacing errors at call sites that pass `zeroise()` results,
query vars, or database values.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

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.

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.

1 participant