Skip to content

Commit

Permalink
fix: handle class name collision while parsing types inside a class
Browse files Browse the repository at this point in the history
  • Loading branch information
romm committed Aug 8, 2023
1 parent 668cd3f commit 51de4b3
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Type/Parser/Lexer/AliasLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public function tokenize(string $symbol): Token

private function resolve(string $symbol): string
{
if (Reflection::classOrInterfaceExists($symbol)) {
// Matches the case where a class extends a class with the same name but
// in a different namespace.
if ($symbol === $this->reflection->getShortName() && Reflection::classOrInterfaceExists($symbol)) {
return $symbol;
}

Expand Down
33 changes: 33 additions & 0 deletions tests/Integration/Mapping/ClassNameCollisionTestCollision.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace CuyZ\Valinor\Tests\Integration\Mapping;

use CuyZ\Valinor\Mapper\MappingError;
use CuyZ\Valinor\MapperBuilder;
use CuyZ\Valinor\Tests\Integration\IntegrationTest;
use CuyZ\Valinor\Tests\Integration\Mapping\Fixture\Error;

final class ClassNameCollisionTestCollision extends IntegrationTest
{
public function test_mapping_to_class_with_same_class_name_as_native_class_works_properly(): void
{
try {
$result = (new MapperBuilder())
->mapper()
->map(ObjectWithErrorsClassNameCollision::class, ['foo', 'bar']);
} catch (MappingError $error) {
$this->mappingFail($error);
}

self::assertSame('foo', $result->errors[0]->message);
self::assertSame('bar', $result->errors[1]->message);
}
}

final class ObjectWithErrorsClassNameCollision
{
public function __construct(
/** @var list<Error> */
public array $errors
) {}
}
13 changes: 13 additions & 0 deletions tests/Integration/Mapping/Fixture/Error.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace CuyZ\Valinor\Tests\Integration\Mapping\Fixture;

/**
* Has the same name of the native class `Error` on purpose.
*/
final class Error
{
public function __construct(
public string $message
) {}
}

0 comments on commit 51de4b3

Please sign in to comment.