diff --git a/fixtures/completion/foreach.php b/fixtures/completion/foreach.php index 4278f00..fae0be9 100644 --- a/fixtures/completion/foreach.php +++ b/fixtures/completion/foreach.php @@ -38,3 +38,10 @@ foreach ($bar->test() as $value) { foreach ($unknownArray as $member->access => $unknown) { $unkno +} + +foreach ($loop as $loop) { +} + +foreach ($loop->getArray() as $loop) { +} diff --git a/src/DefinitionResolver.php b/src/DefinitionResolver.php index 72bdce4..43f8a09 100644 --- a/src/DefinitionResolver.php +++ b/src/DefinitionResolver.php @@ -571,12 +571,27 @@ class DefinitionResolver // If we get to a ForeachStatement, check the keys and values if ($n instanceof Node\Statement\ForeachStatement) { - if ($n->foreachKey instanceof Node\Expression\Variable + // Only check keys and values if we did not get here from the foreach collection, otherwise code like + // foreach ($a as $a) will send us in circles + $isForeachCollection = false; + if ($n->forEachCollectionName) { + if ($n->forEachCollectionName === $var) { + $isForeachCollection = true; + } else { + foreach ($n->forEachCollectionName->getDescendantNodes() as $childNode) { + if ($childNode === $var) { + $isForeachCollection = true; + break; + } + } + } + } + if (!$isForeachCollection && $n->foreachKey instanceof Node\Expression\Variable && $n->foreachKey->expression->getName() === $name ) { return $n->foreachKey; } - if ($n->foreachValue + if (!$isForeachCollection && $n->foreachValue && $n->foreachValue->expression instanceof Node\Expression\Variable && $n->foreachValue->expression->getName() === $name ) {