Skip to content

Commit e936ba0

Browse files
committed
Collapse per-version PHP sets into single php-version-based.php, drop PhpLevelSetResolver
withPhpSets() and withPhpLevel() now load one config/set/php-version-based.php set that lists every PHP version rule. The runtime PhpVersionedFilter already gates each rule by MinPhpVersionInterface, so cumulative per-version file selection is redundant - PhpLevelSetResolver is removed. An explicitly picked withPhpSets(phpXX: true) version still caps the set: the ceiling now applies to all rules in PhpVersionedFilter (previously only to polyfilled ones, the rest were capped by file selection), preserving behavior. Claude-Session: https://claude.ai/code/session_014Q25TQ4b7b13UQQXScu3Fg
1 parent e264a53 commit e936ba0

7 files changed

Lines changed: 67 additions & 129 deletions

File tree

‎config/set/php-version-based.php‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
7+
// all PHP version rules, oldest to newest; each rule gates itself at runtime
8+
// via MinPhpVersionInterface, so only rules up to the target PHP version apply
9+
return static function (RectorConfig $rectorConfig): void {
10+
$rectorConfig->sets([
11+
__DIR__ . '/php52.php',
12+
__DIR__ . '/php53.php',
13+
__DIR__ . '/php54.php',
14+
__DIR__ . '/php55.php',
15+
__DIR__ . '/php56.php',
16+
__DIR__ . '/php70.php',
17+
__DIR__ . '/php71.php',
18+
__DIR__ . '/php72.php',
19+
__DIR__ . '/php73.php',
20+
__DIR__ . '/php74.php',
21+
__DIR__ . '/php80.php',
22+
__DIR__ . '/php81.php',
23+
__DIR__ . '/php82.php',
24+
__DIR__ . '/php83.php',
25+
__DIR__ . '/php84.php',
26+
__DIR__ . '/php85.php',
27+
__DIR__ . '/php86.php',
28+
]);
29+
};

‎phpstan.neon‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,11 @@ parameters:
7676
message: '#EARLY_RETURN of class Rector\\Set\\ValueObject\\SetList#'
7777
path: src/Configuration/RectorConfigBuilder.php
7878

79-
# the deprecated SetList::PHP_* constants are still resolved internally, until withPhpSets()/withPhpLevel() replace them
79+
# the deprecated SetList::PHP_* constants are still resolved internally by the level sets and their tests
8080
-
8181
identifier: classConstant.deprecated
8282
paths:
83-
- src/Configuration/PhpLevelSetResolver.php
8483
- config/set/level
85-
- tests/Configuration/PhpLevelSetResolverTest.php
8684
- tests/Bridge/SetRectorsResolverTest.php
8785

8886
# the deprecated github/gitlab output formatters are still tested until removed in next minor version

‎src/Configuration/PhpLevelSetResolver.php‎

Lines changed: 0 additions & 59 deletions
This file was deleted.

‎src/Configuration/RectorConfigBuilder.php‎

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,11 @@ final class RectorConfigBuilder
177177

178178
private ?string $editorUrl = null;
179179

180+
/**
181+
* All PHP version rules in one set; each rule gates itself by PHP version at runtime
182+
*/
183+
private const string PHP_VERSION_BASED_SET = __DIR__ . '/../../config/set/php-version-based.php';
184+
180185
private ?bool $isWithPhpSetsUsed = null;
181186

182187
private ?bool $isWithPhpLevelUsed = null;
@@ -562,15 +567,16 @@ public function withPhpSets(
562567
);
563568
}
564569

565-
// no version picked, resolve it from the project composer.json
570+
// no version picked, target the project composer.json PHP version; validated here, gated at runtime
566571
if ($pickedPhpVersions === []) {
567-
return $this->addPhpLevelSets(ComposerJsonPhpVersionResolver::resolveFromCwdOrFail());
572+
ComposerJsonPhpVersionResolver::resolveFromCwdOrFail();
573+
return $this->addPhpLevelSets();
568574
}
569575

570576
// explicitly picked version is a ceiling, even for polyfilled rules
571577
$this->pickedPhpSetsVersion = $pickedPhpVersions[0];
572578

573-
return $this->addPhpLevelSets($pickedPhpVersions[0]);
579+
return $this->addPhpLevelSets();
574580
}
575581

576582
#[Deprecated(message: 'Use "withPhpLevel()" instead, it raises PHP level one rule at a time.')]
@@ -984,12 +990,10 @@ public function withPhpLevel(int $level): self
984990

985991
$this->isWithPhpLevelUsed = true;
986992

987-
$phpVersion = ComposerJsonPhpVersionResolver::resolveFromCwdOrFail();
988-
989993
$setRectorsResolver = new SetRectorsResolver();
990-
$setFilePaths = PhpLevelSetResolver::resolveFromPhpVersion($phpVersion);
991-
992-
$rectorRulesWithConfiguration = $setRectorsResolver->resolveFromFilePathsIncludingConfiguration($setFilePaths);
994+
$rectorRulesWithConfiguration = $setRectorsResolver->resolveFromFilePathIncludingConfiguration(
995+
self::PHP_VERSION_BASED_SET
996+
);
993997

994998
foreach ($rectorRulesWithConfiguration as $position => $rectorRuleWithConfiguration) {
995999
// add rules until level is reached
@@ -1159,14 +1163,11 @@ public function withSetProviders(): self
11591163
return $this;
11601164
}
11611165

1162-
/**
1163-
* @param PhpVersion::* $phpVersion
1164-
*/
1165-
private function addPhpLevelSets(int $phpVersion): self
1166+
private function addPhpLevelSets(): self
11661167
{
11671168
$this->isWithPhpSetsUsed = true;
11681169

1169-
$this->sets = array_merge($this->sets, PhpLevelSetResolver::resolveFromPhpVersion($phpVersion));
1170+
$this->sets[] = self::PHP_VERSION_BASED_SET;
11701171

11711172
return $this;
11721173
}

‎src/VersionBonding/PhpVersionedFilter.php‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,15 @@ public function filter(array $rectors): array
5050
continue;
5151
}
5252

53-
$maxPhpVersion = $rector instanceof RelatedPolyfillInterface && $ceilingPhpVersion !== null
54-
? $ceilingPhpVersion
55-
: $minProjectPhpVersion;
53+
// an explicitly picked withPhpSets() version caps the whole set:
54+
// polyfilled rules up to the ceiling, the rest up to the lower of ceiling and project version
55+
if ($ceilingPhpVersion !== null) {
56+
$maxPhpVersion = $rector instanceof RelatedPolyfillInterface
57+
? $ceilingPhpVersion
58+
: min($ceilingPhpVersion, $minProjectPhpVersion);
59+
} else {
60+
$maxPhpVersion = $minProjectPhpVersion;
61+
}
5662

5763
// does satisfy version? → include
5864
if ($rector->provideMinPhpVersion() <= $maxPhpVersion) {

‎tests/Bridge/SetRectorsResolverTest.php‎

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@
66

77
use PHPUnit\Framework\TestCase;
88
use Rector\Bridge\SetRectorsResolver;
9-
use Rector\Configuration\PhpLevelSetResolver;
109
use Rector\Contract\Rector\RectorInterface;
11-
use Rector\Php\PhpVersionResolver\ComposerJsonPhpVersionResolver;
1210
use Rector\Set\ValueObject\SetList;
13-
use Rector\ValueObject\PhpVersion;
1411

1512
final class SetRectorsResolverTest extends TestCase
1613
{
@@ -21,31 +18,16 @@ protected function setUp(): void
2118
$this->setRectorsResolver = new SetRectorsResolver();
2219
}
2320

24-
public function testResolveFromFilePathForPhpVersion(): void
21+
public function testResolvePhpVersionBasedSet(): void
2522
{
26-
$configFilePaths = PhpLevelSetResolver::resolveFromPhpVersion(PhpVersion::PHP_70);
27-
$this->assertCount(6, $configFilePaths);
28-
$this->assertContainsOnlyString($configFilePaths);
29-
30-
foreach ($configFilePaths as $configFilePath) {
31-
$this->assertFileExists($configFilePath);
32-
}
33-
}
34-
35-
public function testResolveFromFilePathForPhpLevel(): void
36-
{
37-
$projectPhpVersion = ComposerJsonPhpVersionResolver::resolve(__DIR__ . '/Fixture/some-composer.json');
38-
39-
$this->assertIsInt($projectPhpVersion);
40-
$this->assertSame(PhpVersion::PHP_73, $projectPhpVersion);
41-
42-
$configFilePaths = PhpLevelSetResolver::resolveFromPhpVersion($projectPhpVersion);
43-
$this->assertCount(9, $configFilePaths);
23+
$phpVersionBasedSetFilePath = dirname(__DIR__, 2) . '/config/set/php-version-based.php';
4424

4525
$rectorRulesWithConfiguration = $this->setRectorsResolver->resolveFromFilePathsIncludingConfiguration(
46-
$configFilePaths
26+
[$phpVersionBasedSetFilePath]
4727
);
48-
$this->assertCount(63, $rectorRulesWithConfiguration);
28+
29+
$this->assertNotEmpty($rectorRulesWithConfiguration);
30+
$this->assertContainsOnlyRules($rectorRulesWithConfiguration);
4931
}
5032

5133
public function testResolveWithConfiguration(): void
@@ -58,6 +40,14 @@ public function testResolveWithConfiguration(): void
5840
$this->assertArrayHasKey(0, $rectorRulesWithConfiguration);
5941
$this->assertArrayHasKey(8, $rectorRulesWithConfiguration);
6042

43+
$this->assertContainsOnlyRules($rectorRulesWithConfiguration);
44+
}
45+
46+
/**
47+
* @param array<int, class-string<RectorInterface>|array<class-string<RectorInterface>, mixed[]>> $rectorRulesWithConfiguration
48+
*/
49+
private function assertContainsOnlyRules(array $rectorRulesWithConfiguration): void
50+
{
6151
foreach ($rectorRulesWithConfiguration as $rectorRuleWithConfiguration) {
6252
if (is_string($rectorRuleWithConfiguration)) {
6353
$this->assertTrue(is_a($rectorRuleWithConfiguration, RectorInterface::class, true));

‎tests/Configuration/PhpLevelSetResolverTest.php‎

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)