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 take 2 (#3941)

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

* Fixed 🎉

* Fix phpstan
  • Loading branch information
samsonasik committed May 24, 2023
1 parent e04e51c commit a354eda
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/Application/ChangedNodeScopeRefresher.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,17 @@ public function refresh(Node $node, ?MutatingScope $mutatingScope, ?string $file

public function reIndexNodeAttributes(Node $node): void
{
if (($node instanceof ClassLike || $node instanceof StmtsAwareInterface || $node instanceof Declare_) && $node->stmts !== null) {
$node->stmts = array_values($node->stmts);
if ($this->hasArrayStmtsNode($node)) {
/**
* @var StmtsAwareInterface|ClassLike|Declare_ $node
* @var Stmt[] $stmts
*/
$stmts = $node->stmts;
$node->stmts = array_values($stmts);

foreach ($node->stmts as $key => $stmt) {
$stmt->setAttribute(AttributeKey::STMT_KEY, $key);
}
}

if ($node instanceof FunctionLike) {
Expand Down Expand Up @@ -129,6 +138,11 @@ public function reIndexNodeAttributes(Node $node): void
}
}

private function hasArrayStmtsNode(Node $node): bool
{
return ($node instanceof ClassLike || $node instanceof StmtsAwareInterface || $node instanceof Declare_) && $node->stmts !== null;
}

/**
* @return Stmt[]
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

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

final class ReUsedAssign
{
public function testCastAsJSONErrorDepth()
{
// Create array with depth 513 to get depth error
$array = [];
$value = 'test value';
$keys = rtrim(str_repeat('test.', 513), '.');
$keys = explode('.', $keys);
$current = $array;

foreach ($keys as $key) {
$current = $current[$key];
}
}
}

?>
-----
<?php

declare(strict_types=1);

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

final class ReUsedAssign
{
public function testCastAsJSONErrorDepth()
{
// Create array with depth 513 to get depth error
$array = [];
$keys = rtrim(str_repeat('test.', 513), '.');
$keys = explode('.', $keys);
$current = $array;

foreach ($keys as $key) {
$current = $current[$key];
}
}
}

?>

0 comments on commit a354eda

Please sign in to comment.