Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Renaming][PostRector] Handle skip path after defined at RenameClassRector #3243

Merged
merged 3 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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_'],
]);
};