Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/Application/RectorRegistry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Rector\Application;

use Rector\Contract\Rector\RectorInterface;
use Rector\Skipper\Skipper\Skipper;

/**
* @see \Rector\Tests\Application\RectorRegistryTest
*/
final class RectorRegistry
{
/**
* @param RectorInterface[] $rectors
*/
public function __construct(
private array $rectors,
private readonly Skipper $skipper
) {
}

/**
* @param RectorInterface[] $rectors
* @api used in tests to update the active rules
*
* @internal Used only in Rector core, not supported outside. Might change any time.
*/
public function refreshRectors(array $rectors): void
{
$this->rectors = $rectors;
}

/**
* @return RectorInterface[]
*
* @api used in tests
*/
public function forPath(string $filePath): array
{
$rectorsForPath = [];
foreach ($this->rectors as $rector) {
if ($this->skipper->shouldSkipRectorAndFile($rector, $filePath)) {
continue;
}

$rectorsForPath[] = $rector;
}

return $rectorsForPath;
}
}
2 changes: 2 additions & 0 deletions src/DependencyInjection/LazyContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PHPStan\Reflection\ReflectionProvider;
use Rector\Application\FileProcessor;
use Rector\Application\Provider\CurrentFileProvider;
use Rector\Application\RectorRegistry;
use Rector\BetterPhpDocParser\Comment\CommentsMerger;
use Rector\BetterPhpDocParser\Contract\BasePhpDocNodeVisitorInterface;
use Rector\BetterPhpDocParser\PhpDocNodeVisitor\ArrayTypePhpDocNodeVisitor;
Expand Down Expand Up @@ -168,6 +169,7 @@ private function registerFileProcessing(RectorConfig $rectorConfig): void
{
$rectorConfig->singleton(FileProcessor::class);
$rectorConfig->singleton(PostFileProcessor::class);
$rectorConfig->singleton(RectorRegistry::class);

// shared state: collects used skips across the skipper, the path skipper and the file processor
$rectorConfig->singleton(UsedSkipCollector::class);
Expand Down
48 changes: 48 additions & 0 deletions tests/Application/RectorRegistryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Application;

use Rector\Application\RectorRegistry;
use Rector\Configuration\Option;
use Rector\Configuration\Parameter\SimpleParameterProvider;
use Rector\Testing\PHPUnit\AbstractLazyTestCase;
use Rector\Tests\Application\Source\SkippableRector;

final class RectorRegistryTest extends AbstractLazyTestCase
{
private RectorRegistry $rectorRegistry;

protected function setUp(): void
{
parent::setUp();

SimpleParameterProvider::setParameter(Option::SKIP, [
SkippableRector::class => ['*/skipped_directory/*'],
]);

$this->rectorRegistry = $this->make(RectorRegistry::class);
$this->rectorRegistry->refreshRectors([new SkippableRector()]);
}

protected function tearDown(): void
{
SimpleParameterProvider::setParameter(Option::SKIP, []);
}

public function testForPathSkipsRectorForSkippedPath(): void
{
$rectorsForPath = $this->rectorRegistry->forPath(__DIR__ . '/skipped_directory/some_file.php');

$this->assertSame([], $rectorsForPath);
}

public function testForPathKeepsRectorForOtherPath(): void
{
$rectorsForPath = $this->rectorRegistry->forPath(__DIR__ . '/some_file.php');

$this->assertCount(1, $rectorsForPath);
$this->assertInstanceOf(SkippableRector::class, $rectorsForPath[0]);
}
}
27 changes: 27 additions & 0 deletions tests/Application/Source/SkippableRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Application\Source;

use PhpParser\Node;
use Rector\Contract\Rector\RectorInterface;
use Rector\RuleDoc\RuleDefinition;

final class SkippableRector implements RectorInterface
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Fixture rule used to assert registry path skipping', []);
}

public function getNodeTypes(): array
{
return [];
}

public function refactor(Node $node)
{
return null;
}
}
Loading