Report invalid DateInterval constructor arguments at analysis time#5587
Open
phpstan-bot wants to merge 4 commits intophpstan:2.1.xfrom
Open
Report invalid DateInterval constructor arguments at analysis time#5587phpstan-bot wants to merge 4 commits intophpstan:2.1.xfrom
DateInterval constructor arguments at analysis time#5587phpstan-bot wants to merge 4 commits intophpstan:2.1.xfrom
Conversation
staabm
reviewed
May 3, 2026
DateInterval and DateTimeZone constructor arguments at analysis timeDateInterval constructor arguments at analysis time
staabm
reviewed
May 3, 2026
| return new DateIntervalInstantiationRule(); | ||
| } | ||
|
|
||
| public function test(): void |
Contributor
There was a problem hiding this comment.
this tests expectations only work for PHP 8.1+.
make it run only on PHP 8.1+.
add another test referecing the same __DIR__ . '/data/date-interval-instantiation.php' path but with different expectations for PHP versions 8.0/7.4
Collaborator
Author
There was a problem hiding this comment.
Done. The review feedback has been addressed:
- Added
#[RequiresPhp('>= 8.1.0')]to the existingtest()method since its error message expectations match PHP 8.1+ format - Added a new
testLegacyPhp()method with#[RequiresPhp('< 8.1.0')]that references the same test data file but expects theDateInterval::__construct():prefix in error messages (the format used on PHP 7.4/8.0) - All tests pass and
make phpstanreports no errors
…s at analysis time - Add DateIntervalInstantiationRule that validates constant duration strings passed to the DateInterval constructor and reports errors for invalid ones - Add DateTimeZoneInstantiationRule that validates constant timezone strings passed to the DateTimeZone constructor and reports errors for invalid ones - Both rules follow the same pattern as the existing DateTimeInstantiationRule: extract constant strings, attempt construction, report exceptions as errors - Both registered at level 5, matching DateTimeInstantiationRule
The DateTimeZone validation rule is less valuable than the DateInterval case since DateInterval durations are more commonly known at analysis time. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
On PHP < 8.1, DateInterval::__construct() prefixes the exception message with "DateInterval::__construct(): ", while PHP 8.1+ uses a shorter format. Gate the existing test with RequiresPhp(>= 8.1.0) and add a separate testLegacyPhp method for older PHP versions. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
070b036 to
c0929f9
Compare
staabm
reviewed
May 3, 2026
| /** | ||
| * @implements Rule<Node\Expr\New_> | ||
| */ | ||
| #[RegisteredRule(level: 5)] |
Contributor
There was a problem hiding this comment.
should it be bleeding edge only?
Collaborator
Author
There was a problem hiding this comment.
Done. The DateIntervalInstantiationRule is now bleeding edge only:
- Removed
#[RegisteredRule(level: 5)]from the rule class - Added
checkDateIntervalConstructorfeature toggle (defaultfalseinconfig.neon,trueinbleedingEdge.neon) - Added the toggle to the parameters schema
- Registered the rule as a service in
config.level5.neonwith aconditionalTagsentry gated on the feature toggle
All tests pass and make phpstan reports no errors.
Co-Authored-By: Claude Opus 4.6 <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.
Summary
PHPStan did not report errors when
DateIntervalwas constructed with an invalid duration string (e.g.new DateInterval('1M')instead ofnew DateInterval('P1M')). While there was already aDateIntervalConstructorThrowTypeExtensionthat refined the throw type, there was no rule to actually report the error to the user.Fixes phpstan/phpstan#14442