Skip to content

Commit 33fa2f9

Browse files
committed
Warn on deprecated per-version PHP set in withSets(), keep addPhpLevelSets() version arg
withSets() emits an E_USER_DEPRECATED notice when a SetList::PHP_* set file is passed, pointing to withPhpSets()/withPhpLevel(). Detected by file path, so the deprecated constants are not referenced internally. addPhpLevelSets() keeps the non-nullable picked version and stores it; an explicit withPhpSets(phpXX) version acts as a ceiling via isPhpSetsVersionPicked, while the composer.json fallback does not, preserving polyfill package support. Storing the version also keeps the argument used, so it is not stripped as dead. Claude-Session: https://claude.ai/code/session_014Q25TQ4b7b13UQQXScu3Fg
1 parent 33e9182 commit 33fa2f9

3 files changed

Lines changed: 86 additions & 5 deletions

File tree

‎phpstan.neon‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ parameters:
8282
paths:
8383
- config/set/level
8484
- tests/Bridge/SetRectorsResolverTest.php
85+
- tests/Configuration/RectorConfigBuilderTest.php
8586

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

‎src/Configuration/RectorConfigBuilder.php‎

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Deprecated;
88
use DrupalRector\Set\DrupalSetList;
9+
use Nette\Utils\Strings;
910
use PhpParser\NodeVisitor;
1011
use Rector\Bridge\SetRectorsResolver;
1112
use Rector\Caching\Contract\ValueObject\Storage\CacheStorageInterface;
@@ -23,6 +24,7 @@
2324
use Rector\Doctrine\Set\DoctrineSetList;
2425
use Rector\Enum\Config\Defaults;
2526
use Rector\Exception\Configuration\InvalidConfigurationException;
27+
use Rector\Php\PhpVersionResolver\ComposerJsonPhpVersionResolver;
2628
use Rector\PHPUnit\Set\PHPUnitSetList;
2729
use Rector\Set\ValueObject\DowngradeLevelSetList;
2830
use Rector\Set\ValueObject\SetList;
@@ -37,11 +39,17 @@
3739

3840
/**
3941
* @api
42+
* @see \Rector\Tests\Configuration\RectorConfigBuilderTest
4043
*/
4144
final class RectorConfigBuilder
4245
{
4346
private const int MAX_LEVEL_GAP = 10;
4447

48+
/**
49+
* Matches the deprecated per-version PHP set files, e.g. .../config/set/php82.php
50+
*/
51+
private const string DEPRECATED_PHP_SET_REGEX = '#/config/set/php\d+\.php$#';
52+
4553
/**
4654
* A level method and the set that contains the very same rules,
4755
* so they are never enabled both at once
@@ -172,6 +180,12 @@ final class RectorConfigBuilder
172180

173181
private ?int $pickedPhpSetsVersion = null;
174182

183+
/**
184+
* Only an explicitly picked withPhpSets(phpXX) version acts as a ceiling; the composer.json
185+
* fallback must not, so polyfilled rules can still be raised above the project PHP version
186+
*/
187+
private bool $isPhpSetsVersionPicked = false;
188+
175189
/**
176190
* @var LevelOverflow[]
177191
*/
@@ -184,7 +198,7 @@ public function __invoke(RectorConfig $rectorConfig): void
184198
$this->sets[] = SetList::PHP_POLYFILLS;
185199
}
186200

187-
if ($this->pickedPhpSetsVersion !== null) {
201+
if ($this->isPhpSetsVersionPicked && $this->pickedPhpSetsVersion !== null) {
188202
SimpleParameterProvider::setParameter(
189203
Option::POLYFILL_CEILING_PHP_VERSION,
190204
$this->pickedPhpSetsVersion
@@ -408,6 +422,18 @@ public function withRootFiles(): self
408422
*/
409423
public function withSets(array $sets): self
410424
{
425+
foreach ($sets as $set) {
426+
if (Strings::match($set, self::DEPRECATED_PHP_SET_REGEX) === null) {
427+
continue;
428+
}
429+
430+
trigger_error(sprintf(
431+
'The per-version PHP set "%s" is deprecated. Use "withPhpSets()" or "withPhpLevel()" instead, '
432+
. 'they pick the rules by your PHP version automatically.',
433+
$set
434+
), E_USER_DEPRECATED);
435+
}
436+
411437
$this->sets = array_merge($this->sets, $sets);
412438

413439
return $this;
@@ -553,13 +579,13 @@ public function withPhpSets(
553579

554580
// no version picked, target the project composer.json PHP version
555581
if ($pickedPhpVersions === []) {
556-
return $this->addPhpLevelSets();
582+
return $this->addPhpLevelSets(ComposerJsonPhpVersionResolver::resolveFromCwdOrFail());
557583
}
558584

559585
// explicitly picked version is a ceiling, even for polyfilled rules
560-
$this->pickedPhpSetsVersion = $pickedPhpVersions[0];
586+
$this->isPhpSetsVersionPicked = true;
561587

562-
return $this->addPhpLevelSets();
588+
return $this->addPhpLevelSets($pickedPhpVersions[0]);
563589
}
564590

565591
#[Deprecated(message: 'Use "withPhpLevel()" instead, it raises PHP level one rule at a time.')]
@@ -1135,10 +1161,16 @@ public function withSetProviders(): self
11351161
return $this;
11361162
}
11371163

1138-
private function addPhpLevelSets(): self
1164+
/**
1165+
* @param PhpVersion::* $phpVersion
1166+
*/
1167+
private function addPhpLevelSets(int $phpVersion): self
11391168
{
11401169
$this->isWithPhpSetsUsed = true;
1170+
$this->pickedPhpSetsVersion = $phpVersion;
1171+
11411172
$this->sets[] = SetList::PHP_VERSION_BASED_SET;
1173+
11421174
return $this;
11431175
}
11441176

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Configuration;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Rector\Config\RectorConfig;
9+
use Rector\Set\ValueObject\SetList;
10+
11+
final class RectorConfigBuilderTest extends TestCase
12+
{
13+
/**
14+
* @var string[]
15+
*/
16+
private array $deprecations = [];
17+
18+
protected function setUp(): void
19+
{
20+
$this->deprecations = [];
21+
set_error_handler(function (int $errorNumber, string $errorMessage): bool {
22+
$this->deprecations[] = $errorMessage;
23+
return true;
24+
}, E_USER_DEPRECATED);
25+
}
26+
27+
protected function tearDown(): void
28+
{
29+
restore_error_handler();
30+
}
31+
32+
public function testWithSetsWarnsOnDeprecatedPhpSet(): void
33+
{
34+
RectorConfig::configure()
35+
->withSets([SetList::PHP_82]);
36+
37+
$this->assertCount(1, $this->deprecations);
38+
$this->assertStringContainsString('withPhpSets()', $this->deprecations[0]);
39+
}
40+
41+
public function testWithSetsDoesNotWarnOnNonPhpSet(): void
42+
{
43+
RectorConfig::configure()
44+
->withSets([SetList::CODE_QUALITY]);
45+
46+
$this->assertSame([], $this->deprecations);
47+
}
48+
}

0 commit comments

Comments
 (0)