Skip to content

ReturnType of "PhpParser\Node\Name\FullyQualified" - but no leading backslash leads to FQN added to current namespace #134

@ancpru

Description

@ancpru

Something strange seems to happen here:

$object->getReturnType() returns a object of PhpParser\Node\Name\FullyQualified despite it's not fully qualified in the source. So far no big deal. But: the string conversion does not contain a leading backslash.

   protected function doCreate($object, StrategyContainer $strategies, ?Context $context = null)
    {
        $docBlock = $this->createDocBlock($strategies, $object->getDocComment(), $context);

        $returnType = null;
        if ($object->getReturnType() !== null) {
            $typeResolver = new TypeResolver();
            if ($object->getReturnType() instanceof NullableType) {
                $typeString = '?' . $object->getReturnType()->type;
            } else {
                $typeString = (string) $object->getReturnType();
            }

            $returnType = $typeResolver->resolve($typeString, $context);

The subsequent call of $typeResolver->resolve($typeString, $context); then leads to a concatination of the namespace of the file with the (not wellformed) FQN (e.g. \My\Current\Namespace\MyCurrent\Namespace\Class)

I did not tried it yet, but it might be enough to make sure that the string contains a leading backslash:

if (($object instanceof \PhpParser\Node\Name\FullyQualified) && ($typeString[0] !== '\\'  ) {
  $typeString = '\\' .$typeString;
}

Update:

Checking on instance of \PhpParser\Node\Name and $returnType->isFullyQualified() might be the even cleaner approach.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions