Skip to content

Commit 59ac31a

Browse files
committed
DateTimeInstantiationRule - fix error message for new with wrong name case
1 parent 87256f3 commit 59ac31a

3 files changed

Lines changed: 19 additions & 4 deletions

File tree

src/Rules/DateTimeInstantiationRule.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,14 @@ public function getNodeType(): string
2828
*/
2929
public function processNode(Node $node, Scope $scope): array
3030
{
31+
if (!$node->class instanceof Node\Name) {
32+
return [];
33+
}
34+
35+
$lowerClassName = strtolower((string) $node->class);
3136
if (
32-
!($node->class instanceof Node\Name)
33-
|| count($node->getArgs()) === 0
34-
|| !in_array(strtolower((string) $node->class), ['datetime', 'datetimeimmutable'], true)
37+
count($node->getArgs()) === 0
38+
|| !in_array($lowerClassName, ['datetime', 'datetimeimmutable'], true)
3539
) {
3640
return [];
3741
}
@@ -54,7 +58,7 @@ public function processNode(Node $node, Scope $scope): array
5458
foreach ($lastErrors['errors'] as $error) {
5559
$errors[] = RuleErrorBuilder::message(sprintf(
5660
'Instantiating %s with %s produces an error: %s',
57-
(string) $node->class,
61+
$lowerClassName === 'datetime' ? 'DateTime' : 'DateTimeImmutable',
5862
$dateString,
5963
$error,
6064
))->build();

tests/PHPStan/Rules/DateTimeInstantiationRuleTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ public function test(): void
4444
'Instantiating DateTime with 2020-04-31 produces a warning: The parsed date was invalid',
4545
20,
4646
],*/
47+
[
48+
'Instantiating DateTime with 2020.11.17 produces an error: Double time specification',
49+
22,
50+
],
51+
[
52+
'Instantiating DateTimeImmutable with 2020.11.17 produces an error: Double time specification',
53+
23,
54+
],
4755
],
4856
);
4957
}

tests/PHPStan/Rules/data/datetime-instantiation.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@ function foo(string $date, string $date2): void {
1818
}
1919

2020
new \DateTime('2020-04-31');
21+
22+
new \dateTime('2020.11.17');
23+
new \dateTimeImmutablE('2020.11.17');

0 commit comments

Comments
 (0)