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

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
));
}
}
2 changes: 0 additions & 2 deletions src/Config/Level/CodeQualityLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -143,7 +142,6 @@ final class CodeQualityLevel
ArrayKeyExistsTernaryThenValueToCoalescingRector::class,
AbsolutizeRequireAndIncludePathRector::class,
ChangeArrayPushToArrayAssignRector::class,
ForRepeatedCountToOwnVariableRector::class,
CommonNotEqualRector::class,
LogicalToBooleanRector::class,
VarToPublicPropertyRector::class,
Expand Down
Loading