Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,72 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules; | ||
|
||
use DateTime; | ||
use DateTimeImmutable; | ||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\New_; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Type\Constant\ConstantStringType; | ||
|
||
/** | ||
* @implements \PHPStan\Rules\Rule<\PhpParser\Node\Expr\New_> | ||
*/ | ||
class DateTimeInstantiationRule implements \PHPStan\Rules\Rule | ||
{ | ||
|
||
public function getNodeType(): string | ||
{ | ||
return New_::class; | ||
} | ||
|
||
/** | ||
* @param New_ $node | ||
*/ | ||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if ( | ||
!($node->class instanceof \PhpParser\Node\Name) | ||
|| \count($node->args) === 0 | ||
|| ( | ||
strtolower((string) $node->class) !== strtolower(DateTime::class) | ||
&& strtolower((string) $node->class) !== strtolower(DateTimeImmutable::class) | ||
) | ||
) { | ||
return []; | ||
} | ||
|
||
$arg = $scope->getType($node->args[0]->value); | ||
if (!($arg instanceof ConstantStringType)) { | ||
return []; | ||
} | ||
|
||
$errors = []; | ||
$dateString = $arg->getValue(); | ||
try { | ||
new DateTime($dateString); | ||
} catch (\Throwable $e) { | ||
// an exception is thrown for errors only but we want to catch warnings too | ||
} | ||
$result = DateTime::getLastErrors(); | ||
foreach ($result['warnings'] as $warning) { | ||
$errors[] = RuleErrorBuilder::message(sprintf( | ||
'Instantiating %s with %s produces a warning: %s', | ||
(string) $node->class, | ||
$dateString, | ||
$warning | ||
))->build(); | ||
} | ||
foreach ($result['errors'] as $error) { | ||
$errors[] = RuleErrorBuilder::message(sprintf( | ||
'Instantiating %s with %s produces an error: %s', | ||
(string) $node->class, | ||
$dateString, | ||
$error | ||
))->build(); | ||
} | ||
|
||
return $errors; | ||
} | ||
|
||
} |
This file contains 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,49 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules; | ||
|
||
/** | ||
* @extends \PHPStan\Testing\RuleTestCase<DateTimeInstantiationRule> | ||
*/ | ||
class DateTimeConstructorRuleTest extends \PHPStan\Testing\RuleTestCase | ||
{ | ||
|
||
protected function getRule(): \PHPStan\Rules\Rule | ||
{ | ||
return new DateTimeInstantiationRule(); | ||
} | ||
|
||
public function test(): void | ||
{ | ||
$this->analyse( | ||
[__DIR__ . '/data/datetime-instantiation.php'], | ||
[ | ||
[ | ||
'Instantiating DateTime with 2020.11.17 produces an error: Double time specification', | ||
3, | ||
], | ||
[ | ||
'Instantiating DateTimeImmutable with asdfasdf produces a warning: Double timezone specification', | ||
5, | ||
], | ||
[ | ||
'Instantiating DateTimeImmutable with asdfasdf produces an error: The timezone could not be found in the database', | ||
5, | ||
], | ||
[ | ||
'Instantiating DateTimeImmutable with 2020.11.17 produces an error: Double time specification', | ||
10, | ||
], | ||
[ | ||
'Instantiating DateTimeImmutable with 2020.11.18 produces an error: Double time specification', | ||
17, | ||
], | ||
[ | ||
'Instantiating DateTime with 2020-04-31 produces a warning: The parsed date was invalid', | ||
20, | ||
], | ||
] | ||
); | ||
} | ||
|
||
} |
This file contains 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,20 @@ | ||
<?php | ||
|
||
new DateTime('2020.11.17'); | ||
|
||
new DateTimeImmutable('asdfasdf'); | ||
|
||
new DateTime(''); | ||
|
||
$test = '2020.11.17'; | ||
new DateTimeImmutable($test); | ||
|
||
/** | ||
* @param '2020.11.18' $date2 | ||
*/ | ||
function foo(string $date, string $date2): void { | ||
new DateTime($date); | ||
new DateTimeImmutable($date2); | ||
} | ||
|
||
new DateTime('2020-04-31'); |