Skip to content

Commit

Permalink
[Renaming][PostRector] Handle skip path after defined at RenameClassR…
Browse files Browse the repository at this point in the history
…ector (#3243)
  • Loading branch information
samsonasik committed Jan 18, 2023
1 parent 72bd75f commit c546aa7
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 2 deletions.
17 changes: 16 additions & 1 deletion packages/PostRector/Application/PostFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Rector\Core\Logging\CurrentRectorProvider;
use Rector\Core\Provider\CurrentFileProvider;
use Rector\Core\ValueObject\Application\File;
use Rector\PostRector\Contract\Rector\PostRectorDependencyInterface;
use Rector\PostRector\Contract\Rector\PostRectorInterface;
use Rector\Skipper\Skipper\Skipper;

Expand Down Expand Up @@ -81,6 +82,20 @@ private function shouldSkipPostRector(PostRectorInterface $postRector): bool
return false;
}

return $this->skipper->shouldSkipElementAndFilePath($postRector, $file->getFilePath());
$filePath = $file->getFilePath();
if ($this->skipper->shouldSkipElementAndFilePath($postRector, $filePath)) {
return true;
}

if ($postRector instanceof PostRectorDependencyInterface) {
$dependencies = $postRector->getRectorDependencies();
foreach ($dependencies as $dependency) {
if ($this->skipper->shouldSkipElementAndFilePath($dependency, $filePath)) {
return true;
}
}
}

return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Rector\PostRector\Contract\Rector;

use Rector\Core\Contract\Rector\RectorInterface;

interface PostRectorDependencyInterface
{
/**
* @return class-string<RectorInterface>[]
*/
public function getRectorDependencies(): array;
}
13 changes: 12 additions & 1 deletion packages/PostRector/Rector/ClassRenamingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@

use PhpParser\Node;
use Rector\Core\Configuration\RenamedClassesDataCollector;
use Rector\Core\Contract\Rector\RectorInterface;
use Rector\PostRector\Contract\Rector\PostRectorDependencyInterface;
use Rector\Renaming\NodeManipulator\ClassRenamer;
use Rector\Renaming\Rector\Name\RenameClassRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

final class ClassRenamingPostRector extends AbstractPostRector
final class ClassRenamingPostRector extends AbstractPostRector implements PostRectorDependencyInterface
{
public function __construct(
private readonly ClassRenamer $classRenamer,
Expand All @@ -24,6 +27,14 @@ public function getPriority(): int
return 650;
}

/**
* @return class-string<RectorInterface>[]
*/
public function getRectorDependencies(): array
{
return [RenameClassRector::class];
}

public function enterNode(Node $node): ?Node
{
$oldToNewClasses = $this->renamedClassesDataCollector->getOldToNewClasses();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace Rector\Tests\Renaming\Rector\Name\RenameClassRector\Fixture;

class SkipAfterDefined extends \Rector\Tests\Renaming\Rector\Name\RenameClassRector\Source\OldClass
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Renaming\Rector\Name\RenameClassRector;

use Iterator;
use Rector\Renaming\Rector\Name\RenameClassRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

/**
* @see RenameClassRector
*/
final class SkipAfterDefinedTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/FixtureSkipAfterDefined');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/skip_after_defined_configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Renaming\Rector\Name\RenameClassRector;
use Rector\Tests\Renaming\Rector\Name\RenameClassRector\Source\NewClass;
use Rector\Tests\Renaming\Rector\Name\RenameClassRector\Source\OldClass;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig
->ruleWithConfiguration(RenameClassRector::class, [
OldClass::class => NewClass::class,
]);

// skip the path after defined
$rectorConfig->skip([
RenameClassRector::class => [sys_get_temp_dir() . '/rector/tests_fixture_'],
]);
};

0 comments on commit c546aa7

Please sign in to comment.