phpDocumentor / phpDocumentor/Reflection

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

Open
#134 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
125
Forks
60
Avg merge
2m
Merged PRs (30d)
9

Description

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at doCreate where getReturnType() is converted to a string and passed to TypeResolver::resolve(), then inspect how FullyQualified and Name instances are handled. Reproduce the namespace-duplication case described in the issue and confirm that resolving a fully qualified return type does not prepend the current namespace.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.