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

[DX] Add version-based set loading based on installed package version #6428

Merged
merged 3 commits into from
Nov 13, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
executionOrder="defects"
defaultTestSuite="main"
displayDetailsOnTestsThatTriggerWarnings="true"
displayDetailsOnPhpunitDeprecations="true"
>
<testsuites>
<testsuite name="main">
Expand Down
9 changes: 2 additions & 7 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
use Rector\CodingStyle\Rector\String_\UseClassKeywordForClassNameResolutionRector;
use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\ConstFetch\RemovePhpVersionIdCheckRector;
use Rector\DeadCode\Rector\Property\RemoveUnusedPrivatePropertyRector;
use Rector\Php55\Rector\String_\StringClassNameToClassConstantRector;

return RectorConfig::configure()
->withComposerBased(twig: true)
->withPreparedSets(
deadCode: true,
codeQuality: true,
Expand All @@ -20,10 +20,9 @@
earlyReturn: true,
naming: true,
rectorPreset: true,
// @experimental 2024-06
// twig: true,
phpunitCodeQuality: true
)
->withComposerBased(phpunit: true)
->withPhpSets()
->withPaths([
__DIR__ . '/bin',
Expand All @@ -41,9 +40,7 @@
StringClassNameToClassConstantRector::class,
__DIR__ . '/bin/validate-phpstan-version.php',
// tests
'*/Fixture/*',
'*/Fixture*',
'*/Source/*',
'*/Source*',
'*/Expected/*',

Expand All @@ -55,6 +52,4 @@
__DIR__ . '/src/Configuration/RectorConfigBuilder.php',
__DIR__ . '/src/Console/Notifier.php',
],

RemoveUnusedPrivatePropertyRector::class => [__DIR__ . '/src/Configuration/RectorConfigBuilder.php'],
]);
12 changes: 12 additions & 0 deletions src/Bridge/SetProviderCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Rector\Set\Contract\SetProviderInterface;
use Rector\Set\SetProvider\CoreSetProvider;
use Rector\Set\SetProvider\PHPSetProvider;
use Rector\Set\ValueObject\ComposerTriggeredSet;
use Rector\Symfony\Set\SetProvider\SymfonySetProvider;
use Rector\Symfony\Set\SetProvider\TwigSetProvider;

Expand Down Expand Up @@ -64,4 +65,15 @@ public function provideSets(): array

return $sets;
}

/**
* @return array<ComposerTriggeredSet>
*/
public function provideComposerTriggeredSets(): array
{
return array_filter(
$this->provideSets(),
fn (SetInterface $set): bool => $set instanceof ComposerTriggeredSet
);
}
}
47 changes: 32 additions & 15 deletions src/Composer/InstalledPackageResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,43 +16,60 @@
final class InstalledPackageResolver
{
/**
* @var array<string, InstalledPackage[]>
* @var InstalledPackage[]
*/
private array $resolvedInstalledPackages = [];

public function __construct(
private readonly ?string $projectDirectory = null
) {
// fallback to root project directory
if ($projectDirectory === null) {
$projectDirectory = getcwd();
}

Assert::directory($projectDirectory);
}

/**
* @return InstalledPackage[]
*/
public function resolve(string $projectDirectory): array
public function resolve(): array
{
// cache
if (isset($this->resolvedInstalledPackages[$projectDirectory])) {
return $this->resolvedInstalledPackages[$projectDirectory];
if ($this->resolvedInstalledPackages !== []) {
return $this->resolvedInstalledPackages;
}

Assert::directory($projectDirectory);

$installedPackagesFilePath = $projectDirectory . '/vendor/composer/installed.json';
$installedPackagesFilePath = $this->projectDirectory . '/vendor/composer/installed.json';
if (! file_exists($installedPackagesFilePath)) {
throw new ShouldNotHappenException(
'The installed package json not found. Make sure you run `composer update` and "vendor/composer/installed.json" file exists'
'The installed package json not found. Make sure you run `composer update` and the "vendor/composer/installed.json" file exists'
);
}

$installedPackageFileContents = FileSystem::read($installedPackagesFilePath);
$installedPackagesFilePath = Json::decode($installedPackageFileContents, true);

$installedPackages = $this->createInstalledPackages($installedPackagesFilePath['packages']);

$this->resolvedInstalledPackages = $installedPackages;

return $installedPackages;
}

/**
* @param mixed[] $packages
* @return InstalledPackage[]
*/
private function createInstalledPackages(array $packages): array
{
$installedPackages = [];

foreach ($installedPackagesFilePath['packages'] as $installedPackage) {
$installedPackages[] = new InstalledPackage(
$installedPackage['name'],
$installedPackage['version_normalized']
);
foreach ($packages as $package) {
$installedPackages[] = new InstalledPackage($package['name'], $package['version_normalized']);
}

$this->resolvedInstalledPackages[$projectDirectory] = $installedPackages;

return $installedPackages;
}
}
109 changes: 38 additions & 71 deletions src/Configuration/RectorConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Rector\Bridge\SetProviderCollector;
use Rector\Bridge\SetRectorsResolver;
use Rector\Caching\Contract\ValueObject\Storage\CacheStorageInterface;
use Rector\Composer\InstalledPackageResolver;
use Rector\Config\Level\CodeQualityLevel;
use Rector\Config\Level\DeadCodeLevel;
use Rector\Config\Level\TypeDeclarationLevel;
Expand Down Expand Up @@ -165,11 +166,8 @@ final class RectorConfigBuilder

public function __invoke(RectorConfig $rectorConfig): void
{
// @experimental 2024-06
if ($this->setGroups !== []) {
$setProviderCollector = $rectorConfig->make(SetProviderCollector::class);
$setManager = new SetManager($setProviderCollector);

$setManager = new SetManager(new SetProviderCollector(), new InstalledPackageResolver(getcwd()));
$this->groupLoadedSets = $setManager->matchBySetGroups($this->setGroups);
}

Expand Down Expand Up @@ -704,79 +702,48 @@ public function withPreparedSets(
bool $doctrineCodeQuality = false,
bool $symfonyCodeQuality = false,
bool $symfonyConfigs = false,
// composer based
bool $twig = false,
bool $phpunit = false,
): self {
Notifier::notifyNotSuitableMethodForPHP74(__METHOD__);

if ($deadCode) {
$this->sets[] = SetList::DEAD_CODE;
}

if ($codeQuality) {
$this->sets[] = SetList::CODE_QUALITY;
}

if ($codingStyle) {
$this->sets[] = SetList::CODING_STYLE;
}

if ($typeDeclarations) {
$this->sets[] = SetList::TYPE_DECLARATION;
}

if ($privatization) {
$this->sets[] = SetList::PRIVATIZATION;
}

if ($naming) {
$this->sets[] = SetList::NAMING;
}

if ($instanceOf) {
$this->sets[] = SetList::INSTANCEOF;
}

if ($earlyReturn) {
$this->sets[] = SetList::EARLY_RETURN;
}

if ($strictBooleans) {
$this->sets[] = SetList::STRICT_BOOLEANS;
}

if ($carbon) {
$this->sets[] = SetList::CARBON;
}

if ($rectorPreset) {
$this->sets[] = SetList::RECTOR_PRESET;
}

if ($phpunitCodeQuality) {
$this->sets[] = PHPUnitSetList::PHPUNIT_CODE_QUALITY;
}

if ($doctrineCodeQuality) {
$this->sets[] = DoctrineSetList::DOCTRINE_CODE_QUALITY;
}

if ($symfonyCodeQuality) {
$this->sets[] = SymfonySetList::SYMFONY_CODE_QUALITY;
}

if ($symfonyConfigs) {
$this->sets[] = SymfonySetList::CONFIGS;
$setMap = [
$deadCode => SetList::DEAD_CODE,
$codeQuality => SetList::CODE_QUALITY,
$codingStyle => SetList::CODING_STYLE,
$typeDeclarations => SetList::TYPE_DECLARATION,
$privatization => SetList::PRIVATIZATION,
$naming => SetList::NAMING,
$instanceOf => SetList::INSTANCEOF,
$earlyReturn => SetList::EARLY_RETURN,
$strictBooleans => SetList::STRICT_BOOLEANS,
$carbon => SetList::CARBON,
$rectorPreset => SetList::RECTOR_PRESET,
$phpunitCodeQuality => PHPUnitSetList::PHPUNIT_CODE_QUALITY,
$doctrineCodeQuality => DoctrineSetList::DOCTRINE_CODE_QUALITY,
$symfonyCodeQuality => SymfonySetList::SYMFONY_CODE_QUALITY,
$symfonyConfigs => SymfonySetList::CONFIGS,
];

foreach ($setMap as $isEnabled => $setPath) {
if ($isEnabled) {
$this->sets[] = $setPath;
}
}

// @experimental 2024-06
if ($twig) {
$this->setGroups[] = SetGroup::TWIG;
}
return $this;
}

if ($phpunit) {
$this->setGroups[] = SetGroup::PHPUNIT;
public function withComposerBased(bool $twig = false, bool $doctrine = false, bool $phpunit = false): self
{
$setMap = [
$twig => SetGroup::TWIG,
$doctrine => SetGroup::DOCTRINE,
$phpunit => SetGroup::PHPUNIT,
];

foreach ($setMap as $isEnabled => $setPath) {
if ($isEnabled) {
$this->setGroups[] = $setPath;
}
}

return $this;
Expand Down
10 changes: 10 additions & 0 deletions src/Set/Enum/SetGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,35 @@ final class SetGroup
public const PHP = 'php';

/**
* Version-based set provider
* @var string
*/
public const TWIG = 'twig';

/**
* Version-based set provider
* @var string
*/
public const PHPUNIT = 'phpunit';

/**
* Version-based set provider
* @var string
*/
public const DOCTRINE = 'doctrine';

/**
* Version-based set provider
* @var string
*/
public const SYMFONY = 'symfony';

/**
* Version-based set provider
* @var string
*/
public const LARAVEL = 'laravel';

/**
* @var string
*/
Expand Down
25 changes: 9 additions & 16 deletions src/Set/SetManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Rector\Bridge\SetProviderCollector;
use Rector\Composer\InstalledPackageResolver;
use Rector\Set\Enum\SetGroup;
use Rector\Set\ValueObject\ComposerTriggeredSet;

/**
Expand All @@ -14,7 +15,8 @@
final readonly class SetManager
{
public function __construct(
private SetProviderCollector $setProviderCollector
private SetProviderCollector $setProviderCollector,
private InstalledPackageResolver $installedPackageResolver,
) {
}

Expand All @@ -25,40 +27,31 @@ public function matchComposerTriggered(string $groupName): array
{
$matchedSets = [];

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

if ($set->getGroupName() === $groupName) {
$matchedSets[] = $set;
foreach ($this->setProviderCollector->provideComposerTriggeredSets() as $composerTriggeredSet) {
if ($composerTriggeredSet->getGroupName() === $groupName) {
$matchedSets[] = $composerTriggeredSet;
}
}

return $matchedSets;
}

/**
* @param string[] $setGroups
* @param SetGroup::*[] $setGroups
* @return string[]
*/
public function matchBySetGroups(array $setGroups): array
{
$installedPackageResolver = new InstalledPackageResolver();
$installedComposerPackages = $installedPackageResolver->resolve(getcwd());

$installedComposerPackages = $this->installedPackageResolver->resolve();
$groupLoadedSets = [];

foreach ($setGroups as $setGroup) {
$composerTriggeredSets = $this->matchComposerTriggered($setGroup);

foreach ($composerTriggeredSets as $composerTriggeredSet) {
if ($composerTriggeredSet->matchInstalledPackages($installedComposerPackages)) {
// @todo add debug note somewhere
// echo sprintf('Loaded "%s" set as it meets the conditions', $composerTriggeredSet->getSetFilePath());

// it matched composer package + version requirements → load set
$groupLoadedSets[] = $composerTriggeredSet->getSetFilePath();
$groupLoadedSets[] = realpath($composerTriggeredSet->getSetFilePath());
}
}
}
Expand Down
Loading