Skip to content

Commit c2bab86

Browse files
dantleechondrejmirtes
authored andcommitted
yield in foreach
1 parent f1c28b2 commit c2bab86

2 files changed

Lines changed: 10 additions & 2 deletions

File tree

src/Analyser/NodeScopeResolver.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -754,8 +754,10 @@ private function processStmtNode(
754754
$hasYield = false;
755755
$this->processTraitUse($stmt, $scope, $nodeCallback);
756756
} elseif ($stmt instanceof Foreach_) {
757-
$scope = $this->processExprNode($stmt->expr, $scope, $nodeCallback, ExpressionContext::createDeep())->getScope();
757+
$condResult = $this->processExprNode($stmt->expr, $scope, $nodeCallback, ExpressionContext::createDeep());
758+
$scope = $condResult->getScope();
758759
$bodyScope = $this->enterForeach($scope, $stmt);
760+
$hasYield = false;
759761
$count = 0;
760762
do {
761763
$prevScope = $bodyScope;
@@ -801,7 +803,7 @@ private function processStmtNode(
801803

802804
return new StatementResult(
803805
$finalScope,
804-
$finalScopeResult->hasYield(),
806+
$finalScopeResult->hasYield() || $condResult->hasYield(),
805807
$isIterableAtLeastOnce->yes() && $finalScopeResult->isAlwaysTerminating(),
806808
[]
807809
);

tests/PHPStan/Rules/Missing/data/missing-return.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,12 @@ public function yieldInDoCondition(): \Generator
414414
} while(yield 1);
415415
}
416416

417+
public function yieldInForeach(): \Generator
418+
{
419+
foreach (yield 1 as $bar) {
420+
}
421+
}
422+
417423
}
418424

419425
class VoidUnion

0 commit comments

Comments
 (0)