Repository navigation
Infer literal types for word date format characters - #6838
Draft
westonruter wants to merge 2 commits into
Draft
westonruter wants to merge 2 commits into
westonruter wants to merge 2 commits into
Conversation
The `a`, `A`, `S`, `D`, `l`, `M`, and `F` format characters of `date()`,
`date_format()`, and `DateTimeInterface::format()` now produce unions of
their possible values, like the numeric single-character formats already do.
PHP formats these characters from hard-coded English tables, so the values
do not depend on the locale.
The final `union()` with `non-empty-string` in `getTypeFromFormatType()` is
removed, since it would otherwise widen these literal unions (and any other
non-numeric constant result, such as `date(' ')`) to `non-empty-string`.
A non-empty format yielding a non-empty result is already guaranteed: a
non-constant format is handled by the `non-empty-string` branch above, and
every type built for a constant non-empty format is itself non-empty.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The "S" date format returns English ordinal suffixes, and typos reports the "nd" string literal as a misspelling of "and". Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
a,A,S,D,l,M, andFformat characters ofdate(),date_format(), andDateTimeInterface::format()now produce unions of their possible values, alongside the numeric single-character formats added in #721. PHP formats these characters from hard-coded English tables inext/date/php_date.c, so the values do not depend on the locale.a'am'|'pm'A'AM'|'PM'S'nd'|'rd'|'st'|'th'D'Fri'|'Mon'|'Sat'|'Sun'|'Thu'|'Tue'|'Wed'l'Friday'|'Monday'|'Saturday'|'Sunday'|'Thursday'|'Tuesday'|'Wednesday'M'Apr'|'Aug'|'Dec'|'Feb'|'Jan'|'Jul'|'Jun'|'Mar'|'May'|'Nov'|'Oct'|'Sep'F'April'|'August'|'December'|'February'|'January'|'July'|'June'|'March'|'May'|'November'|'October'|'September'Removal of the
non-empty-stringwideningAdding the cases alone is not enough:
getTypeFromFormatType()ended withwhich widens any non-numeric constant result to
non-empty-string, so the new unions came out asnon-empty-string. The same happens today for formats without format characters, e.g.date(' ')isnon-empty-stringrather than' '.This block was added in #2888 for phpstan/phpstan#10468 (a non-empty format should give a non-empty result). That guarantee does not depend on it:
count($types) === 0branch, which already returnsnon-empty-stringfor anon-empty-stringformat.buildReturnTypeFromFormat()returns is already non-empty: the explicit cases,ConstantStringType($date),numeric-string, andnon-falsy-string.So the block is removed rather than worked around. The
bug-10468.phpandbug-6613.phptests still pass.Tests
nsrt/bug-15537.phpis the reproducer from the issue. It fails before this change (non-falsy-stringfor every assertion) and passes after.nsrt/date-format.phpexpectednon-falsy-stringforD; it now expects the weekday union.Closes phpstan/phpstan#15537
🤖 Generated with Claude Code