tolerant-php-parser icon indicating copy to clipboard operation
tolerant-php-parser copied to clipboard

Why is QualifiedName::getResolvedName disabled for Node\TraitUseClause?

Open zobo opened this issue 2 years ago • 4 comments
trafficstars

When I need to resolve the FQN of the use TestTrait I cannot use the handy getResolvedName. Why is that?

namespace YYY;

use \XXX\TestTrait;

class TestClazz
{
	use TestTrait;

https://github.com/microsoft/tolerant-php-parser/blame/84ce745a39619b10734c8e6b736e055b9fa64d13/src/Node/QualifiedName.php#L88

Thanks!

zobo avatar May 15 '23 12:05 zobo

I don't know- I was just looking at the test cases and I'm not sure why you can use it on some code that references that name, but not in the use statement. If it makes sense, we could change that.

roblourens avatar May 15 '23 15:05 roblourens

Hi, thanks for the feedback. Maybe it has something to do with the crazy things you can do with the insteadof and as keywords. See "Conflict resolution" in https://www.php.net/manual/en/language.oop5.traits.php .

zobo avatar May 15 '23 15:05 zobo

But then again, I can't imagine this be the reason... I tried commenting out Node\TraitUseClause to see if any of the tests in php-intellisense break and there was no problem - if something, some things I missed got fixed.

    public function getResolvedName($namespaceDefinition = null) {
        // Name resolution not applicable to constructs that define symbol names or aliases.
        if (($this->parent instanceof Node\Statement\NamespaceDefinition && $this->parent->name->getStartPosition() === $this->getStartPosition()) ||
            $this->parent instanceof Node\Statement\NamespaceUseDeclaration ||
            $this->parent instanceof Node\NamespaceUseClause ||
            $this->parent instanceof Node\NamespaceUseGroupClause ||
            /*$this->parent->parent instanceof Node\TraitUseClause ||*/
            $this->parent instanceof Node\TraitSelectOrAliasClause
        ) {
            return null;
        }

I have not yet managed to wrap my head around TraitSelectOrAliasClause where in the parsers it's being used.

zobo avatar May 15 '23 16:05 zobo

I cannot figure out why @mousetraps prohibited this. use Trait works much the same way as use Namespace - in the sense of resolving to FQN.

Even the other case of TraitSelectOrAliasClause is useless here because the following code

class TestClazz
{
	use TestTrait {
		TestTrait::test2 as testX;
	}

Gets constructed into: TraitUseClause -> TratSelectOrAliasClauseList -> TraitSelectOrAliasClause -> ScopedPropertyAccessExpression -> QualifiedName.

And such $this->parent instanceof Node\TraitSelectOrAliasClause should actually be $this->parent->parent instanceof Node\TraitSelectOrAliasClause to have any effect.

Then again it still does not make sense to have this as even here the QualifiedName can be resolved without any extra logic or context.

(unless I'm missing something)

Making a PR.

zobo avatar May 16 '23 09:05 zobo