Skip to content

Commit

Permalink
[CodingStyle] Remove parent lookup on AliasUsesResolver (#4257)
Browse files Browse the repository at this point in the history
* [CodingStyle] Remove parent lookup on AliasUsesResolver

* clean up for consistency
  • Loading branch information
samsonasik authored Jun 17, 2023
1 parent 906a774 commit 8cb7e85
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
17 changes: 15 additions & 2 deletions rules/CodingStyle/ClassNameImport/AliasUsesResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,25 @@ public function __construct(
}

/**
* @param Stmt[] $stmts
* @return string[]
*/
public function resolveFromNode(Node $node): array
public function resolveFromNode(Node $node, array $stmts): array
{
if (! $node instanceof Namespace_) {
$node = $this->betterNodeFinder->findParentType($node, Namespace_::class);
/** @var Namespace_[] $namespaces */
$namespaces = array_filter($stmts, static fn (Stmt $stmt): bool => $stmt instanceof Namespace_);
foreach ($namespaces as $namespace) {
$isFoundInNamespace = (bool) $this->betterNodeFinder->findFirst(
$namespace,
static fn (Node $subNode): bool => $subNode === $node
);

if ($isFoundInNamespace) {
$node = $namespace;
break;
}
}
}

if ($node instanceof Namespace_) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct(

public function shouldSkip(File $file, FullyQualifiedObjectType $fullyQualifiedObjectType, Node $node): bool
{
$aliasedUses = $this->aliasUsesResolver->resolveFromNode($node);
$aliasedUses = $this->aliasUsesResolver->resolveFromNode($node, $file->getNewStmts());
$shortNameLowered = $fullyQualifiedObjectType->getShortNameLowered();

foreach ($aliasedUses as $aliasedUse) {
Expand Down

0 comments on commit 8cb7e85

Please sign in to comment.