From 51343702e67d1829fde00179e4909f831191d271 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felipe=20Say=C3=A3o=20Lobato=20Abreu?= Date: Wed, 25 Nov 2026 01:15:40 -0300 Subject: [PATCH] fix: preserve Rector and ECS rules across toolchain updates --- .changelog/current-toolchain-compatibility.md | 6 +++ src/Config/ECSConfig.php | 52 ++++++++++++++++++- src/Config/RectorConfig.php | 20 +++++-- src/Environment/RuntimeEnvironment.php | 8 +-- tests/Config/ECSConfigTest.php | 35 +++++++++++++ tests/Config/RectorConfigTest.php | 7 ++- tests/Environment/RuntimeEnvironmentTest.php | 21 +++++--- 7 files changed, 128 insertions(+), 21 deletions(-) create mode 100644 .changelog/current-toolchain-compatibility.md diff --git a/.changelog/current-toolchain-compatibility.md b/.changelog/current-toolchain-compatibility.md new file mode 100644 index 000000000..604641fff --- /dev/null +++ b/.changelog/current-toolchain-compatibility.md @@ -0,0 +1,6 @@ +--- +category: fixed +type: patch +--- + +Keep the default Rector and ECS rules working with current toolchain releases, and use the agent-detector 2.x process-environment API. Existing supported tool versions retain their legacy lint rules. diff --git a/src/Config/ECSConfig.php b/src/Config/ECSConfig.php index bd2b3fd45..62e809f1c 100644 --- a/src/Config/ECSConfig.php +++ b/src/Config/ECSConfig.php @@ -36,7 +36,16 @@ use Symplify\CodingStandard\Fixer\Annotation\RemovePropertyVariableNameDescriptionFixer; use Symplify\CodingStandard\Fixer\ArrayNotation\ArrayListItemNewlineFixer; use Symplify\CodingStandard\Fixer\ArrayNotation\ArrayOpenerAndCloserNewlineFixer; -use Symplify\CodingStandard\Fixer\Commenting\ParamReturnAndVarTagMalformsFixer; +use Symplify\CodingStandard\Fixer\Commenting\AddMissingParamNameFixer; +use Symplify\CodingStandard\Fixer\Commenting\AddMissingVarNameFixer; +use Symplify\CodingStandard\Fixer\Commenting\DoubleAsteriskInlineVarFixer; +use Symplify\CodingStandard\Fixer\Commenting\FixParamNameTypoFixer; +use Symplify\CodingStandard\Fixer\Commenting\RemoveDeadParamFixer; +use Symplify\CodingStandard\Fixer\Commenting\RemoveParamNameReferenceFixer; +use Symplify\CodingStandard\Fixer\Commenting\RemoveSuperfluousReturnNameFixer; +use Symplify\CodingStandard\Fixer\Commenting\RemoveSuperfluousVarNameFixer; +use Symplify\CodingStandard\Fixer\Commenting\SingleLineInlineVarDocBlockFixer; +use Symplify\CodingStandard\Fixer\Commenting\SwitchedTypeAndNameFixer; use Symplify\CodingStandard\Fixer\Commenting\RemoveUselessDefaultCommentFixer; use Symplify\CodingStandard\Fixer\LineLength\LineLengthFixer; use Symplify\CodingStandard\Fixer\Spacing\MethodChainingNewlineFixer; @@ -108,6 +117,25 @@ final class ECSConfig ], ]; + /** + * @var list successors of ECS's bundled malformed PHPDoc-tag fixer + * @see https://github.com/ecsphp/ecs-src/pull/334 + */ + private const array PHPDOC_TAG_FIXERS = [ + AddMissingParamNameFixer::class, + AddMissingVarNameFixer::class, + DoubleAsteriskInlineVarFixer::class, + FixParamNameTypoFixer::class, + RemoveDeadParamFixer::class, + RemoveParamNameReferenceFixer::class, + RemoveSuperfluousReturnNameFixer::class, + RemoveSuperfluousVarNameFixer::class, + SingleLineInlineVarDocBlockFixer::class, + SwitchedTypeAndNameFixer::class, + ]; + + private const string LEGACY_PHPDOC_TAG_FIXER = 'Symplify\\CodingStandard\\Fixer\\Commenting\\ParamReturnAndVarTagMalformsFixer'; + /** * Creates the default ECS configuration. * @@ -201,7 +229,7 @@ public static function applyPhpCsFixerSets(ECSConfigInterface $config): void if (self::DEFAULT_PREPARED_SETS['symplify']) { $config->rules([ RemovePHPStormAnnotationFixer::class, - ParamReturnAndVarTagMalformsFixer::class, + ...self::phpdocTagFixers($config), RemoveUselessDefaultCommentFixer::class, RemoveMethodNameDuplicateDescriptionFixer::class, RemovePropertyVariableNameDescriptionFixer::class, @@ -218,4 +246,24 @@ public static function applyPhpCsFixerSets(ECSConfigInterface $config): void ]); } } + + /** + * @return list + */ + private static function phpdocTagFixers(ECSConfigInterface $config): array + { + foreach (self::PHPDOC_TAG_FIXERS as $fixer) { + if (! class_exists($fixer)) { + // Older supported ECS releases bundle the original complete fixer. + return [self::LEGACY_PHPDOC_TAG_FIXER]; + } + } + + // Recent ECS versions already register the successors in the clean-code set. + if (method_exists($config, 'getCheckerClasses')) { + return array_values(array_diff(self::PHPDOC_TAG_FIXERS, $config->getCheckerClasses())); + } + + return self::PHPDOC_TAG_FIXERS; + } } diff --git a/src/Config/RectorConfig.php b/src/Config/RectorConfig.php index f8b8a0999..6a287d78b 100644 --- a/src/Config/RectorConfig.php +++ b/src/Config/RectorConfig.php @@ -50,7 +50,7 @@ final class RectorConfig { /** - * @var list the default Rector sets applied to Fast Forward projects + * @var list stable default Rector sets; configure() also loads legacy sets on older Rector */ public const array DEFAULT_SETS = [ SetList::DEAD_CODE, @@ -58,8 +58,6 @@ final class RectorConfig SetList::CODING_STYLE, SetList::TYPE_DECLARATION, SetList::PRIVATIZATION, - SetList::INSTANCEOF, - SetList::EARLY_RETURN, ]; /** @@ -95,7 +93,21 @@ public static function configure(?callable $customize = null): callable $skipPaths = WorkingProjectPathResolver::getToolingExcludedDirectories(); $skipRules = self::DEFAULT_SKIPPED_RULES; - $rectorConfig->sets(self::DEFAULT_SETS); + $sets = self::DEFAULT_SETS; + + // On older Rector releases the rules still live in these sets. + // Newer releases include them in the stable sets and removed the constants. + foreach (['INSTANCEOF', 'EARLY_RETURN'] as $legacySet) { + $constant = SetList::class . '::' . $legacySet; + if (\defined($constant)) { + $set = \constant($constant); + if (\is_string($set)) { + $sets[] = $set; + } + } + } + + $rectorConfig->sets($sets); $rectorConfig->paths($paths); $rectorConfig->skip([...$skipPaths, ...$skipRules]); $rectorConfig->cacheDirectory( diff --git a/src/Environment/RuntimeEnvironment.php b/src/Environment/RuntimeEnvironment.php index 8462751e3..7d7d36c17 100644 --- a/src/Environment/RuntimeEnvironment.php +++ b/src/Environment/RuntimeEnvironment.php @@ -82,12 +82,6 @@ public function isComposerTestRun(): bool */ public function isAgentPresent(): bool { - $environment = $this->environment->get(); - - if (! \is_array($environment)) { - return false; - } - - return $this->agentDetector->isAgentPresent($environment); + return $this->agentDetector->isAgentPresent(); } } diff --git a/tests/Config/ECSConfigTest.php b/tests/Config/ECSConfigTest.php index c81b244f5..4f100cacb 100644 --- a/tests/Config/ECSConfigTest.php +++ b/tests/Config/ECSConfigTest.php @@ -93,6 +93,41 @@ public function configureWillLoadPresetsAndPreserveConsumerRuleCustomization(): self::assertInstanceOf(ArrayListItemNewlineFixer::class, $config->make(ArrayListItemNewlineFixer::class)); } + #[Test] + public function standalonePresetWillRegisterTheUpstreamDocblockFixers(): void + { + require_once InstalledVersions::getInstallPath('symplify/easy-coding-standard') + . '/vendor/squizlabs/php_codesniffer/autoload.php'; + + $config = new ECSConfigInterface(); + ECSConfig::applyPhpCsFixerSets($config); + + $family = 'Symplify\\CodingStandard\\Fixer\\Commenting\\'; + $fixers = [ + 'AddMissingParamNameFixer', + 'AddMissingVarNameFixer', + 'DoubleAsteriskInlineVarFixer', + 'FixParamNameTypoFixer', + 'RemoveDeadParamFixer', + 'RemoveParamNameReferenceFixer', + 'RemoveSuperfluousReturnNameFixer', + 'RemoveSuperfluousVarNameFixer', + 'SingleLineInlineVarDocBlockFixer', + 'SwitchedTypeAndNameFixer', + ]; + $expectedNames = array_map(static fn (string $name): string => $family . $name, $fixers); + if (count(array_filter($expectedNames, class_exists(...))) !== count($expectedNames)) { + $expectedNames = [$family . 'ParamReturnAndVarTagMalformsFixer']; + } + + foreach ($expectedNames as $fixer) { + self::assertInstanceOf($fixer, $config->make($fixer)); + if (method_exists($config, 'getCheckerClasses')) { + self::assertContains($fixer, $config->getCheckerClasses()); + } + } + } + /** * @return void */ diff --git a/tests/Config/RectorConfigTest.php b/tests/Config/RectorConfigTest.php index 3b900f7b1..fb7c77161 100644 --- a/tests/Config/RectorConfigTest.php +++ b/tests/Config/RectorConfigTest.php @@ -37,6 +37,8 @@ use PHPUnit\Framework\TestCase; use Rector\Configuration\Option; use Rector\Configuration\Parameter\SimpleParameterProvider; +use Rector\CodeQuality\Rector\FuncCall\InlineIsAInstanceOfRector; +use Rector\EarlyReturn\Rector\If_\ChangeIfElseValueAssignToEarlyReturnRector; use Rector\Config\RectorConfig as RectorConfigInterface; use Rector\Php54\Rector\Array_\LongArrayToShortArrayRector; use function Safe\getcwd; @@ -134,6 +136,9 @@ public function configureWillApplyTheDefaultRectorConfigurationAndCustomizationC SimpleParameterProvider::provideArrayParameter(Option::REGISTERED_RECTOR_RULES), ); self::assertNotEmpty(SimpleParameterProvider::provideArrayParameter(Option::REGISTERED_RECTOR_SETS)); + $registeredRules = SimpleParameterProvider::provideArrayParameter(Option::REGISTERED_RECTOR_RULES); + self::assertContains(InlineIsAInstanceOfRector::class, $registeredRules); + self::assertContains(ChangeIfElseValueAssignToEarlyReturnRector::class, $registeredRules); self::assertContains( LongArrayToShortArrayRector::class, SimpleParameterProvider::provideArrayParameter(Option::REGISTERED_RECTOR_RULES), @@ -153,8 +158,6 @@ public function itWillExposeReusableDefaultSetsAndRules(): void SetList::CODING_STYLE, SetList::TYPE_DECLARATION, SetList::PRIVATIZATION, - SetList::INSTANCEOF, - SetList::EARLY_RETURN, ], RectorConfig::DEFAULT_SETS ); diff --git a/tests/Environment/RuntimeEnvironmentTest.php b/tests/Environment/RuntimeEnvironmentTest.php index 5fde74339..bc25b8769 100644 --- a/tests/Environment/RuntimeEnvironmentTest.php +++ b/tests/Environment/RuntimeEnvironmentTest.php @@ -28,6 +28,7 @@ use PHPUnit\Framework\TestCase; use Prophecy\PhpUnit\ProphecyTrait; use Prophecy\Prophecy\ObjectProphecy; +use function Safe\putenv; #[CoversClass(RuntimeEnvironment::class)] final class RuntimeEnvironmentTest extends TestCase @@ -170,11 +171,19 @@ public function isComposerTestRunWillReturnTrueDuringPhpUnitRuntime(): void #[Test] public function isAgentPresentWillReturnWhetherKnownAgentVariablesExist(): void { - $this->environment->get() - ->willReturn([ - 'CODEX_SANDBOX' => '1', - ]); - - self::assertTrue($this->runtimeEnvironment->isAgentPresent()); + $previousMarker = getenv('CODEX_SANDBOX'); + $previousDisable = getenv('AGENT_DETECTOR_DISABLE'); + + try { + putenv('CODEX_SANDBOX=1'); + putenv('AGENT_DETECTOR_DISABLE'); + self::assertTrue($this->runtimeEnvironment->isAgentPresent()); + + putenv('AGENT_DETECTOR_DISABLE=1'); + self::assertFalse($this->runtimeEnvironment->isAgentPresent()); + } finally { + putenv(false === $previousMarker ? 'CODEX_SANDBOX' : 'CODEX_SANDBOX=' . $previousMarker); + putenv(false === $previousDisable ? 'AGENT_DETECTOR_DISABLE' : 'AGENT_DETECTOR_DISABLE=' . $previousDisable); + } } }