Skip to content

Potential Bug in Class Name Resolution #543

Open
@NHZEX

Description

I've encountered a potential bug related to class name resolution in the codebase. When attempting to use a specific class, an error is thrown indicating that a non-existent class is being referenced.

Create Code

Base.php

<?php
declare(strict_types=1);

namespace tests;

use does_not_exist_namespace\tests;

abstract class Base
{
    public static function mustReturnStatic(): static
    {
        return new static();
    }
}

Element.php

<?php
declare(strict_types=1);

namespace tests;

class Element extends Base
{
    public function __construct(
        protected string $name,
        protected int $status,
    )
    {
    }
}

Execute the following code

use CuyZ\Valinor\Mapper\Source\Source;
use CuyZ\Valinor\MapperBuilder;

$source = Source::array([
    'name' => 'a',
    'status' => 1
]);
$builder = (new MapperBuilder());
var_dump($builder->mapper()->map(\tests\Element::class, $source));

Observe the error message:

  [ReflectionException]                                       
  Class "does_not_exist_namespace\tests\Base" does not exist  

Exception trace:
 () at /home/www-api/vendor/cuyz/valinor/src/Utility/Reflection/Reflection.php:80
 ReflectionClass->__construct() at /home/www-api/vendor/cuyz/valinor/src/Utility/Reflection/Reflection.php:80
 CuyZ\Valinor\Utility\Reflection\Reflection::class() at /home/www-api/vendor/cuyz/valinor/src/Type/Parser/Lexer/Token/ClassNameToken.php:43
 CuyZ\Valinor\Type\Parser\Lexer\Token\ClassNameToken->__construct() at /home/www-api/vendor/cuyz/valinor/src/Type/Parser/Lexer/Token/ObjectToken.php:27
 CuyZ\Valinor\Type\Parser\Lexer\Token\ObjectToken->traverse() at /home/www-api/vendor/cuyz/valinor/src/Type/Parser/Lexer/Token/VacantToken.php:30
 CuyZ\Valinor\Type\Parser\Lexer\Token\VacantToken->traverse() at /home/www-api/vendor/cuyz/valinor/src/Type/Parser/Lexer/TokenStream.php:36
 CuyZ\Valinor\Type\Parser\Lexer\TokenStream->read() at /home/www-api/vendor/cuyz/valinor/src/Type/Parser/LexingParser.php:24
 CuyZ\Valinor\Type\Parser\LexingParser->parse() at /home/www-api/vendor/cuyz/valinor/src/Type/Parser/GenericCheckerParser.php:35
 CuyZ\Valinor\Type\Parser\GenericCheckerParser->parse() at /home/www-api/vendor/cuyz/valinor/src/Definition/Repository/Reflection/ReflectionTypeResolver.php:106
 CuyZ\Valinor\Definition\Repository\Reflection\ReflectionTypeResolver->parseType() at /home/www-api/vendor/cuyz/valinor/src/Definition/Repository/Reflection/ReflectionTypeResolver.php:42
 CuyZ\Valinor\Definition\Repository\Reflection\ReflectionTypeResolver->resolveType() at /home/www-api/vendor/cuyz/valinor/src/Definition/Repository/Reflection/ReflectionMethodDefinitionBuilder.php:46
 CuyZ\Valinor\Definition\Repository\Reflection\ReflectionMethodDefinitionBuilder->for() at /home/www-api/vendor/cuyz/valinor/src/Definition/Repository/Reflection/ReflectionClassDefinitionRepository.php:129
 CuyZ\Valinor\Definition\Repository\Reflection\ReflectionClassDefinitionRepository->CuyZ\Valinor\Definition\Repository\Reflection\{closure}() at n/a:n/a
 array_map() at /home/www-api/vendor/cuyz/valinor/src/Definition/Repository/Reflection/ReflectionClassDefinitionRepository.php:130
 CuyZ\Valinor\Definition\Repository\Reflection\ReflectionClassDefinitionRepository->methods() at /home/www-api/vendor/cuyz/valinor/src/Definition/Repository/Reflection/ReflectionClassDefinitionRepository.php:78
 CuyZ\Valinor\Definition\Repository\Reflection\ReflectionClassDefinitionRepository->for() at /home/www-api/vendor/cuyz/valinor/src/Definition/Repository/Cache/CacheClassDefinitionRepository.php:34
 CuyZ\Valinor\Definition\Repository\Cache\CacheClassDefinitionRepository->for() at /home/www-api/vendor/cuyz/valinor/src/Mapper/Tree/Builder/InterfaceNodeBuilder.php:60
 CuyZ\Valinor\Mapper\Tree\Builder\InterfaceNodeBuilder->build() at /home/www-api/vendor/cuyz/valinor/src/Mapper/Tree/Builder/CasterProxyNodeBuilder.php:28
 CuyZ\Valinor\Mapper\Tree\Builder\CasterProxyNodeBuilder->build() at /home/www-api/vendor/cuyz/valinor/src/Mapper/Tree/Builder/IterableNodeBuilder.php:26
 CuyZ\Valinor\Mapper\Tree\Builder\IterableNodeBuilder->build() at /home/www-api/vendor/cuyz/valinor/src/Mapper/Tree/Builder/StrictNodeBuilder.php:36
 CuyZ\Valinor\Mapper\Tree\Builder\StrictNodeBuilder->build() at /home/www-api/vendor/cuyz/valinor/src/Mapper/Tree/Builder/ErrorCatcherNodeBuilder.php:33
 CuyZ\Valinor\Mapper\Tree\Builder\ErrorCatcherNodeBuilder->build() at /home/www-api/vendor/cuyz/valinor/src/Mapper/Tree/Builder/RootNodeBuilder.php:16
 CuyZ\Valinor\Mapper\Tree\Builder\RootNodeBuilder->build() at /home/www-api/vendor/cuyz/valinor/src/Mapper/TypeTreeMapper.php:47
 CuyZ\Valinor\Mapper\TypeTreeMapper->node() at /home/www-api/vendor/cuyz/valinor/src/Mapper/TypeTreeMapper.php:27
 CuyZ\Valinor\Mapper\TypeTreeMapper->map() at /home/www-api/scripts/tets.php:47

An error is thrown, indicating that the class does_not_exist_namespace\tests\Base does not exist, which seems to stem from an incorrect class name resolution process.

Environment:

PHP version: 8.1
Library version: 1.12.0

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions