Skip to content

Reject dates that parse only with a warning in DateTimeCoercer - #53

Closed
JaWitold wants to merge 1 commit into
dualmediaspzoo:masterfrom
JaWitold:fix/datetime-coercer-rejects-overflowing-dates
Closed

JaWitold wants to merge 1 commit into
dualmediaspzoo:masterfrom
JaWitold:fix/datetime-coercer-rejects-overflowing-dates

Conversation

@JaWitold

Copy link
Copy Markdown
Contributor

Problem

DateTimeCoercer treated only false from createFromFormat() (or an exception from the constructor) as a failed parse. PHP rolls an out-of-range date over and records only a warning:

\DateTimeImmutable::createFromFormat('!Y-m-d', '2026-02-30'); // 2026-03-02, warning "The parsed date was invalid"

So a \DateTimeImmutable property with #[Format('!Y-m-d')] accepted 2026-02-30 and silently handed the controller 2 March. Same on the no-format path (new \DateTimeImmutable('2026-02-30 10:00:00')).

Fix

After parsing, check \DateTimeImmutable::getLastErrors(); any warning_count > 0 returns the raw value, so the existing Type constraint reports it as invalid. Both paths covered. Documented in EXAMPLES.md.

Tests

DateTimeCoercerTest: overflowing date with format, overflowing date without format (both red before the fix), trailing garbage with format (pinned, already rejected).

PHPStan clean. Full suite: 470 tests, 1 pre-existing unrelated failure on master (CacheReflectorTest::testUnknownClassReturnsNull).

createFromFormat() and the DateTimeImmutable constructor roll an out-of-range date over (30 February becomes 2 March) and only record a warning, so the coercer accepted it as valid input. A parse with warnings now returns the raw value and the Type constraint reports it.

Signed-off-by: JaWitold <witekkaras00@gmail.com>
@JaWitold
JaWitold force-pushed the fix/datetime-coercer-rejects-overflowing-dates branch 2 times, most recently from 57805af to e3dc667 Compare October 29, 2026 10:13
@JaWitold JaWitold closed this Oct 29, 2026
@JaWitold
JaWitold deleted the fix/datetime-coercer-rejects-overflowing-dates branch October 29, 2026 10:14
@JaWitold
JaWitold restored the fix/datetime-coercer-rejects-overflowing-dates branch October 29, 2026 10:15
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