From 4a0f2b0ba2ea2db1263d6d042bba7fd89632274c Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sun, 11 Oct 2026 11:58:56 +0200 Subject: [PATCH] Deprecate ForRepeatedCountToOwnVariableRector, gain is negligible --- .../already_existing_items_count.php.inc | 40 ------------ .../Fixture/fallback_for_complex.php.inc | 32 ---------- .../Fixture/fixture.php.inc | 32 ---------- .../Fixture/method_call_count.php.inc | 32 ---------- .../skip_counter_variable_exists.php.inc | 15 ----- .../Fixture/skip_in_html.php.inc | 5 -- ...orRepeatedCountToOwnVariableRectorTest.php | 28 --------- .../config/configured_rule.php | 9 --- .../ForRepeatedCountToOwnVariableRector.php | 62 +++---------------- src/Config/Level/CodeQualityLevel.php | 2 - 10 files changed, 9 insertions(+), 248 deletions(-) delete mode 100644 rules-tests/CodeQuality/Rector/For_/ForRepeatedCountToOwnVariableRector/Fixture/already_existing_items_count.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/For_/ForRepeatedCountToOwnVariableRector/Fixture/fallback_for_complex.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/For_/ForRepeatedCountToOwnVariableRector/Fixture/fixture.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/For_/ForRepeatedCountToOwnVariableRector/Fixture/method_call_count.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/For_/ForRepeatedCountToOwnVariableRector/Fixture/skip_counter_variable_exists.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/For_/ForRepeatedCountToOwnVariableRector/Fixture/skip_in_html.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/For_/ForRepeatedCountToOwnVariableRector/ForRepeatedCountToOwnVariableRectorTest.php delete mode 100644 rules-tests/CodeQuality/Rector/For_/ForRepeatedCountToOwnVariableRector/config/configured_rule.php diff --git a/rules-tests/CodeQuality/Rector/For_/ForRepeatedCountToOwnVariableRector/Fixture/already_existing_items_count.php.inc b/rules-tests/CodeQuality/Rector/For_/ForRepeatedCountToOwnVariableRector/Fixture/already_existing_items_count.php.inc deleted file mode 100644 index 1679797fbc2..00000000000 --- a/rules-tests/CodeQuality/Rector/For_/ForRepeatedCountToOwnVariableRector/Fixture/already_existing_items_count.php.inc +++ /dev/null @@ -1,40 +0,0 @@ -getItems() + 10); $i++) { - echo $items[$i]; - } - - return $itemsCount; - } -} - -?> ------ -getItems() + 10); - - for ($i = 5; $i <= $counter; $i++) { - echo $items[$i]; - } - - return $itemsCount; - } -} - -?> diff --git a/rules-tests/CodeQuality/Rector/For_/ForRepeatedCountToOwnVariableRector/Fixture/fallback_for_complex.php.inc b/rules-tests/CodeQuality/Rector/For_/ForRepeatedCountToOwnVariableRector/Fixture/fallback_for_complex.php.inc deleted file mode 100644 index 2093a7ea9a6..00000000000 --- a/rules-tests/CodeQuality/Rector/For_/ForRepeatedCountToOwnVariableRector/Fixture/fallback_for_complex.php.inc +++ /dev/null @@ -1,32 +0,0 @@ -getItems() + 10); $i++) { - echo $items[$i]; - } - } -} - -?> ------ -getItems() + 10); - for ($i = 5; $i <= $counter; $i++) { - echo $items[$i]; - } - } -} - -?> diff --git a/rules-tests/CodeQuality/Rector/For_/ForRepeatedCountToOwnVariableRector/Fixture/fixture.php.inc b/rules-tests/CodeQuality/Rector/For_/ForRepeatedCountToOwnVariableRector/Fixture/fixture.php.inc deleted file mode 100644 index 9868c392aed..00000000000 --- a/rules-tests/CodeQuality/Rector/For_/ForRepeatedCountToOwnVariableRector/Fixture/fixture.php.inc +++ /dev/null @@ -1,32 +0,0 @@ - ------ - diff --git a/rules-tests/CodeQuality/Rector/For_/ForRepeatedCountToOwnVariableRector/Fixture/method_call_count.php.inc b/rules-tests/CodeQuality/Rector/For_/ForRepeatedCountToOwnVariableRector/Fixture/method_call_count.php.inc deleted file mode 100644 index 59f928b4d11..00000000000 --- a/rules-tests/CodeQuality/Rector/For_/ForRepeatedCountToOwnVariableRector/Fixture/method_call_count.php.inc +++ /dev/null @@ -1,32 +0,0 @@ -getItems()); $i++) { - echo $items[$i]; - } - } -} - -?> ------ -getItems()); - for ($i = 5; $i <= $counter; $i++) { - echo $items[$i]; - } - } -} - -?> diff --git a/rules-tests/CodeQuality/Rector/For_/ForRepeatedCountToOwnVariableRector/Fixture/skip_counter_variable_exists.php.inc b/rules-tests/CodeQuality/Rector/For_/ForRepeatedCountToOwnVariableRector/Fixture/skip_counter_variable_exists.php.inc deleted file mode 100644 index c59f0cd76db..00000000000 --- a/rules-tests/CodeQuality/Rector/For_/ForRepeatedCountToOwnVariableRector/Fixture/skip_counter_variable_exists.php.inc +++ /dev/null @@ -1,15 +0,0 @@ - - -

Item

- - \ No newline at end of file diff --git a/rules-tests/CodeQuality/Rector/For_/ForRepeatedCountToOwnVariableRector/ForRepeatedCountToOwnVariableRectorTest.php b/rules-tests/CodeQuality/Rector/For_/ForRepeatedCountToOwnVariableRector/ForRepeatedCountToOwnVariableRectorTest.php deleted file mode 100644 index be01a380947..00000000000 --- a/rules-tests/CodeQuality/Rector/For_/ForRepeatedCountToOwnVariableRector/ForRepeatedCountToOwnVariableRectorTest.php +++ /dev/null @@ -1,28 +0,0 @@ -doTestFile($filePath); - } - - public static function provideData(): Iterator - { - return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/configured_rule.php'; - } -} diff --git a/rules-tests/CodeQuality/Rector/For_/ForRepeatedCountToOwnVariableRector/config/configured_rule.php b/rules-tests/CodeQuality/Rector/For_/ForRepeatedCountToOwnVariableRector/config/configured_rule.php deleted file mode 100644 index f271a4603d1..00000000000 --- a/rules-tests/CodeQuality/Rector/For_/ForRepeatedCountToOwnVariableRector/config/configured_rule.php +++ /dev/null @@ -1,9 +0,0 @@ -withRules([ForRepeatedCountToOwnVariableRector::class]); diff --git a/rules/CodeQuality/Rector/For_/ForRepeatedCountToOwnVariableRector.php b/rules/CodeQuality/Rector/For_/ForRepeatedCountToOwnVariableRector.php index 0eeb546fcbb..3785137a5bc 100644 --- a/rules/CodeQuality/Rector/For_/ForRepeatedCountToOwnVariableRector.php +++ b/rules/CodeQuality/Rector/For_/ForRepeatedCountToOwnVariableRector.php @@ -5,27 +5,18 @@ namespace Rector\CodeQuality\Rector\For_; use PhpParser\Node; -use PhpParser\Node\Expr; -use PhpParser\Node\Expr\Assign; -use PhpParser\Node\Expr\BinaryOp\Smaller; -use PhpParser\Node\Expr\BinaryOp\SmallerOrEqual; -use PhpParser\Node\Expr\FuncCall; -use PhpParser\Node\Expr\Variable; -use PhpParser\Node\Stmt; -use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\For_; -use Rector\PHPStan\ScopeFetcher; +use Rector\Configuration\Deprecation\Contract\DeprecatedInterface; +use Rector\Exception\ShouldNotHappenException; use Rector\Rector\AbstractRector; use Rector\RuleDoc\CodeSample\CodeSample; use Rector\RuleDoc\RuleDefinition; /** - * @see \Rector\Tests\CodeQuality\Rector\For_\ForRepeatedCountToOwnVariableRector\ForRepeatedCountToOwnVariableRectorTest + * @deprecated This rule is deprecated, as the gain is negligible. On arrays count() is O(1), so hoisting it to a variable saves nothing. On collections a dedicated count() method is more suitable than a for loop. */ -final class ForRepeatedCountToOwnVariableRector extends AbstractRector +final class ForRepeatedCountToOwnVariableRector extends AbstractRector implements DeprecatedInterface { - private const string COUNTER_NAME = 'counter'; - public function getRuleDefinition(): RuleDefinition { return new RuleDefinition( @@ -71,47 +62,12 @@ public function getNodeTypes(): array /** * @param For_ $node - * @return Stmt[]|null */ - public function refactor(Node $node): ?array + public function refactor(Node $node): ?Node { - if ($this->getFile()->containsHTML()) { - return null; - } - - $scope = ScopeFetcher::fetch($node); - - if ($scope->hasVariableType(self::COUNTER_NAME)->yes()) { - return null; - } - - $countInCond = null; - $counterVariable = new Variable(self::COUNTER_NAME); - - foreach ($node->cond as $condExpr) { - if (! $condExpr instanceof Smaller && ! $condExpr instanceof SmallerOrEqual) { - continue; - } - - if (! $condExpr->right instanceof FuncCall) { - continue; - } - - $funcCall = $condExpr->right; - if (! $this->isName($funcCall, 'count')) { - continue; - } - - $countInCond = $condExpr->right; - $condExpr->right = $counterVariable; - } - - if (! $countInCond instanceof Expr) { - return null; - } - - $countAssign = new Assign($counterVariable, $countInCond); - - return [new Expression($countAssign), $node]; + throw new ShouldNotHappenException(sprintf( + '"%s" rule is deprecated, as the gain is negligible; count() on arrays is O(1)', + self::class + )); } } diff --git a/src/Config/Level/CodeQualityLevel.php b/src/Config/Level/CodeQualityLevel.php index f20343b7cf3..bf81de986ad 100644 --- a/src/Config/Level/CodeQualityLevel.php +++ b/src/Config/Level/CodeQualityLevel.php @@ -29,7 +29,6 @@ use Rector\CodeQuality\Rector\Equal\UseIdenticalOverEqualWithSameTypeRector; use Rector\CodeQuality\Rector\Expression\InlineIfToExplicitIfRector; use Rector\CodeQuality\Rector\Expression\TernaryFalseExpressionToIfRector; -use Rector\CodeQuality\Rector\For_\ForRepeatedCountToOwnVariableRector; use Rector\CodeQuality\Rector\Foreach_\ForeachToInArrayRector; use Rector\CodeQuality\Rector\Foreach_\SimplifyForeachToCoalescingRector; use Rector\CodeQuality\Rector\FuncCall\ArrayMergeOfNonArraysToSimpleArrayRector; @@ -143,7 +142,6 @@ final class CodeQualityLevel ArrayKeyExistsTernaryThenValueToCoalescingRector::class, AbsolutizeRequireAndIncludePathRector::class, ChangeArrayPushToArrayAssignRector::class, - ForRepeatedCountToOwnVariableRector::class, CommonNotEqualRector::class, LogicalToBooleanRector::class, VarToPublicPropertyRector::class,