From 62ce4bb1227c1b392e3143df4f03b7150e80c823 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20M=C3=B6ller?= Date: Wed, 8 Jan 2025 10:05:20 +0100 Subject: [PATCH] Fix: Detect cases where null is referenced with incorrect case or relative to the root namespace --- CHANGELOG.md | 5 +++++ composer-require-checker.json | 1 + src/Analyzer.php | 12 +++++++++--- .../NoNullableReturnTypeDeclarationRuleTest.php | 4 ++++ ...oParameterWithNullableTypeDeclarationRuleTest.php | 4 ++++ .../NoNullableReturnTypeDeclarationRuleTest.php | 4 ++++ ...oParameterWithNullableTypeDeclarationRuleTest.php | 4 ++++ .../NoNullableReturnTypeDeclarationRuleTest.php | 4 ++++ ...oParameterWithNullableTypeDeclarationRuleTest.php | 4 ++++ 9 files changed, 39 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e705b8a..3a971db8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), For a full diff see [`2.5.1...main`][2.5.1...main]. +### Fixed + +- 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] + ## [`2.5.1`][2.5.1] 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]. [#890]: https://github.com/ergebnis/phpstan-rules/pull/890 [#891]: https://github.com/ergebnis/phpstan-rules/pull/891 [#895]: https://github.com/ergebnis/phpstan-rules/pull/895 +[#897]: https://github.com/ergebnis/phpstan-rules/pull/897 [@enumag]: https://github.com/enumag [@cosmastech]: https://github.com/cosmastech diff --git a/composer-require-checker.json b/composer-require-checker.json index 92f8a316..1f7f3d81 100644 --- a/composer-require-checker.json +++ b/composer-require-checker.json @@ -15,6 +15,7 @@ "PhpParser\\Node\\Expr\\Isset_", "PhpParser\\Node\\Identifier", "PhpParser\\Node\\Name", + "PhpParser\\Node\\Name\\FullyQualified", "PhpParser\\Node\\NullableType", "PhpParser\\Node\\Param", "PhpParser\\Node\\Scalar\\LNumber", diff --git a/src/Analyzer.php b/src/Analyzer.php index 4e0a6d4a..49981a2a 100644 --- a/src/Analyzer.php +++ b/src/Analyzer.php @@ -40,11 +40,17 @@ public function isNullableTypeDeclaration($typeDeclaration): bool if ($typeDeclaration instanceof Node\UnionType) { foreach ($typeDeclaration->types as $type) { - if (!$type instanceof Node\Identifier) { - continue; + if ( + $type instanceof Node\Identifier + && 'null' === $type->toLowerString() + ) { + return true; } - if ('null' === $type->toLowerString()) { + if ( + $type instanceof Node\Name\FullyQualified + && 'null' === $type->toLowerString() + ) { return true; } } diff --git a/test/Integration/Closures/NoNullableReturnTypeDeclarationRuleTest.php b/test/Integration/Closures/NoNullableReturnTypeDeclarationRuleTest.php index dba8e764..00e03fc9 100644 --- a/test/Integration/Closures/NoNullableReturnTypeDeclarationRuleTest.php +++ b/test/Integration/Closures/NoNullableReturnTypeDeclarationRuleTest.php @@ -48,6 +48,10 @@ public function testNoNullableReturnTypeDeclarationRule(): void 'Closure has a nullable return type declaration.', 23, ], + [ + 'Closure has a nullable return type declaration.', + 27, + ], ], ); } diff --git a/test/Integration/Closures/NoParameterWithNullableTypeDeclarationRuleTest.php b/test/Integration/Closures/NoParameterWithNullableTypeDeclarationRuleTest.php index 021580b4..0509288f 100644 --- a/test/Integration/Closures/NoParameterWithNullableTypeDeclarationRuleTest.php +++ b/test/Integration/Closures/NoParameterWithNullableTypeDeclarationRuleTest.php @@ -48,6 +48,10 @@ public function testNoParameterWithNullableTypeDeclarationRule(): void 'Closure has parameter $bar with a nullable type declaration.', 26, ], + [ + 'Closure has parameter $bar with a nullable type declaration.', + 30, + ], ], ); } diff --git a/test/Integration/Functions/NoNullableReturnTypeDeclarationRuleTest.php b/test/Integration/Functions/NoNullableReturnTypeDeclarationRuleTest.php index e87a9561..4fc47792 100644 --- a/test/Integration/Functions/NoNullableReturnTypeDeclarationRuleTest.php +++ b/test/Integration/Functions/NoNullableReturnTypeDeclarationRuleTest.php @@ -48,6 +48,10 @@ public function testNoNullableReturnTypeDeclarationRule(): void 'Function Ergebnis\PHPStan\Rules\Test\Fixture\Functions\NoNullableReturnTypeDeclarationRule\quz() has a nullable return type declaration.', 27, ], + [ + 'Function Ergebnis\PHPStan\Rules\Test\Fixture\Functions\NoNullableReturnTypeDeclarationRule\corge() has a nullable return type declaration.', + 32, + ], ], ); } diff --git a/test/Integration/Functions/NoParameterWithNullableTypeDeclarationRuleTest.php b/test/Integration/Functions/NoParameterWithNullableTypeDeclarationRuleTest.php index b0ba677c..a705f04e 100644 --- a/test/Integration/Functions/NoParameterWithNullableTypeDeclarationRuleTest.php +++ b/test/Integration/Functions/NoParameterWithNullableTypeDeclarationRuleTest.php @@ -48,6 +48,10 @@ public function testNoParameterWithNullableTypeDeclarationRule(): void 'Function Ergebnis\PHPStan\Rules\Test\Fixture\Functions\NoParameterWithNullableTypeDeclarationRule\quz() has parameter $bar with a nullable type declaration.', 31, ], + [ + 'Function Ergebnis\PHPStan\Rules\Test\Fixture\Functions\NoParameterWithNullableTypeDeclarationRule\corge() has parameter $bar with a nullable type declaration.', + 36, + ], ], ); } diff --git a/test/Integration/Methods/NoNullableReturnTypeDeclarationRuleTest.php b/test/Integration/Methods/NoNullableReturnTypeDeclarationRuleTest.php index 41551844..1b430c0e 100644 --- a/test/Integration/Methods/NoNullableReturnTypeDeclarationRuleTest.php +++ b/test/Integration/Methods/NoNullableReturnTypeDeclarationRuleTest.php @@ -76,6 +76,10 @@ public function testNoNullableReturnTypeDeclarationRule(): void 'Method toString() in anonymous class has a nullable return type declaration.', 36, ], + [ + 'Method toString() in anonymous class has a nullable return type declaration.', + 43, + ], ], ); } diff --git a/test/Integration/Methods/NoParameterWithNullableTypeDeclarationRuleTest.php b/test/Integration/Methods/NoParameterWithNullableTypeDeclarationRuleTest.php index 9eadd329..d1d9a740 100644 --- a/test/Integration/Methods/NoParameterWithNullableTypeDeclarationRuleTest.php +++ b/test/Integration/Methods/NoParameterWithNullableTypeDeclarationRuleTest.php @@ -76,6 +76,10 @@ public function testNoParameterWithNullableTypeDeclarationRule(): void 'Method foo() in anonymous class has parameter $bar with a nullable type declaration.', 42, ], + [ + 'Method foo() in anonymous class has parameter $bar with a nullable type declaration.', + 49, + ], ], ); }