Skip to content

Commit

Permalink
[CodeQuality] Skip collections ForeachToInArrayRector [closes #1533]
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jun 4, 2019
1 parent d8dbf06 commit 9dfb666
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Return_;
use PHPStan\Type\ObjectType;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\Node\Manipulator\BinaryOpManipulator;
use Rector\PhpParser\NodeTraverser\CallableNodeTraverser;
Expand Down Expand Up @@ -93,6 +94,11 @@ public function refactor(Node $node): ?Node
$ifCondition = $firstNodeInsideForeach->cond;
$foreachValueVar = $node->valueVar;

$foreachValueStaticType = $this->getStaticType($node->expr);
if ($foreachValueStaticType instanceof ObjectType) {
return null;
}

$matchedNodes = $this->matchNodes($ifCondition, $foreachValueVar);
if ($matchedNodes === null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Rector\CodeQuality\Tests\Rector\Foreach_\ForeachToInArrayRector\Fixtur

use Rector\CodeQuality\Tests\Rector\Foreach_\ForeachToInArrayRector\Source\Collection;

final class IgnoreCollections
final class SkipCollections
{
public function foreachToInArray(): bool
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function test(): void
__DIR__ . '/Fixture/fixture2.php.inc',
__DIR__ . '/Fixture/fixture3.php.inc',
__DIR__ . '/Fixture/fixture4.php.inc',
__DIR__ . '/Fixture/ignore_collections.php.inc',
__DIR__ . '/Fixture/skip_collections.php.inc',
]);
}

Expand Down

0 comments on commit 9dfb666

Please sign in to comment.