Skip to content

Commit

Permalink
fix: properly handle class type with matching name and namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
romm committed Mar 24, 2024
1 parent 37993b6 commit 0f5e96e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
5 changes: 2 additions & 3 deletions qa/PHPStan/Extension/ApiAndInternalAnnotationCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;

use function str_contains;
use function str_starts_with;

/**
Expand All @@ -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 [];
}

Expand Down
4 changes: 4 additions & 0 deletions src/Type/Parser/Lexer/AliasLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ private function resolveAlias(string $symbol): string
return $symbol;
}

if ($aliases[$alias] === $symbol) {
return $symbol;
}

$full = $aliases[$alias];

if (! empty($namespaceParts)) {
Expand Down
7 changes: 7 additions & 0 deletions tests/Fixture/Object/ObjectWithNameMatchingRootNamespace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

declare(strict_types=1);

namespace ObjectWithNameMatchingRootNamespace;

final class ObjectWithNameMatchingRootNamespace {}
25 changes: 25 additions & 0 deletions tests/Unit/Type/Parser/Lexer/AliasLexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use CuyZ\Valinor\Tests\Unit\Type\Parser\Lexer as PathAlias;
use CuyZ\Valinor\Type\Parser\Lexer\AliasLexer;
use DateTimeInterface as SecondClassAlias;
use ObjectWithNameMatchingRootNamespace\ObjectWithNameMatchingRootNamespace;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use ReflectionFunction;
Expand Down Expand Up @@ -70,6 +71,17 @@ public function test_same_namespace_classes_are_checked_in_class(): void
self::assertSame($token, $lexer->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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0f5e96e

Please sign in to comment.