Skip to content

Commit

Permalink
[Performance] Cache should traverse for AddUseStatementGuard (#6234)
Browse files Browse the repository at this point in the history
* [Performance] Cache should traverse for AddUseStatementGuard

* grammar

* more clarification comment
  • Loading branch information
samsonasik committed Aug 22, 2024
1 parent 4f89b74 commit 8aaee5e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
6 changes: 4 additions & 2 deletions src/PostRector/Application/PostFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ public function reset(): void
public function traverse(array $stmts, File $file): array
{
foreach ($this->getPostRectors() as $postRector) {
// file must be set early into PostRector class to ensure its usage
// always match on skipping process
$postRector->setFile($file);

if ($this->shouldSkipPostRector($postRector, $file->getFilePath(), $stmts)) {
continue;
}

$postRector->setFile($file);

$nodeTraverser = new NodeTraverser();
$nodeTraverser->addVisitor($postRector);
$stmts = $nodeTraverser->traverse($stmts);
Expand Down
15 changes: 12 additions & 3 deletions src/PostRector/Guard/AddUseStatementGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

class AddUseStatementGuard
{
/**
* @var array<string, bool>
*/
private array $shouldTraverseOnFiles = [];

public function __construct(
private readonly BetterNodeFinder $betterNodeFinder
) {
Expand All @@ -19,8 +24,12 @@ public function __construct(
/**
* @param Stmt[] $stmts
*/
public function shouldTraverse(array $stmts): bool
public function shouldTraverse(array $stmts, string $filePath): bool
{
if (isset($this->shouldTraverseOnFiles[$filePath])) {
return $this->shouldTraverseOnFiles[$filePath];
}

$totalNamespaces = 0;

// just loop the first level stmts to locate namespace to improve performance
Expand All @@ -32,10 +41,10 @@ public function shouldTraverse(array $stmts): bool

// skip if 2 namespaces are present
if ($totalNamespaces === 2) {
return false;
return $this->shouldTraverseOnFiles[$filePath] = false;
}
}

return ! $this->betterNodeFinder->hasInstancesOf($stmts, [InlineHTML::class]);
return $this->shouldTraverseOnFiles[$filePath] = ! $this->betterNodeFinder->hasInstancesOf($stmts, [InlineHTML::class]);
}
}
2 changes: 1 addition & 1 deletion src/PostRector/Rector/DocblockNameImportingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ public function enterNode(Node $node): Node|int|null
*/
public function shouldTraverse(array $stmts): bool
{
return $this->addUseStatementGuard->shouldTraverse($stmts);
return $this->addUseStatementGuard->shouldTraverse($stmts, $this->getFile()->getFilePath());
}
}
2 changes: 1 addition & 1 deletion src/PostRector/Rector/NameImportingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function enterNode(Node $node): Node|int|null
*/
public function shouldTraverse(array $stmts): bool
{
return $this->addUseStatementGuard->shouldTraverse($stmts);
return $this->addUseStatementGuard->shouldTraverse($stmts, $this->getFile()->getFilePath());
}

/**
Expand Down

0 comments on commit 8aaee5e

Please sign in to comment.