Skip to content

Commit

Permalink
[DeadCode][Php56] Handle infinite loop on RemoveUnusedVariableAssignR…
Browse files Browse the repository at this point in the history
…ector+AddDefaultValueForUndefinedVariableRector (#3940)

* [DeadCode][Php56] Handle infinite loop on RemoveUnusedVariableAssignRector+AddDefaultValueForUndefinedVariableRector

* Fixed 🎉

* Fix
  • Loading branch information
samsonasik committed May 23, 2023
1 parent 5cd8ac7 commit e04e51c
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 1 deletion.
10 changes: 10 additions & 0 deletions rules/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PHPStan\Analyser\Scope;
use Rector\Core\NodeAnalyzer\VariableAnalyzer;
use Rector\Core\Php\ReservedKeywordAnalyzer;
use Rector\Core\Rector\AbstractScopeAwareRector;
use Rector\DeadCode\SideEffect\SideEffectNodeDetector;
Expand All @@ -30,6 +31,7 @@ final class RemoveUnusedVariableAssignRector extends AbstractScopeAwareRector
public function __construct(
private readonly ReservedKeywordAnalyzer $reservedKeywordAnalyzer,
private readonly SideEffectNodeDetector $sideEffectNodeDetector,
private readonly VariableAnalyzer $variableAnalyzer
) {
}

Expand Down Expand Up @@ -217,6 +219,14 @@ private function resolvedAssignedVariablesByStmtPosition(array $stmts): array
continue;
}

if ($this->variableAnalyzer->isStaticOrGlobal($assign->var)) {
continue;
}

if ($this->variableAnalyzer->isUsedByReference($assign->var)) {
continue;
}

$assignedVariableNamesByStmtPosition[$key] = $variableName;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Kernel/RectorKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ final class RectorKernel
/**
* @var string
*/
private const CACHE_KEY = 'v23';
private const CACHE_KEY = 'v24';

private ContainerInterface|null $container = null;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\RemoveUnusedVariableDefault\Fixture;

class SkipUseStatic
{
public static function showProgress($thisStep = 1, int $totalSteps = 10)
{
static $inProgress = false;

if ($inProgress !== false && $inProgress <= $thisStep) {
static::fwrite(STDOUT, "\033[1A");
}
$inProgress = $thisStep;

if ($thisStep !== false) {
$thisStep = abs($thisStep);
}
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\RemoveUnusedVariableDefault;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class RemoveUnusedVariableDefaultTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\Assign\RemoveUnusedVariableAssignRector;
use Rector\Php56\Rector\FunctionLike\AddDefaultValueForUndefinedVariableRector;
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules([
RemoveUnusedVariableAssignRector::class,
AddDefaultValueForUndefinedVariableRector::class
]);
};

0 comments on commit e04e51c

Please sign in to comment.