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

Add withSetProviders method #6515

Merged
Merged
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
41 changes: 39 additions & 2 deletions src/Configuration/RectorConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Rector\Exception\Configuration\InvalidConfigurationException;
use Rector\Php\PhpVersionResolver\ComposerJsonPhpVersionResolver;
use Rector\PHPUnit\Set\PHPUnitSetList;
use Rector\Set\Contract\SetProviderInterface;
use Rector\Set\Enum\SetGroup;
use Rector\Set\SetManager;
use Rector\Set\ValueObject\DowngradeLevelSetList;
Expand Down Expand Up @@ -165,10 +166,22 @@ final class RectorConfigBuilder

private ?bool $isWithPhpLevelUsed = null;

/**
* @var array<class-string<SetProviderInterface>,bool>
*/
private array $setProviders = [];

public function __invoke(RectorConfig $rectorConfig): void
{
if ($this->setGroups !== []) {
$setManager = new SetManager(new SetProviderCollector(), new InstalledPackageResolver(getcwd()));
if ($this->setGroups !== [] || $this->setProviders !== []) {
$setProviderCollector = new SetProviderCollector(array_map(
static fn (string $setProvider): SetProviderInterface =>
$rectorConfig->make($setProvider),
\array_keys($this->setProviders)
));

$setManager = new SetManager($setProviderCollector, new InstalledPackageResolver(getcwd()));

$this->groupLoadedSets = $setManager->matchBySetGroups($this->setGroups);

SimpleParameterProvider::addParameter(Option::COMPOSER_BASED_SETS, $this->groupLoadedSets);
Expand Down Expand Up @@ -1096,4 +1109,28 @@ public function withEditorUrl(string $editorUrl): self

return $this;
}

/**
* @param class-string<SetProviderInterface> ...$setProviders
*/
public function withSetProviders(string ...$setProviders): self
{
foreach ($setProviders as $setProvider) {
if (\array_key_exists($setProvider, $this->setProviders)) {
continue;
}

if (! is_a($setProvider, SetProviderInterface::class, true)) {
throw new InvalidConfigurationException(sprintf(
'Set provider "%s" must implement "%s"',
$setProvider,
SetProviderInterface::class
));
}

$this->setProviders[$setProvider] = true;
}

return $this;
}
}