From 6c8b3dc9f8d10cefe8351b0f3f80c6954dd2397a Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 8 Jun 2022 12:48:55 +0700 Subject: [PATCH] [DeadCode][PHPUnit] handle crash on RemoveJustPropertyFetchForAssignRector+SimplifyForeachInstanceOfRector (#2453) * [DeadCode][PHPUnit] handle crash on RemoveJustPropertyFetchForAssignRector+SimplifyForeachInstanceOfRector * Fixed :tada: --- src/Application/ChangedNodeScopeRefresher.php | 5 +++ .../Fixture/fixture.php.inc | 39 +++++++++++++++++++ .../UndefinedStmtIndexTest.php | 33 ++++++++++++++++ .../config/configured_rule.php | 12 ++++++ 4 files changed, 89 insertions(+) create mode 100644 tests/Issues/UndefinedStmtIndex/Fixture/fixture.php.inc create mode 100644 tests/Issues/UndefinedStmtIndex/UndefinedStmtIndexTest.php create mode 100644 tests/Issues/UndefinedStmtIndex/config/configured_rule.php diff --git a/src/Application/ChangedNodeScopeRefresher.php b/src/Application/ChangedNodeScopeRefresher.php index a1d559ac50a..14c7249583f 100644 --- a/src/Application/ChangedNodeScopeRefresher.php +++ b/src/Application/ChangedNodeScopeRefresher.php @@ -12,6 +12,7 @@ use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\Property; use PHPStan\Analyser\MutatingScope; +use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface; use Rector\Core\Exception\ShouldNotHappenException; use Rector\Core\NodeAnalyzer\ScopeAnalyzer; use Rector\Core\NodeAnalyzer\UnreachableStmtAnalyzer; @@ -69,6 +70,10 @@ final class ChangedNodeScopeRefresher $node = new Property(0, [], [], null, [$attributeGroup]); } + if ($node instanceof StmtsAwareInterface && $node->stmts !== null) { + $node->stmts = array_values($node->stmts); + } + $stmts = $this->resolveStmts($node); $this->phpStanNodeScopeResolver->processNodes($stmts, $smartFileInfo, $mutatingScope); } diff --git a/tests/Issues/UndefinedStmtIndex/Fixture/fixture.php.inc b/tests/Issues/UndefinedStmtIndex/Fixture/fixture.php.inc new file mode 100644 index 00000000000..50277506635 --- /dev/null +++ b/tests/Issues/UndefinedStmtIndex/Fixture/fixture.php.inc @@ -0,0 +1,39 @@ +user_id]->identities; + $array[] = $id; + $mappedUsers[$id->user_id]->identities = $array; + } + } +} + +?> +----- +user_id]->identities[] = $id; + } + } +} + +?> diff --git a/tests/Issues/UndefinedStmtIndex/UndefinedStmtIndexTest.php b/tests/Issues/UndefinedStmtIndex/UndefinedStmtIndexTest.php new file mode 100644 index 00000000000..a1ea63f5971 --- /dev/null +++ b/tests/Issues/UndefinedStmtIndex/UndefinedStmtIndexTest.php @@ -0,0 +1,33 @@ +doTestFileInfo($fileInfo); + } + + /** + * @return Iterator + */ + public function provideData(): Iterator + { + return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/tests/Issues/UndefinedStmtIndex/config/configured_rule.php b/tests/Issues/UndefinedStmtIndex/config/configured_rule.php new file mode 100644 index 00000000000..5a0294275a6 --- /dev/null +++ b/tests/Issues/UndefinedStmtIndex/config/configured_rule.php @@ -0,0 +1,12 @@ +rule(RemoveJustPropertyFetchForAssignRector::class); + $rectorConfig->rule(SimplifyForeachInstanceOfRector::class); +};