Skip to content
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 bin/rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ public function loadIfExistsAndNotLoadedYet(string $filePath): void
do {
$errors[] = $throwable->getMessage();
} while ($throwable = $throwable->getPrevious());

echo Json::encode([
'fatal_errors' => $errors,
]);
Expand Down
3 changes: 2 additions & 1 deletion src/Configuration/OnlyRuleResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Rector\Contract\Rector\RectorInterface;
use Rector\Exception\Configuration\RectorRuleNameAmbiguousException;
use Rector\Exception\Configuration\RectorRuleNotFoundException;
use ReflectionClass;

/**
* @see \Rector\Tests\Configuration\OnlyRuleResolverTest
Expand Down Expand Up @@ -106,7 +107,7 @@ private function isRectorRuleClass(string $className): bool
return false;
}

$reflectionClass = new \ReflectionClass($className);
$reflectionClass = new ReflectionClass($className);
if ($reflectionClass->isAbstract()) {
return false;
}
Expand Down
8 changes: 8 additions & 0 deletions src/Configuration/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ final class Option
*/
public const string POLYFILL_PACKAGES = 'polyfill_packages';

/**
* PHP version explicitly picked in withPhpSets(), e.g. withPhpSets(php82: true).
* Polyfilled rules above this version are skipped, as the version is an intended ceiling.
*
* @internal
*/
public const string POLYFILL_CEILING_PHP_VERSION = 'polyfill_ceiling_php_version';

/**
* @internal Use @see \Rector\Config\RectorConfig::importNames() instead
* @var string
Expand Down
12 changes: 12 additions & 0 deletions src/Configuration/RectorConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ final class RectorConfigBuilder

private ?bool $isWithPhpLevelUsed = null;

private ?int $pickedPhpSetsVersion = null;

/**
* @var array<class-string<SetProviderInterface>,bool>
*/
Expand Down Expand Up @@ -221,6 +223,13 @@ public function __invoke(RectorConfig $rectorConfig): void
$this->sets[] = SetList::PHP_POLYFILLS;
}

if ($this->pickedPhpSetsVersion !== null) {
SimpleParameterProvider::setParameter(
Option::POLYFILL_CEILING_PHP_VERSION,
$this->pickedPhpSetsVersion
);
}

// merge sets together
$this->sets = array_merge($this->sets, $this->groupLoadedSets);

Expand Down Expand Up @@ -601,6 +610,9 @@ public function withPhpSets(
return $this->addPhpLevelSets(ComposerJsonPhpVersionResolver::resolveFromCwdOrFail());
}

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

return $this->addPhpLevelSets($pickedPhpVersions[0]);
}

Expand Down
27 changes: 25 additions & 2 deletions src/VersionBonding/PhpVersionedFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Rector\VersionBonding;

use Rector\Configuration\Option;
use Rector\Configuration\Parameter\SimpleParameterProvider;
use Rector\Contract\Rector\RectorInterface;
use Rector\Php\PhpVersionProvider;
use Rector\Php\PolyfillPackagesProvider;
Expand All @@ -28,10 +30,13 @@ public function __construct(
public function filter(array $rectors): array
{
$minProjectPhpVersion = $this->phpVersionProvider->provide();
$ceilingPhpVersion = $this->resolveCeilingPhpVersion();

$activeRectors = [];
foreach ($rectors as $rector) {
if ($rector instanceof RelatedPolyfillInterface) {
// polyfill package can raise the rule above the project PHP version,
// but never above an explicitly picked withPhpSets() version
if ($rector instanceof RelatedPolyfillInterface && $ceilingPhpVersion === null) {
$polyfillPackageNames = $this->polyfillPackagesProvider->provide();

if (in_array($rector->providePolyfillPackage(), $polyfillPackageNames, true)) {
Expand All @@ -45,12 +50,30 @@ public function filter(array $rectors): array
continue;
}

$maxPhpVersion = $rector instanceof RelatedPolyfillInterface && $ceilingPhpVersion !== null
? $ceilingPhpVersion
: $minProjectPhpVersion;

// does satisfy version? → include
if ($rector->provideMinPhpVersion() <= $minProjectPhpVersion) {
if ($rector->provideMinPhpVersion() <= $maxPhpVersion) {
$activeRectors[] = $rector;
}
}

return $activeRectors;
}

private function resolveCeilingPhpVersion(): ?int
{
if (! SimpleParameterProvider::hasParameter(Option::POLYFILL_CEILING_PHP_VERSION)) {
return null;
}

$ceilingPhpVersion = SimpleParameterProvider::provideIntParameter(Option::POLYFILL_CEILING_PHP_VERSION);
if ($ceilingPhpVersion <= 0) {
return null;
}

return $ceilingPhpVersion;
}
}
1 change: 0 additions & 1 deletion tests/Configuration/OnlyRuleResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Rector\Tests\Configuration;

use PHPStan\Reflection\ReflectionProvider;
use Rector\Configuration\OnlyRuleResolver;
use Rector\Contract\Rector\RectorInterface;
use Rector\DeadCode\Rector\Assign\RemoveDoubleAssignRector;
Expand Down
41 changes: 41 additions & 0 deletions tests/VersionBonding/Fixture/PolyfillPhp83Rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\VersionBonding\Fixture;

use PhpParser\Node;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\PhpVersion;
use Rector\ValueObject\PolyfillPackage;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Rector\VersionBonding\Contract\RelatedPolyfillInterface;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

final class PolyfillPhp83Rector extends AbstractRector implements MinPhpVersionInterface, RelatedPolyfillInterface
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Test rector bound to PHP 8.3 polyfill', []);
}

public function getNodeTypes(): array
{
return [Node\Stmt\Class_::class];
}

public function refactor(Node $node): ?Node
{
return null;
}

public function provideMinPhpVersion(): int
{
return PhpVersion::PHP_83;
}

public function providePolyfillPackage(): string
{
return PolyfillPackage::PHP_83;
}
}
41 changes: 41 additions & 0 deletions tests/VersionBonding/PhpVersionedFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
namespace Rector\Tests\VersionBonding;

use PHPUnit\Framework\TestCase;
use Rector\Configuration\Option;
use Rector\Configuration\Parameter\SimpleParameterProvider;
use Rector\Php\PhpVersionProvider;
use Rector\Php\PolyfillPackagesProvider;
use Rector\Tests\VersionBonding\Fixture\NoInterfaceRector;
use Rector\Tests\VersionBonding\Fixture\PolyfillPhp83Rector;
use Rector\ValueObject\PhpVersion;
use Rector\ValueObject\PolyfillPackage;
use Rector\VersionBonding\PhpVersionedFilter;

final class PhpVersionedFilterTest extends TestCase
Expand All @@ -20,6 +25,14 @@ protected function setUp(): void
$polyfillPackagesProvider = new PolyfillPackagesProvider();

$this->phpVersionedFilter = new PhpVersionedFilter($phpVersionProvider, $polyfillPackagesProvider);

SimpleParameterProvider::setParameter(Option::POLYFILL_PACKAGES, [PolyfillPackage::PHP_83]);
}

protected function tearDown(): void
{
SimpleParameterProvider::setParameter(Option::POLYFILL_PACKAGES, []);
SimpleParameterProvider::setParameter(Option::POLYFILL_CEILING_PHP_VERSION, 0);
}

public function testRectorWithoutInterfaceIsIncluded(): void
Expand All @@ -30,4 +43,32 @@ public function testRectorWithoutInterfaceIsIncluded(): void
$this->assertCount(1, $filtered);
$this->assertSame($noInterfaceRector, $filtered[0]);
}

public function testPolyfilledRectorIsIncluded(): void
{
$polyfillPhp83Rector = new PolyfillPhp83Rector();
$filtered = $this->phpVersionedFilter->filter([$polyfillPhp83Rector]);

$this->assertCount(1, $filtered);
$this->assertSame($polyfillPhp83Rector, $filtered[0]);
}

public function testPolyfilledRectorAbovePickedPhpSetsVersionIsSkipped(): void
{
SimpleParameterProvider::setParameter(Option::POLYFILL_CEILING_PHP_VERSION, PhpVersion::PHP_82);

$filtered = $this->phpVersionedFilter->filter([new PolyfillPhp83Rector()]);
$this->assertCount(0, $filtered);
}

public function testPolyfilledRectorBelowPickedPhpSetsVersionIsIncluded(): void
{
SimpleParameterProvider::setParameter(Option::POLYFILL_CEILING_PHP_VERSION, PhpVersion::PHP_83);

$polyfillPhp83Rector = new PolyfillPhp83Rector();
$filtered = $this->phpVersionedFilter->filter([$polyfillPhp83Rector]);

$this->assertCount(1, $filtered);
$this->assertSame($polyfillPhp83Rector, $filtered[0]);
}
}
Loading