diff --git a/qa/PHPStan/Extension/ApiAndInternalAnnotationCheck.php b/qa/PHPStan/Extension/ApiAndInternalAnnotationCheck.php index c05891c3..632cb4bc 100644 --- a/qa/PHPStan/Extension/ApiAndInternalAnnotationCheck.php +++ b/qa/PHPStan/Extension/ApiAndInternalAnnotationCheck.php @@ -10,6 +10,7 @@ use PHPStan\Rules\Rule; use PHPStan\Rules\RuleErrorBuilder; +use function str_contains; use function str_starts_with; /** @@ -34,9 +35,7 @@ public function processNode(Node $node, Scope $scope): array return []; } - if (str_starts_with($reflection->getName(), 'CuyZ\Valinor\Tests') - || str_starts_with($reflection->getName(), 'SimpleNamespace') - ) { + if (str_contains($reflection->getFileName() ?? '', '/tests/')) { return []; } diff --git a/src/Type/Parser/Lexer/AliasLexer.php b/src/Type/Parser/Lexer/AliasLexer.php index 746d0a77..f9ac6cbd 100644 --- a/src/Type/Parser/Lexer/AliasLexer.php +++ b/src/Type/Parser/Lexer/AliasLexer.php @@ -69,6 +69,10 @@ private function resolveAlias(string $symbol): string return $symbol; } + if ($aliases[$alias] === $symbol) { + return $symbol; + } + $full = $aliases[$alias]; if (! empty($namespaceParts)) { diff --git a/tests/Fixture/Object/ObjectWithNameMatchingRootNamespace.php b/tests/Fixture/Object/ObjectWithNameMatchingRootNamespace.php new file mode 100644 index 00000000..706d0ae0 --- /dev/null +++ b/tests/Fixture/Object/ObjectWithNameMatchingRootNamespace.php @@ -0,0 +1,7 @@ +tokenize($symbol)); } + public function test_object_with_same_name_as_root_namespace_are_checked_in_class(): void + { + $lexer = new AliasLexer($this->delegate, new ReflectionClass(ClassWithAlias::class)); + $symbol = ObjectWithNameMatchingRootNamespace::class; + $token = new FakeToken(); + + $this->delegate->will(ObjectWithNameMatchingRootNamespace::class, $token); + + self::assertSame($token, $lexer->tokenize($symbol)); + } + public function test_resolve_unsupported_type_in_function_returns_same_type(): void { $function = fn () => 42; @@ -121,6 +133,19 @@ public function test_same_namespace_classes_are_checked_in_function(): void self::assertSame($token, $lexer->tokenize($symbol)); } + + public function test_object_with_same_name_as_root_namespace_are_checked_in_function(): void + { + $function = fn () => 42; + + $lexer = new AliasLexer($this->delegate, new ReflectionFunction($function)); + $symbol = ObjectWithNameMatchingRootNamespace::class; + $token = new FakeToken(); + + $this->delegate->will(ObjectWithNameMatchingRootNamespace::class, $token); + + self::assertSame($token, $lexer->tokenize($symbol)); + } } final class ClassWithAlias