Skip to content

Commit

Permalink
[CodingStyle] Reduce parent attribute usage on NameImporter (#4344)
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Jun 25, 2023
1 parent dfef34d commit 53e4e9f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
10 changes: 10 additions & 0 deletions packages/NodeTypeResolver/Node/AttributeKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,4 +266,14 @@ final class AttributeKey
* @var string
*/
public const IS_STATICCALL_CLASS_NAME = 'is_staticcall_class_name';

/**
* @var string
*/
public const IS_FUNCCALL_NAME = 'is_funccall_name';

/**
* @var string
*/
public const IS_CONSTFETCH_NAME = 'is_constfetch_name';
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor;

use PhpParser\Node;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Namespace_;
Expand All @@ -28,6 +30,16 @@ public function enterNode(Node $node): ?Node
return null;
}

if ($node instanceof FuncCall && $node->name instanceof Name) {
$node->name->setAttribute(AttributeKey::IS_FUNCCALL_NAME, true);
return null;
}

if ($node instanceof ConstFetch) {
$node->name->setAttribute(AttributeKey::IS_CONSTFETCH_NAME, true);
return null;
}

if (!$node instanceof StaticCall) {
return null;
}
Expand Down
14 changes: 5 additions & 9 deletions rules/CodingStyle/Node/NameImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@
namespace Rector\CodingStyle\Node;

use PhpParser\Node;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\GroupUse;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Use_;
use PhpParser\Node\Stmt\UseUse;
use PHPStan\Reflection\ReflectionProvider;
Expand Down Expand Up @@ -142,11 +139,11 @@ private function importNameAndCollectNewUseStatement(
*/
private function isNamespaceOrUseImportName(Name $name): bool
{
$parentNode = $name->getAttribute(AttributeKey::PARENT_NODE);
if ($parentNode instanceof Namespace_) {
if ($name->getAttribute(AttributeKey::IS_NAMESPACE_NAME) === true) {
return true;
}

$parentNode = $name->getAttribute(AttributeKey::PARENT_NODE);
return $parentNode instanceof UseUse;
}

Expand All @@ -164,11 +161,11 @@ private function isFunctionOrConstantImportWithSingleName(Name $name): bool
return true;
}

if ($parentNode instanceof ConstFetch) {
if ($name->getAttribute(AttributeKey::IS_CONSTFETCH_NAME) === true) {
return count($name->getParts()) === 1;
}

if ($parentNode instanceof FuncCall) {
if ($name->getAttribute(AttributeKey::IS_FUNCCALL_NAME) === true) {
return count($name->getParts()) === 1;
}

Expand All @@ -181,8 +178,7 @@ private function addUseImport(File $file, Name $name, FullyQualifiedObjectType $
return;
}

$parentNode = $name->getAttribute(AttributeKey::PARENT_NODE);
if ($parentNode instanceof FuncCall) {
if ($name->getAttribute(AttributeKey::IS_FUNCCALL_NAME) === true) {
$this->useNodesToAddCollector->addFunctionUseImport($fullyQualifiedObjectType);
} else {
$this->useNodesToAddCollector->addUseImport($fullyQualifiedObjectType);
Expand Down

0 comments on commit 53e4e9f

Please sign in to comment.