Skip to content

Commit

Permalink
[Performance] Remove findFirst on AliasUsesResolver and UsedImportsRe…
Browse files Browse the repository at this point in the history
…solver (#4259)

* [Performance] Remove findFirst on AliasUsesResolver and UsedImportsResolver

* typo fix

* typo fix
  • Loading branch information
samsonasik committed Jun 17, 2023
1 parent 8cb7e85 commit 96dc306
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 41 deletions.
22 changes: 5 additions & 17 deletions rules/CodingStyle/ClassNameImport/AliasUsesResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,11 @@
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\UseUse;
use Rector\Core\PhpParser\Node\BetterNodeFinder;

final class AliasUsesResolver
{
public function __construct(
private readonly UseImportsTraverser $useImportsTraverser,
private readonly BetterNodeFinder $betterNodeFinder
private readonly UseImportsTraverser $useImportsTraverser
) {
}

Expand All @@ -28,24 +26,14 @@ public function resolveFromNode(Node $node, array $stmts): array
if (! $node instanceof Namespace_) {
/** @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 (count($namespaces) !== 1) {
return [];
}
}

if ($node instanceof Namespace_) {
return $this->resolveFromStmts($node->stmts);
$node = current($namespaces);
}

return [];
return $this->resolveFromStmts($node->stmts);
}

/**
Expand Down
45 changes: 21 additions & 24 deletions rules/CodingStyle/ClassNameImport/UsedImportsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,6 @@ public function __construct(
) {
}

private function resolveCurrentNamespaceForNode(Node $node): ?Namespace_
{
if ($node instanceof Namespace_) {
return $node;
}

$file = $this->currentFileProvider->getFile();
if (! $file instanceof File) {
return null;
}

$stmts = $file->getNewStmts();
foreach ($stmts as $stmt) {
if ($stmt instanceof Namespace_) {
$foundNode = $this->betterNodeFinder->findFirst($stmt, static fn(Node $subNode): bool => $subNode === $node);
if ($foundNode instanceof Node) {
return $stmt;
}
}
}

return null;
}

/**
* @return array<FullyQualifiedObjectType|AliasedObjectType>
*/
Expand Down Expand Up @@ -115,6 +91,27 @@ public function resolveFunctionImportsForStmts(array $stmts): array
return $usedFunctionImports;
}

private function resolveCurrentNamespaceForNode(Node $node): ?Namespace_
{
if ($node instanceof Namespace_) {
return $node;
}

$file = $this->currentFileProvider->getFile();
if (! $file instanceof File) {
return null;
}

$stmts = $file->getNewStmts();
$namespaces = array_filter($stmts, static fn (Stmt $stmt): bool => $stmt instanceof Namespace_);

if (count($namespaces) !== 1) {
return null;
}

return current($namespaces);
}

/**
* @return array<FullyQualifiedObjectType|AliasedObjectType>
*/
Expand Down

0 comments on commit 96dc306

Please sign in to comment.