Skip to content

Commit

Permalink
[CodeQuality] Skip $counter variable exists on ForRepeatedCountToOwnV…
Browse files Browse the repository at this point in the history
…ariableRector (#5828)
  • Loading branch information
samsonasik committed Apr 17, 2024
1 parent d67342e commit 56e2031
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
@@ -0,0 +1,15 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\For_\ForRepeatedCountToOwnVariableRector\Fixture;

class SkipCounterVariableExists
{
public function run($items)
{
$counter = 0;
for ($i = 5; $i <= count($items); $i++) {
echo $items[$i];
}
echo $counter++;
}
}
Expand Up @@ -14,14 +14,15 @@
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\For_;
use Rector\Rector\AbstractRector;
use PHPStan\Analyser\Scope;
use Rector\Rector\AbstractScopeAwareRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\CodeQuality\Rector\For_\ForRepeatedCountToOwnVariableRector\ForRepeatedCountToOwnVariableRectorTest
*/
final class ForRepeatedCountToOwnVariableRector extends AbstractRector
final class ForRepeatedCountToOwnVariableRector extends AbstractScopeAwareRector
{
/**
* @var string
Expand Down Expand Up @@ -75,8 +76,12 @@ public function getNodeTypes(): array
* @param For_ $node
* @return Stmt[]|null
*/
public function refactor(Node $node): ?array
public function refactorWithScope(Node $node, Scope $scope): ?array
{
if ($scope->hasVariableType(self::COUNTER_NAME)->yes()) {
return null;
}

$countInCond = null;
$counterVariable = new Variable(self::COUNTER_NAME);

Expand Down

0 comments on commit 56e2031

Please sign in to comment.