Skip to content

Commit 4da2b65

Browse files
authored
Merge pull request #897 from ergebnis/fix/null
Fix: Detect cases where `null` is referenced with incorrect case or relative to the root namespace
2 parents b8992e4 + 62ce4bb commit 4da2b65

9 files changed

+39
-3
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
88

99
For a full diff see [`2.5.1...main`][2.5.1...main].
1010

11+
### Fixed
12+
13+
- Adjusted `Closures\NoNullableReturnTypeDeclarationRule`, `Closures\NoParameterWithNullableTypeDeclarationRule`, `Functions\NoNullableReturnTypeDeclarationRule`, `Functions\NoParameterWithNullableTypeDeclarationRule`, `Methods\NoNullableReturnTypeDeclarationRule`, `Methods\NoParameterWithNullableTypeDeclarationRule` to detect cases where `null` is referenced with incorrect case or relative to the root namespace ([#897]), by [@localheinz]
14+
1115
## [`2.5.1`][2.5.1]
1216

1317
For a full diff see [`2.5.0...2.5.1`][2.5.0...2.5.1].
@@ -566,6 +570,7 @@ For a full diff see [`362c7ea...0.1.0`][362c7ea...0.1.0].
566570
[#890]: https://github.com/ergebnis/phpstan-rules/pull/890
567571
[#891]: https://github.com/ergebnis/phpstan-rules/pull/891
568572
[#895]: https://github.com/ergebnis/phpstan-rules/pull/895
573+
[#897]: https://github.com/ergebnis/phpstan-rules/pull/897
569574

570575
[@enumag]: https://github.com/enumag
571576
[@cosmastech]: https://github.com/cosmastech

composer-require-checker.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"PhpParser\\Node\\Expr\\Isset_",
1616
"PhpParser\\Node\\Identifier",
1717
"PhpParser\\Node\\Name",
18+
"PhpParser\\Node\\Name\\FullyQualified",
1819
"PhpParser\\Node\\NullableType",
1920
"PhpParser\\Node\\Param",
2021
"PhpParser\\Node\\Scalar\\LNumber",

src/Analyzer.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,17 @@ public function isNullableTypeDeclaration($typeDeclaration): bool
4040

4141
if ($typeDeclaration instanceof Node\UnionType) {
4242
foreach ($typeDeclaration->types as $type) {
43-
if (!$type instanceof Node\Identifier) {
44-
continue;
43+
if (
44+
$type instanceof Node\Identifier
45+
&& 'null' === $type->toLowerString()
46+
) {
47+
return true;
4548
}
4649

47-
if ('null' === $type->toLowerString()) {
50+
if (
51+
$type instanceof Node\Name\FullyQualified
52+
&& 'null' === $type->toLowerString()
53+
) {
4854
return true;
4955
}
5056
}

test/Integration/Closures/NoNullableReturnTypeDeclarationRuleTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ public function testNoNullableReturnTypeDeclarationRule(): void
4848
'Closure has a nullable return type declaration.',
4949
23,
5050
],
51+
[
52+
'Closure has a nullable return type declaration.',
53+
27,
54+
],
5155
],
5256
);
5357
}

test/Integration/Closures/NoParameterWithNullableTypeDeclarationRuleTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ public function testNoParameterWithNullableTypeDeclarationRule(): void
4848
'Closure has parameter $bar with a nullable type declaration.',
4949
26,
5050
],
51+
[
52+
'Closure has parameter $bar with a nullable type declaration.',
53+
30,
54+
],
5155
],
5256
);
5357
}

test/Integration/Functions/NoNullableReturnTypeDeclarationRuleTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ public function testNoNullableReturnTypeDeclarationRule(): void
4848
'Function Ergebnis\PHPStan\Rules\Test\Fixture\Functions\NoNullableReturnTypeDeclarationRule\quz() has a nullable return type declaration.',
4949
27,
5050
],
51+
[
52+
'Function Ergebnis\PHPStan\Rules\Test\Fixture\Functions\NoNullableReturnTypeDeclarationRule\corge() has a nullable return type declaration.',
53+
32,
54+
],
5155
],
5256
);
5357
}

test/Integration/Functions/NoParameterWithNullableTypeDeclarationRuleTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ public function testNoParameterWithNullableTypeDeclarationRule(): void
4848
'Function Ergebnis\PHPStan\Rules\Test\Fixture\Functions\NoParameterWithNullableTypeDeclarationRule\quz() has parameter $bar with a nullable type declaration.',
4949
31,
5050
],
51+
[
52+
'Function Ergebnis\PHPStan\Rules\Test\Fixture\Functions\NoParameterWithNullableTypeDeclarationRule\corge() has parameter $bar with a nullable type declaration.',
53+
36,
54+
],
5155
],
5256
);
5357
}

test/Integration/Methods/NoNullableReturnTypeDeclarationRuleTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ public function testNoNullableReturnTypeDeclarationRule(): void
7676
'Method toString() in anonymous class has a nullable return type declaration.',
7777
36,
7878
],
79+
[
80+
'Method toString() in anonymous class has a nullable return type declaration.',
81+
43,
82+
],
7983
],
8084
);
8185
}

test/Integration/Methods/NoParameterWithNullableTypeDeclarationRuleTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ public function testNoParameterWithNullableTypeDeclarationRule(): void
7676
'Method foo() in anonymous class has parameter $bar with a nullable type declaration.',
7777
42,
7878
],
79+
[
80+
'Method foo() in anonymous class has parameter $bar with a nullable type declaration.',
81+
49,
82+
],
7983
],
8084
);
8185
}

0 commit comments

Comments
 (0)