Fix UnusedForeachValueToArrayKeysRector on objects (#4750)

This commit is contained in:
Krystian Marcisz 2020-12-02 14:09:35 +01:00 committed by GitHub
parent 7dc7763585
commit 2eba542760
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 0 deletions

View File

@ -8,6 +8,7 @@ use PhpParser\Node;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Foreach_;
use PHPStan\Type\ObjectType;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@ -84,6 +85,10 @@ CODE_SAMPLE
return null;
}
if (is_a($this->getStaticType($node->expr), ObjectType::class)) {
return null;
}
$this->removeForeachValueAndUseArrayKeys($node);
return $node;

View File

@ -0,0 +1,18 @@
<?php
namespace Rector\CodeQuality\Tests\Rector\Foreach_\UnusedForeachValueToArrayKeysRector\Fixture;
use Rector\CodeQuality\Tests\Rector\Foreach_\UnusedForeachValueToArrayKeysRector\Source\Collection;
class SkipValuesIsObject
{
public function run()
{
$collection = new Collection();
foreach ($collection as $key => $value) {
$collection[$key] = null;
}
}
}
?>

View File

@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
namespace Rector\CodeQuality\Tests\Rector\Foreach_\UnusedForeachValueToArrayKeysRector\Source;
class Collection implements \IteratorAggregate
{
public function getIterator()
{
}
}