Skip to content
Open
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
6 changes: 6 additions & 0 deletions .changelog/current-toolchain-compatibility.md
Original file line number Diff line number Diff line change
@@ -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.
52 changes: 50 additions & 2 deletions src/Config/ECSConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -108,6 +117,25 @@ final class ECSConfig
],
];

/**
* @var list<class-string> 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.
*
Expand Down Expand Up @@ -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,
Expand All @@ -218,4 +246,24 @@ public static function applyPhpCsFixerSets(ECSConfigInterface $config): void
]);
}
}

/**
* @return list<class-string>
*/
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;
}
}
20 changes: 16 additions & 4 deletions src/Config/RectorConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,14 @@
final class RectorConfig
{
/**
* @var list<string> the default Rector sets applied to Fast Forward projects
* @var list<string> stable default Rector sets; configure() also loads legacy sets on older Rector
*/
public const array DEFAULT_SETS = [
SetList::DEAD_CODE,
SetList::CODE_QUALITY,
SetList::CODING_STYLE,
SetList::TYPE_DECLARATION,
SetList::PRIVATIZATION,
SetList::INSTANCEOF,
SetList::EARLY_RETURN,
];

/**
Expand Down Expand Up @@ -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(
Expand Down
8 changes: 1 addition & 7 deletions src/Environment/RuntimeEnvironment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
35 changes: 35 additions & 0 deletions tests/Config/ECSConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
7 changes: 5 additions & 2 deletions tests/Config/RectorConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
Expand All @@ -153,8 +158,6 @@ public function itWillExposeReusableDefaultSetsAndRules(): void
SetList::CODING_STYLE,
SetList::TYPE_DECLARATION,
SetList::PRIVATIZATION,
SetList::INSTANCEOF,
SetList::EARLY_RETURN,
],
RectorConfig::DEFAULT_SETS
);
Expand Down
21 changes: 15 additions & 6 deletions tests/Environment/RuntimeEnvironmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
}