Skip to content

Commit

Permalink
Fix: Detect cases where null is referenced with incorrect case or rel…
Browse files Browse the repository at this point in the history
…ative to the root namespace
  • Loading branch information
localheinz committed Jan 8, 2025
1 parent b8992e4 commit 62ce4bb
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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].
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions composer-require-checker.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 9 additions & 3 deletions src/Analyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public function testNoNullableReturnTypeDeclarationRule(): void
'Closure has a nullable return type declaration.',
23,
],
[
'Closure has a nullable return type declaration.',
27,
],
],
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
],
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
],
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
],
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
],
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
],
);
}
Expand Down

0 comments on commit 62ce4bb

Please sign in to comment.