-
Notifications
You must be signed in to change notification settings - Fork 574
Report invalid DateInterval constructor arguments at analysis time
#5587
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
staabm
merged 7 commits into
phpstan:2.1.x
from
phpstan-bot:create-pull-request/patch-v8z1qut
May 5, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a44be18
Report invalid `DateInterval` and `DateTimeZone` constructor argument…
staabm 2d5bad0
Remove DateTimeZoneInstantiationRule per review feedback
phpstan-bot 3b63a9c
Split DateInterval test expectations by PHP version
phpstan-bot a71fa23
Make DateIntervalInstantiationRule bleeding edge only
phpstan-bot 266fa4e
normalize errors accross php version
staabm 1d8670c
Update DateIntervalInstantiationRule.php
staabm 7210b30
Stop normalizing DateInterval error messages across PHP versions
phpstan-bot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Rules; | ||
|
|
||
| use DateInterval; | ||
| use PhpParser\Node; | ||
| use PhpParser\Node\Expr\New_; | ||
| use PHPStan\Analyser\Scope; | ||
| use Throwable; | ||
| use function count; | ||
| use function sprintf; | ||
| use function strtolower; | ||
|
|
||
| /** | ||
| * @implements Rule<Node\Expr\New_> | ||
| */ | ||
| final class DateIntervalInstantiationRule implements Rule | ||
| { | ||
|
|
||
| public function getNodeType(): string | ||
| { | ||
| return New_::class; | ||
| } | ||
|
|
||
| /** | ||
| * @param New_ $node | ||
| */ | ||
| public function processNode(Node $node, Scope $scope): array | ||
| { | ||
| if (!$node->class instanceof Node\Name) { | ||
| return []; | ||
| } | ||
|
|
||
| if ( | ||
| count($node->getArgs()) === 0 | ||
| || strtolower((string) $node->class) !== 'dateinterval' | ||
| ) { | ||
| return []; | ||
| } | ||
|
|
||
| $arg = $scope->getType($node->getArgs()[0]->value); | ||
| $errors = []; | ||
|
|
||
| foreach ($arg->getConstantStrings() as $constantString) { | ||
| $dateIntervalString = $constantString->getValue(); | ||
| try { | ||
| new DateInterval($dateIntervalString); | ||
| } catch (Throwable $e) { | ||
| $errors[] = RuleErrorBuilder::message(sprintf( | ||
| 'Instantiating DateInterval with %s produces an error: %s', | ||
| $dateIntervalString, | ||
| $e->getMessage(), | ||
| ))->identifier('new.dateInterval')->build(); | ||
| } | ||
| } | ||
|
|
||
| return $errors; | ||
| } | ||
|
|
||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Rules; | ||
|
|
||
| use PHPStan\Testing\RuleTestCase; | ||
| use const PHP_VERSION_ID; | ||
|
|
||
| /** | ||
| * @extends RuleTestCase<DateIntervalInstantiationRule> | ||
| */ | ||
| class DateIntervalInstantiationRuleTest extends RuleTestCase | ||
| { | ||
|
|
||
| protected function getRule(): Rule | ||
| { | ||
| return new DateIntervalInstantiationRule(); | ||
| } | ||
|
|
||
| public function test(): void | ||
| { | ||
| if (PHP_VERSION_ID < 80100) { | ||
| $prefix = 'DateInterval::__construct(): '; | ||
| } else { | ||
| $prefix = ''; | ||
| } | ||
|
|
||
| $this->analyse( | ||
| [__DIR__ . '/data/date-interval-instantiation.php'], | ||
| [ | ||
| [ | ||
| 'Instantiating DateInterval with 1M produces an error: ' . $prefix . 'Unknown or bad format (1M)', | ||
| 5, | ||
| ], | ||
| [ | ||
| 'Instantiating DateInterval with asdfasdf produces an error: ' . $prefix . 'Unknown or bad format (asdfasdf)', | ||
| 18, | ||
| ], | ||
| [ | ||
| 'Instantiating DateInterval with produces an error: ' . $prefix . 'Unknown or bad format ()', | ||
| 21, | ||
| ], | ||
| [ | ||
| 'Instantiating DateInterval with 1M produces an error: ' . $prefix . 'Unknown or bad format (1M)', | ||
| 30, | ||
| ], | ||
| [ | ||
| 'Instantiating DateInterval with invalid produces an error: ' . $prefix . 'Unknown or bad format (invalid)', | ||
| 37, | ||
| ], | ||
| ], | ||
| ); | ||
| } | ||
|
|
||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| <?php | ||
| namespace DateIntervalInstantiation; | ||
|
|
||
| // invalid - missing P prefix | ||
| new \DateInterval('1M'); | ||
|
|
||
| // valid durations | ||
| new \DateInterval('P1Y'); | ||
| new \DateInterval('P1M'); | ||
| new \DateInterval('P1D'); | ||
| new \DateInterval('PT1H'); | ||
| new \DateInterval('PT1M'); | ||
| new \DateInterval('PT1S'); | ||
| new \DateInterval('P1Y2M3DT4H5M6S'); | ||
| new \DateInterval('P7D'); | ||
|
|
||
| // invalid | ||
| new \DateInterval('asdfasdf'); | ||
|
|
||
| // empty string is invalid | ||
| new \DateInterval(''); | ||
|
|
||
| // non-constant string - should not report | ||
| function foo(string $duration): void { | ||
| new \DateInterval($duration); | ||
| } | ||
|
|
||
| // constant string via variable | ||
| $test = '1M'; | ||
| new \DateInterval($test); | ||
|
|
||
| /** | ||
| * @param 'invalid' $duration2 | ||
| */ | ||
| function bar(string $duration, string $duration2): void { | ||
| new \DateInterval($duration); | ||
| new \DateInterval($duration2); | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.