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

refactor: add support for Custom Skip Voters #5002

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
14 changes: 5 additions & 9 deletions packages/Skipper/Skipper/Skipper.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
use Rector\Core\Contract\Rector\RectorInterface;
use Rector\Core\ProcessAnalyzer\RectifiedAnalyzer;
use Rector\Skipper\Contract\SkipVoterInterface;
use Rector\Skipper\SkipVoter\ClassSkipVoter;
use Rector\Skipper\SkipVoter\PathSkipVoter;
use Webmozart\Assert\Assert;

/**
* @api
Expand All @@ -23,16 +22,13 @@ final class Skipper
private const FILE_ELEMENT = 'file_elements';

/**
* @var SkipVoterInterface[]
* @param array<SkipVoterInterface> $skipVoters
*/
private array $skipVoters = [];

public function __construct(
ClassSkipVoter $classSkipVoter,
PathSkipVoter $pathSkipVoter,
private readonly RectifiedAnalyzer $rectifiedAnalyzer
private readonly RectifiedAnalyzer $rectifiedAnalyzer,
private readonly array $skipVoters,
) {
$this->skipVoters = [$classSkipVoter, $pathSkipVoter];
Assert::allIsInstanceOf($this->skipVoters, SkipVoterInterface::class);
}

public function shouldSkipElement(string | object $element): bool
Expand Down
14 changes: 14 additions & 0 deletions src/DependencyInjection/LazyContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,10 @@
use Rector\PHPStanStaticTypeMapper\TypeMapper\TypeWithClassNameTypeMapper;
use Rector\PHPStanStaticTypeMapper\TypeMapper\VoidTypeMapper;
use Rector\PostRector\Application\PostFileProcessor;
use Rector\Skipper\Contract\SkipVoterInterface;
use Rector\Skipper\Skipper\Skipper;
use Rector\Skipper\SkipVoter\ClassSkipVoter;
use Rector\Skipper\SkipVoter\PathSkipVoter;
use Rector\StaticTypeMapper\Contract\PhpDocParser\PhpDocTypeMapperInterface;
use Rector\StaticTypeMapper\Contract\PhpParser\PhpParserNodeMapperInterface;
use Rector\StaticTypeMapper\Mapper\PhpParserNodeMapper;
Expand Down Expand Up @@ -358,6 +361,11 @@ final class LazyContainerFactory
UnionTypeNodeMapper::class,
];

/**
* @var array<class-string<SkipVoterInterface>>
*/
private const SKIP_VOTER_CLASSES = [ClassSkipVoter::class, PathSkipVoter::class];
samsonasik marked this conversation as resolved.
Show resolved Hide resolved

/**
* @api used as next rectorConfig factory
*/
Expand Down Expand Up @@ -493,6 +501,12 @@ static function (
->needs('$nodeNameResolvers')
->giveTagged(NodeNameResolverInterface::class);

$rectorConfig->when(Skipper::class)
->needs('$skipVoters')
->giveTagged(SkipVoterInterface::class);

$this->registerTagged($rectorConfig, self::SKIP_VOTER_CLASSES, SkipVoterInterface::class);

$rectorConfig->afterResolving(
AbstractRector::class,
static function (AbstractRector $rector, Container $container): void {
Expand Down