Skip to content

AliasSpecification::resolveAlias implementation bug #707

@Florian-agora

Description

@Florian-agora

I got an issue upgrading from 1.7 to the latest version.

After investigation, it appears to come from AliasSpecification::resolveAlias.

private function resolveAlias(string $symbol): string
    {
        $aliases = PhpParser::parseUseStatements($this->reflection);

        if (in_array($symbol, $aliases, true)) {
            return $symbol;
        }

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

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

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

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

        return $aliases[$alias] . '\\' . implode('\\', $namespaceParts);
    }

First the method does $aliases = PhpParser::parseUseStatements($this->reflection);, which is an array with the class / alias as the key.

Then it checks if the alias from $alias = strtolower(array_shift($namespaceParts)); is in the previous array.

Since we are not taking the last part, but the first (array_shift instead of array_pop), we are searching the known aliases from the beginning of the path, not the last part. I am pretty sure this is NOT intended, since I found a previous commit where the variable was called "lastPart" instead.

Metadata

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