Skip to content

Commit

Permalink
[DX] Try removing resolved name attribute, as namespacedName is alrea…
Browse files Browse the repository at this point in the history
…dy part of named nodes (#3921)
  • Loading branch information
TomasVotruba committed May 22, 2023
1 parent 44735d3 commit d90c645
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\Analyser\Scope;
use Rector\NodeNameResolver\Contract\NodeNameResolverInterface;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand Down Expand Up @@ -38,11 +37,6 @@ public function resolve(Node $node, ?Scope $scope): ?string
return $this->funcCallNameResolver->resolve($parentNode, $scope);
}

$resolvedName = $node->getAttribute(AttributeKey::RESOLVED_NAME);
if ($resolvedName instanceof FullyQualified) {
return $resolvedName->toString();
}

return $node->toString();
}
}
3 changes: 3 additions & 0 deletions packages/NodeTypeResolver/Node/AttributeKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ final class AttributeKey
public const ORIGINAL_NAME = 'originalName';

/**
* @api
* @deprecated Use $node->namespacedName instead
*
* Internal php-parser name. @see \PhpParser\NodeVisitor\NameResolver
* Do not change this even if you want!
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Contract\NodeTypeResolverInterface;
use Rector\NodeTypeResolver\Node\AttributeKey;

/**
* @see \Rector\Tests\NodeTypeResolver\PerNodeTypeResolver\NameTypeResolver\NameTypeResolverTest
Expand Down Expand Up @@ -108,12 +107,6 @@ private function resolveFullyQualifiedName(Name $name): string
return (string) $this->nodeNameResolver->getName($classLike);
}

/** @var Name|null $resolvedNameNode */
$resolvedNameNode = $name->getAttribute(AttributeKey::RESOLVED_NAME);
if ($resolvedNameNode instanceof Name) {
return $resolvedNameNode->toString();
}

return $nameValue;
}
}
2 changes: 1 addition & 1 deletion src/Kernel/RectorKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class RectorKernel
/**
* @var string
*/
private const CACHE_KEY = 'v18';
private const CACHE_KEY = 'v19';

private ContainerInterface|null $container = null;

Expand Down
17 changes: 1 addition & 16 deletions src/PhpParser/Node/NodeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,11 @@
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTagNode;
use PHPStan\Type\Type;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\Core\Configuration\CurrentNodeProvider;
use Rector\Core\Enum\ObjectReference;
use Rector\Core\Exception\NotImplementedYetException;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\NodeDecorator\PropertyTypeDecorator;
use Rector\Core\ValueObject\MethodName;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PHPStanStaticTypeMapper\Enum\TypeKind;
use Rector\PostRector\ValueObject\PropertyMetadata;
use Rector\StaticTypeMapper\StaticTypeMapper;
Expand All @@ -66,7 +64,6 @@ public function __construct(
private readonly BuilderFactory $builderFactory,
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly StaticTypeMapper $staticTypeMapper,
private readonly CurrentNodeProvider $currentNodeProvider,
private readonly PropertyTypeDecorator $propertyTypeDecorator
) {
}
Expand Down Expand Up @@ -348,19 +345,7 @@ public function createTrue(): ConstFetch
*/
public function createClassConstFetchFromName(Name $className, string $constantName): ClassConstFetch
{
$classConstFetch = $this->builderFactory->classConstFetch($className, $constantName);

$classNameString = $className->toString();
if (in_array($classNameString, [ObjectReference::SELF, ObjectReference::STATIC], true)) {
$currentNode = $this->currentNodeProvider->getNode();
if ($currentNode instanceof Node) {
$classConstFetch->class->setAttribute(AttributeKey::RESOLVED_NAME, $className);
}
} else {
$classConstFetch->class->setAttribute(AttributeKey::RESOLVED_NAME, $classNameString);
}

return $classConstFetch;
return $this->builderFactory->classConstFetch($className, $constantName);
}

/**
Expand Down
9 changes: 1 addition & 8 deletions tests/PhpParser/Node/Value/ValueResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
use PhpParser\BuilderFactory;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\Plus;
use PhpParser\Node\Name\FullyQualified;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Core\PhpParser\Node\Value\ValueResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Testing\PHPUnit\AbstractTestCase;

final class ValueResolverTest extends AbstractTestCase
Expand Down Expand Up @@ -39,12 +37,7 @@ public static function dataProvider(): Iterator
$builderFactory = new BuilderFactory();

$classConstFetchNode = $builderFactory->classConstFetch('SomeClass', 'SOME_CONSTANT');
$classConstFetchNode->class->setAttribute(
AttributeKey::RESOLVED_NAME,
new FullyQualified('SomeClassResolveName')
);

yield [$classConstFetchNode, 'SomeClassResolveName::SOME_CONSTANT'];
yield [$classConstFetchNode, 'SomeClass::SOME_CONSTANT'];
yield [$builderFactory->val(true), true];
yield [$builderFactory->val(1), 1];
yield [$builderFactory->val(1.0), 1.0];
Expand Down

0 comments on commit d90c645

Please sign in to comment.