Skip to content

Commit

Permalink
remove withSetProviders() as no longer needed
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jun 26, 2024
1 parent c7be54f commit f25d46a
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 58 deletions.
2 changes: 1 addition & 1 deletion rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
naming: true,
rectorPreset: true,
// @experimental 2024-06
// twig: false,
// twig: true,
phpunitCodeQuality: true
)
->withPhpSets()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ function () {
};
CODE_SAMPLE
,
[
[
self::ONLY_DIRECT_ASSIGN => false,
]
),
Expand Down
9 changes: 7 additions & 2 deletions src/Bridge/SetProviderCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@
*/
private array $setProviders;

public function __construct()
/**
* @param SetProviderInterface[] $extraSetProviders
*/
public function __construct(array $extraSetProviders = [])
{
$this->setProviders = [
$setProviders = [
// register all known set providers here
new PHPSetProvider(),
new CoreSetProvider(),
Expand All @@ -36,6 +39,8 @@ public function __construct()
new DoctrineSetProvider(),
new TwigSetProvider(),
];

$this->setProviders = array_merge($setProviders, $extraSetProviders);
}

/**
Expand Down
31 changes: 3 additions & 28 deletions src/Configuration/RectorConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\Configuration;

use Nette\Utils\FileSystem;
use Rector\Bridge\SetProviderCollector;
use Rector\Caching\Contract\ValueObject\Storage\CacheStorageInterface;
use Rector\Config\Level\CodeQualityLevel;
use Rector\Config\Level\DeadCodeLevel;
Expand All @@ -17,11 +18,8 @@
use Rector\Contract\Rector\RectorInterface;
use Rector\Doctrine\Set\DoctrineSetList;
use Rector\Exception\Configuration\InvalidConfigurationException;
use Rector\Exception\ShouldNotHappenException;
use Rector\Php\PhpVersionResolver\ProjectComposerJsonPhpVersionResolver;
use Rector\PHPUnit\Set\PHPUnitSetList;
use Rector\PHPUnit\Set\SetProvider\PHPUnitSetProvider;
use Rector\Set\Contract\SetProviderInterface;
use Rector\Set\Enum\SetGroup;
use Rector\Set\SetManager;
use Rector\Set\ValueObject\DowngradeLevelSetList;
Expand All @@ -30,7 +28,6 @@
use Rector\Symfony\Set\FOSRestSetList;
use Rector\Symfony\Set\JMSSetList;
use Rector\Symfony\Set\SensiolabsSetList;
use Rector\Symfony\Set\SetProvider\TwigSetProvider;
use Rector\Symfony\Set\SymfonySetList;
use Rector\ValueObject\PhpVersion;
use Symfony\Component\Finder\Finder;
Expand Down Expand Up @@ -145,11 +142,6 @@ final class RectorConfigBuilder
*/
private array $registerServices = [];

/**
* @var SetProviderInterface[]
*/
private array $setProviders = [];

/**
* @var array<SetGroup::*>
*/
Expand All @@ -164,14 +156,9 @@ public function __invoke(RectorConfig $rectorConfig): void
{
// @experimental 2024-06
if ($this->setGroups !== []) {
if ($this->setProviders === []) {
throw new ShouldNotHappenException(sprintf(
'Register set providers first, as they are required for dynamic sets: "%s"',
implode('", "', $this->setGroups)
));
}
$setProviderCollector = $rectorConfig->make(SetProviderCollector::class);
$setManager = new SetManager($setProviderCollector);

$setManager = new SetManager($this->setProviders);
$this->groupLoadedSets = $setManager->matchBySetGroups($this->setGroups);
}

Expand Down Expand Up @@ -623,16 +610,6 @@ public function withPhp74Sets(): self
// there is no withPhp80Sets() and above,
// as we already use PHP 8.0 and should go with withPhpSets() instead

/**
* @param SetProviderInterface[] $setProviders
*/
public function withSetProviders(array $setProviders): self
{
$this->setProviders = array_merge($this->setProviders, $setProviders);

return $this;
}

public function withPreparedSets(
bool $deadCode = false,
bool $codeQuality = false,
Expand Down Expand Up @@ -718,12 +695,10 @@ public function withPreparedSets(
// @experimental 2024-06
if ($twig) {
$this->setGroups[] = SetGroup::TWIG;
$this->withSetProviders([new TwigSetProvider()]);
}

if ($phpunit) {
$this->setGroups[] = SetGroup::PHPUNIT;
$this->withSetProviders([new PHPUnitSetProvider()]);
}

return $this;
Expand Down
6 changes: 4 additions & 2 deletions src/Console/Command/DetectNodeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ protected function configure(): void

protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->symfonyStyle->warning('This rule is deprecated as too sensitive on correct output and unable to click through.
Use dynamic, sharedable online version https://getrector.com/ast instead.');
$this->symfonyStyle->warning(
'This rule is deprecated as too sensitive on correct output and unable to click through.
Use dynamic, sharedable online version https://getrector.com/ast instead.'
);

return self::SUCCESS;
}
Expand Down
25 changes: 9 additions & 16 deletions src/Set/SetManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,18 @@

namespace Rector\Set;

use Rector\Bridge\SetProviderCollector;
use Rector\Composer\InstalledPackageResolver;
use Rector\Set\Contract\SetProviderInterface;
use Rector\Set\ValueObject\ComposerTriggeredSet;
use Webmozart\Assert\Assert;

/**
* @see \Rector\Tests\Set\SetCollector\SetCollectorTest
* @see \Rector\Tests\Set\SetManager\SetManagerTest
*/
final readonly class SetManager
{
/**
* @param SetProviderInterface[] $setProviders
*/
public function __construct(
private array $setProviders
private SetProviderCollector $setProviderCollector
) {
Assert::allIsInstanceOf($setProviders, SetProviderInterface::class);
}

/**
Expand All @@ -30,15 +25,13 @@ public function matchComposerTriggered(string $groupName): array
{
$matchedSets = [];

foreach ($this->setProviders as $setProvider) {
foreach ($setProvider->provide() as $set) {
if (! $set instanceof ComposerTriggeredSet) {
continue;
}
foreach ($this->setProviderCollector->provideSets() as $set) {
if (! $set instanceof ComposerTriggeredSet) {
continue;
}

if ($set->getGroupName() === $groupName) {
$matchedSets[] = $set;
}
if ($set->getGroupName() === $groupName) {
$matchedSets[] = $set;
}
}

Expand Down
1 change: 1 addition & 0 deletions templates/blade/basic.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ some_function('value') }}
5 changes: 1 addition & 4 deletions templates/rector.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector;

return RectorConfig::configure()
->withPaths([
__PATHS__
])
// uncomment to reach your current PHP version
// ->withPhpSets()
->withRules([
AddVoidReturnTypeWhereNoReturnRector::class,
]);
->withTypeCoverageLevel(0);
10 changes: 7 additions & 3 deletions tests/Set/SetManager/SetManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@
namespace Rector\Tests\Set\SetManager;

use PHPUnit\Framework\TestCase;
use Rector\Bridge\SetProviderCollector;
use Rector\Set\Enum\SetGroup;
use Rector\Set\SetManager;
use Rector\Tests\Set\SetManager\Source\SomeSetProvider;

final class SetManagerTest extends TestCase
{
public function test(): void
{
$setManager = new SetManager([new SomeSetProvider()]);
$setProviderCollector = new SetProviderCollector([new SomeSetProvider()]);

$twigComposerTriggeredSet = $setManager->matchComposerTriggered('twig');
$this->assertCount(1, $twigComposerTriggeredSet);
$setManager = new SetManager($setProviderCollector);

$twigComposerTriggeredSet = $setManager->matchComposerTriggered(SetGroup::TWIG);
$this->assertGreaterThan(6, count($twigComposerTriggeredSet));
}
}
3 changes: 2 additions & 1 deletion tests/Set/SetManager/Source/SomeSetProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Rector\Set\Contract\SetInterface;
use Rector\Set\Contract\SetProviderInterface;
use Rector\Set\Enum\SetGroup;
use Rector\Set\ValueObject\ComposerTriggeredSet;
use Rector\Symfony\Set\TwigSetList;

Expand All @@ -17,7 +18,7 @@ final class SomeSetProvider implements SetProviderInterface
public function provide(): array
{
return [
new ComposerTriggeredSet('twig', 'twig/twig', '1.12', TwigSetList::TWIG_112)
new ComposerTriggeredSet(SetGroup::TWIG, 'twig/twig', '1.12', TwigSetList::TWIG_112)
];
}
}

0 comments on commit f25d46a

Please sign in to comment.