[CodeQuality] Fixes #4578 Skip ForToForeachRector on complex init (#4579)

This commit is contained in:
Abdul Malik Ikhsan 2020-11-12 00:08:28 +07:00 committed by GitHub
parent 0e5386c5d8
commit 75714bb3d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 0 deletions

View File

@ -138,6 +138,11 @@ CODE_SAMPLE
return null;
}
$init = $node->init;
if (count($init) > 2) {
return null;
}
$iteratedVariableSingle = $this->inflector->singularize($iteratedVariable);
$foreach = $this->createForeach($node, $iteratedVariableSingle);

View File

@ -0,0 +1,23 @@
<?php
namespace Rector\CodeQuality\Tests\Rector\For_\ForToForeachRector\Fixture;
class SkipComplexAssignmentInInit
{
public function run($tokens)
{
$o = new class {
public function getResult()
{
return ['a'];
}
};
for ($i = 0, $query = $o->getResult(), $c = count($query); $i < $c; $i ++)
{
echo $query[$i];
}
}
}
?>