Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/PhpParser/NodeTraverser/RectorNodeTraverser.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
use PhpParser\Node\Stmt;
use PhpParser\NodeTraverserInterface;
use PhpParser\NodeVisitor;
use Rector\Application\Provider\CurrentFileProvider;
use Rector\Configuration\ConfigurationRuleFilter;
use Rector\Contract\Rector\RectorInterface;
use Rector\Exception\ShouldNotHappenException;
use Rector\Rector\RectorRunner;
use Rector\ValueObject\Application\File;
use Rector\VersionBonding\ComposerPackageConstraintFilter;
use Rector\VersionBonding\PhpVersionedFilter;
use Webmozart\Assert\Assert;
Expand All @@ -40,6 +42,8 @@ final class RectorNodeTraverser implements NodeTraverserInterface

private bool $areNodeVisitorsPrepared = false;

private File $file;

/**
* @var array<class-string<Node>, RectorInterface[]>
*/
Expand All @@ -54,6 +58,7 @@ public function __construct(
private readonly ComposerPackageConstraintFilter $composerPackageConstraintFilter,
private readonly ConfigurationRuleFilter $configurationRuleFilter,
private readonly RectorRunner $rectorRunner,
private readonly CurrentFileProvider $currentFileProvider,
) {
}

Expand All @@ -75,6 +80,14 @@ public function traverse(array $nodes): array
{
$this->prepareNodeVisitors();

$file = $this->currentFileProvider->getFile();
if (! $file instanceof File) {
throw new ShouldNotHappenException(
'File object is missing. Make sure you call $this->currentFileProvider->setFile(...) before traversing.'
);
}

$this->file = $file;
$this->stopTraversal = false;

return $this->traverseArray($nodes);
Expand Down Expand Up @@ -146,7 +159,7 @@ private function traverseNode(Node $node): void
$currentNodeVisitors = $this->getVisitorsForNode($subNode);

foreach ($currentNodeVisitors as $currentNodeVisitor) {
$return = $this->rectorRunner->run($currentNodeVisitor, $subNode);
$return = $this->rectorRunner->run($currentNodeVisitor, $subNode, $this->file);
if ($return !== null) {
if ($return instanceof Node) {
$originalSubNodeClass = $subNode::class;
Expand Down Expand Up @@ -206,7 +219,7 @@ private function traverseArray(array $nodes): array
$currentNodeVisitors = $this->getVisitorsForNode($node);

foreach ($currentNodeVisitors as $currentNodeVisitor) {
$return = $this->rectorRunner->run($currentNodeVisitor, $node);
$return = $this->rectorRunner->run($currentNodeVisitor, $node, $this->file);
if ($return !== null) {
if ($return instanceof Node) {
$originalNodeNodeClass = $node::class;
Expand Down
18 changes: 1 addition & 17 deletions src/Rector/RectorRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use PhpParser\NodeVisitor\CloningVisitor;
use PHPStan\Analyser\MutatingScope;
use Rector\Application\ChangedNodeScopeRefresher;
use Rector\Application\Provider\CurrentFileProvider;
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
use Rector\Contract\Rector\HTMLAverseRectorInterface;
use Rector\Contract\Rector\RectorInterface;
Expand Down Expand Up @@ -46,17 +45,14 @@ public function __construct(
private Skipper $skipper,
private CreatedByRuleDecorator $createdByRuleDecorator,
private ChangedNodeScopeRefresher $changedNodeScopeRefresher,
private CurrentFileProvider $currentFileProvider,
) {
}

/**
* @return int|Node|Node[]|null
*/
public function run(RectorInterface $rector, Node $node): int|Node|null|array
public function run(RectorInterface $rector, Node $node, File $file): int|Node|null|array
{
$file = $this->getFile();

if ($rector instanceof HTMLAverseRectorInterface && $file->containsHTML()) {
return null;
}
Expand Down Expand Up @@ -114,18 +110,6 @@ public function run(RectorInterface $rector, Node $node): int|Node|null|array
return $this->postRefactorProcess($rector, $file, $originalNode, $node, $refactoredNodeOrState, $filePath);
}

private function getFile(): File
{
$file = $this->currentFileProvider->getFile();
if (! $file instanceof File) {
throw new ShouldNotHappenException(
'File object is missing. Make sure you call $this->currentFileProvider->setFile(...) before traversing.'
);
}

return $file;
}

/**
* Deep clone, so a skipped rule can be probed on the clone without mutating the real node.
*/
Expand Down
7 changes: 2 additions & 5 deletions tests/Rector/RectorRunner/RectorRunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Rector\Tests\Rector\RectorRunner;

use PhpParser\Node\Stmt\Class_;
use Rector\Application\Provider\CurrentFileProvider;
use Rector\Rector\RectorRunner;
use Rector\Testing\PHPUnit\AbstractLazyTestCase;
use Rector\Tests\Rector\RectorRunner\Source\ReturnNullRector;
Expand All @@ -23,14 +22,12 @@ protected function setUp(): void
parent::setUp();

$this->rectorRunner = $this->make(RectorRunner::class);

$currentFileProvider = $this->make(CurrentFileProvider::class);
$currentFileProvider->setFile(new File('some_file.php', '<?php'));
}

public function testReturnsNullWhenRuleMakesNoChange(): void
{
$return = $this->rectorRunner->run(new ReturnNullRector(), new Class_('SomeClass'));
$file = new File('some_file.php', '<?php');
$return = $this->rectorRunner->run(new ReturnNullRector(), new Class_('SomeClass'), $file);

$this->assertNull($return);
}
Expand Down
Loading