Skip to content

Commit

Permalink
refactor: add support for Custom Skip Voters (#5002)
Browse files Browse the repository at this point in the history
Co-authored-by: Tomas Votruba <tomas.vot@gmail.com>
Co-authored-by: bernd.kaczenski <bernd.kaczenski@allmyhomes.com>
  • Loading branch information
3 people authored Sep 12, 2023
1 parent ffa43a1 commit ef003b2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
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];

/**
* @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

0 comments on commit ef003b2

Please sign in to comment.