[DeadCode][PHPUnit] handle crash on RemoveJustPropertyFetchForAssignRector+SimplifyForeachInstanceOfRector (#2453)

* [DeadCode][PHPUnit] handle crash on RemoveJustPropertyFetchForAssignRector+SimplifyForeachInstanceOfRector

* Fixed 🎉
This commit is contained in:
Abdul Malik Ikhsan 2022-06-08 12:48:55 +07:00 committed by GitHub
parent 1a84313089
commit 6c8b3dc9f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 89 additions and 0 deletions

View File

@ -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);
}

View File

@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
namespace Rector\Core\Tests\Issues\UndefinedStmtIndex\Fixture;
class Fixture
{
public function fetchIdentities(array $data, array $identities)
{
$mappedUsers = [];
foreach ($identities as $id) {
$array = $mappedUsers[$id->user_id]->identities;
$array[] = $id;
$mappedUsers[$id->user_id]->identities = $array;
}
}
}
?>
-----
<?php
declare(strict_types=1);
namespace Rector\Core\Tests\Issues\UndefinedStmtIndex\Fixture;
class Fixture
{
public function fetchIdentities(array $data, array $identities)
{
$mappedUsers = [];
foreach ($identities as $id) {
$mappedUsers[$id->user_id]->identities[] = $id;
}
}
}
?>

View File

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace Rector\Core\Tests\Issues\UndefinedStmtIndex;
use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;
final class UndefinedStmtIndexTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}
/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}
public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}

View File

@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\StmtsAwareInterface\RemoveJustPropertyFetchForAssignRector;
use Rector\PHPUnit\Rector\Foreach_\SimplifyForeachInstanceOfRector;
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(RemoveJustPropertyFetchForAssignRector::class);
$rectorConfig->rule(SimplifyForeachInstanceOfRector::class);
};