55namespace Rector \Configuration ;
66
77use Deprecated ;
8+ use DrupalRector \Set \DrupalSetList ;
9+ use Nette \Utils \Strings ;
810use PhpParser \NodeVisitor ;
911use Rector \Bridge \SetRectorsResolver ;
1012use Rector \Caching \Contract \ValueObject \Storage \CacheStorageInterface ;
@@ -42,6 +44,11 @@ final class RectorConfigBuilder
4244{
4345 private const int MAX_LEVEL_GAP = 10 ;
4446
47+ /**
48+ * Matches the deprecated per-version PHP set files, e.g. .../config/set/php82.php
49+ */
50+ private const string DEPRECATED_PHP_SET_REGEX = '#/config/set/php\d+\.php$# ' ;
51+
4552 /**
4653 * A level method and the set that contains the very same rules,
4754 * so they are never enabled both at once
@@ -172,6 +179,12 @@ final class RectorConfigBuilder
172179
173180 private ?int $ pickedPhpSetsVersion = null ;
174181
182+ /**
183+ * Only an explicitly picked withPhpSets(phpXX) version acts as a ceiling; the composer.json
184+ * fallback must not, so polyfilled rules can still be raised above the project PHP version
185+ */
186+ private bool $ isPhpSetsVersionPicked = false ;
187+
175188 /**
176189 * @var LevelOverflow[]
177190 */
@@ -184,7 +197,7 @@ public function __invoke(RectorConfig $rectorConfig): void
184197 $ this ->sets [] = SetList::PHP_POLYFILLS ;
185198 }
186199
187- if ($ this ->pickedPhpSetsVersion !== null ) {
200+ if ($ this ->isPhpSetsVersionPicked && $ this -> pickedPhpSetsVersion !== null ) {
188201 SimpleParameterProvider ::setParameter (
189202 Option::POLYFILL_CEILING_PHP_VERSION ,
190203 $ this ->pickedPhpSetsVersion
@@ -408,6 +421,14 @@ public function withRootFiles(): self
408421 */
409422 public function withSets (array $ sets ): self
410423 {
424+ foreach ($ sets as $ set ) {
425+ if (Strings ::match ($ set , self ::DEPRECATED_PHP_SET_REGEX ) === null ) {
426+ continue ;
427+ }
428+
429+ Notifier ::notifyDeprecatedPhpSet ($ set );
430+ }
431+
411432 $ this ->sets = array_merge ($ this ->sets , $ sets );
412433
413434 return $ this ;
@@ -551,13 +572,13 @@ public function withPhpSets(
551572 );
552573 }
553574
554- // no version picked, resolve it from the project composer.json
575+ // no version picked, target the project composer.json PHP version
555576 if ($ pickedPhpVersions === []) {
556577 return $ this ->addPhpLevelSets (ComposerJsonPhpVersionResolver ::resolveFromCwdOrFail ());
557578 }
558579
559580 // explicitly picked version is a ceiling, even for polyfilled rules
560- $ this ->pickedPhpSetsVersion = $ pickedPhpVersions [ 0 ] ;
581+ $ this ->isPhpSetsVersionPicked = true ;
561582
562583 return $ this ->addPhpLevelSets ($ pickedPhpVersions [0 ]);
563584 }
@@ -688,7 +709,7 @@ public function withComposerBased(
688709
689710 if ($ drupal && class_exists ('DrupalRector\Set\DrupalSetList ' ) && constant ('DrupalRector\Set\DrupalSetList::COMPOSER_BASED ' )) {
690711 // waits on https://github.com/palantirnet/drupal-rector/pull/419/files#diff-c6bd4ee854830efc1363a7d99c1b6a2e7e64f2499a51e503174ab777de7e64e5
691- $ this ->sets [] = \ DrupalRector \ Set \ DrupalSetList::COMPOSER_BASED ;
712+ $ this ->sets [] = DrupalSetList::COMPOSER_BASED ;
692713 }
693714
694715 if ($ phpunit ) {
@@ -962,12 +983,10 @@ public function withPhpLevel(int $level): self
962983
963984 $ this ->isWithPhpLevelUsed = true ;
964985
965- $ phpVersion = ComposerJsonPhpVersionResolver ::resolveFromCwdOrFail ();
966-
967986 $ setRectorsResolver = new SetRectorsResolver ();
968- $ setFilePaths = PhpLevelSetResolver :: resolveFromPhpVersion ( $ phpVersion );
969-
970- $ rectorRulesWithConfiguration = $ setRectorsResolver -> resolveFromFilePathsIncludingConfiguration ( $ setFilePaths );
987+ $ rectorRulesWithConfiguration = $ setRectorsResolver -> resolveFromFilePathIncludingConfiguration (
988+ SetList:: PHP_VERSION_BASED_SET
989+ );
971990
972991 foreach ($ rectorRulesWithConfiguration as $ position => $ rectorRuleWithConfiguration ) {
973992 // add rules until level is reached
@@ -1143,8 +1162,9 @@ public function withSetProviders(): self
11431162 private function addPhpLevelSets (int $ phpVersion ): self
11441163 {
11451164 $ this ->isWithPhpSetsUsed = true ;
1165+ $ this ->pickedPhpSetsVersion = $ phpVersion ;
11461166
1147- $ this ->sets = array_merge ( $ this -> sets , PhpLevelSetResolver :: resolveFromPhpVersion ( $ phpVersion )) ;
1167+ $ this ->sets [] = SetList:: PHP_VERSION_BASED_SET ;
11481168
11491169 return $ this ;
11501170 }
0 commit comments