1
0
Fork 0

don't check foreach keys/values when we come from the foreach collection

pull/567/head
Philip Nelson 2017-12-28 08:21:44 +11:00
parent 425b2390b5
commit 2b7d340c6f
No known key found for this signature in database
GPG Key ID: 8C8235644C5E2279
2 changed files with 26 additions and 2 deletions

View File

@ -38,3 +38,10 @@ foreach ($bar->test() as $value) {
foreach ($unknownArray as $unknown) {
$unkno
}
foreach ($loop as $loop) {
}
foreach ($loop->getArray() as $loop) {
}

View File

@ -571,10 +571,27 @@ class DefinitionResolver
// If we get to a ForeachStatement, check the keys and values
if ($n instanceof Node\Statement\ForeachStatement) {
if ($n->foreachKey && $n->foreachKey->expression->getName() === $name) {
// 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
) {