Skip to content

Commit

Permalink
fix: properly handle partial namespace signature
Browse files Browse the repository at this point in the history
  • Loading branch information
romm committed Nov 6, 2024
1 parent 3f6d418 commit 9b58f27
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/Type/Parser/Factory/Specifications/AliasSpecification.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use function array_shift;
use function explode;
use function in_array;
use function strtolower;

/** @internal */
Expand Down Expand Up @@ -55,14 +56,12 @@ private function resolveAlias(string $symbol): string
{
$aliases = PhpParser::parseUseStatements($this->reflection);

$namespaceParts = explode('\\', $symbol);

$lastPart = strtolower(end($namespaceParts));

if (isset($aliases[$lastPart])) {
return $aliases[$lastPart];
if (in_array($symbol, $aliases, true)) {
return $symbol;
}

$namespaceParts = explode('\\', $symbol);

$alias = strtolower(array_shift($namespaceParts));

if (! isset($aliases[$alias])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace CuyZ\Valinor\Tests\Unit\Type\Parser\Factory\Specification;

use CuyZ\Valinor\Tests\Fake\Type\Parser\Lexer\Token\FakeToken;
use CuyZ\Valinor\Tests\Integration\Mapping\Fixture as SimpleObject;
use CuyZ\Valinor\Tests\Unit\Type\Parser\Factory\Specification as PathAlias;
use CuyZ\Valinor\Type\Parser\Factory\Specifications\AliasSpecification;
use DateTimeInterface as SecondClassAlias;
Expand Down Expand Up @@ -67,6 +68,16 @@ public function test_object_with_same_name_as_root_namespace_are_checked_in_clas
self::assertSame(ObjectWithNameMatchingRootNamespace::class, $newToken->symbol());
}

public function test_object_with_partial_namespace_alias_matching_class_name_are_checked_in_class(): void
{
$specification = new AliasSpecification(new ReflectionClass(ClassWithAlias::class));

$token = new FakeToken('SimpleObject\SimpleObject');
$newToken = $specification->manipulateToken($token);

self::assertSame(SimpleObject\SimpleObject::class, $newToken->symbol());
}

public function test_resolve_unsupported_type_in_function_returns_same_type(): void
{
$function = fn () => 42;
Expand Down Expand Up @@ -126,6 +137,18 @@ public function test_object_with_same_name_as_root_namespace_are_checked_in_func

self::assertSame(ObjectWithNameMatchingRootNamespace::class, $newToken->symbol());
}

public function test_object_with_partial_namespace_alias_matching_class_name_are_checked_in_function(): void
{
$function = fn () => 42;

$specification = new AliasSpecification(new ReflectionFunction($function));

$token = new FakeToken('SimpleObject\SimpleObject');
$newToken = $specification->manipulateToken($token);

self::assertSame(SimpleObject\SimpleObject::class, $newToken->symbol());
}
}

final class ClassWithAlias
Expand Down

0 comments on commit 9b58f27

Please sign in to comment.