Skip to content

Commit

Permalink
Add withSetProviders method (#6515)
Browse files Browse the repository at this point in the history
* Add `withSetProviders` method

Signed-off-by: Nathanael Esayeas <[email protected]>

* Prevent duplicate set providers

Signed-off-by: Nathanael Esayeas <[email protected]>

---------

Signed-off-by: Nathanael Esayeas <[email protected]>
  • Loading branch information
ghostwriter authored Jan 11, 2025
1 parent 9d81eba commit 0a6eb12
Showing 1 changed file with 39 additions and 2 deletions.
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 @@ -162,10 +163,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 @@ -1112,4 +1125,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;
}
}

0 comments on commit 0a6eb12

Please sign in to comment.