Skip to content

Commit

Permalink
Ease finding slow files (#3785)
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed May 10, 2023
1 parent 388763c commit d871136
Showing 1 changed file with 37 additions and 6 deletions.
43 changes: 37 additions & 6 deletions src/Rector/AbstractRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PhpParser\Node\Stmt\Nop;
use PhpParser\NodeVisitorAbstract;
use PHPStan\Analyser\MutatingScope;
use PHPStan\Internal\BytesHelper;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
Expand Down Expand Up @@ -197,19 +198,32 @@ final public function enterNode(Node $node)
return null;
}

$isDebug = $this->rectorOutputStyle->isDebug();

$this->currentRectorProvider->changeCurrentRector($this);
// for PHP doc info factory and change notifier
$this->currentNodeProvider->setNode($node);

$this->printDebugCurrentFileAndRule();
if ($isDebug) {
$this->printCurrentFileAndRule();
}

$originalNode = $node->getAttribute(AttributeKey::ORIGINAL_NODE);
if ($originalNode instanceof Node) {
$this->changedNodeScopeRefresher->reIndexNodeAttributes($node);
}

if ($isDebug) {
$startTime = microtime(true);
$previousMemory = memory_get_peak_usage(true);
}

$refactoredNode = $this->refactor($node);

if ($isDebug) {
$this->printConsumptions($startTime, $previousMemory);
}

// nothing to change or just removed via removeNode() → continue
if ($refactoredNode === null) {
return null;
Expand All @@ -220,6 +234,14 @@ final public function enterNode(Node $node)
throw new ShouldNotHappenException($errorMessage);
}

return $this->postRefactorProcess($originalNode, $node, $refactoredNode);
}

/**
* @param Node|Node[] $refactoredNode
*/
private function postRefactorProcess(Node|null $originalNode, Node $node, Node|array $refactoredNode): Node
{
$originalNode ??= $node;

/** @var non-empty-array<Node>|Node $refactoredNode */
Expand Down Expand Up @@ -441,16 +463,25 @@ private function connectNodes(array $nodes, Node $node): void
$this->nodeConnectingTraverser->traverse($nodes);
}

private function printDebugCurrentFileAndRule(): void
private function printCurrentFileAndRule(): void
{
if (! $this->rectorOutputStyle->isDebug()) {
return;
}

$relativeFilePath = $this->filePathHelper->relativePath($this->file->getFilePath());

$this->rectorOutputStyle->writeln('[file] ' . $relativeFilePath);
$this->rectorOutputStyle->writeln('[rule] ' . static::class);
}

private function printConsumptions(float $startTime, int $previousMemory): void
{
$elapsedTime = microtime(true) - $startTime;
$currentTotalMemory = memory_get_peak_usage(true);

$consumed = sprintf(
'--- consumed %s, total %s, took %.2f s',
BytesHelper::bytes($currentTotalMemory - $previousMemory),
BytesHelper::bytes($currentTotalMemory), $elapsedTime
);
$this->rectorOutputStyle->writeln($consumed);
$this->rectorOutputStyle->newLine(1);
}
}

0 comments on commit d871136

Please sign in to comment.