Skip to content

Commit 8a0dec1

Browse files
dantleechondrejmirtes
authored andcommitted
Handle yield in for
1 parent ef8206b commit 8a0dec1

3 files changed

Lines changed: 21 additions & 5 deletions

File tree

src/Analyser/NodeScopeResolver.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,7 @@ private function processStmtNode(
929929
return new StatementResult($finalScope, $bodyScopeResult->hasYield(), $alwaysTerminating, []);
930930
} elseif ($stmt instanceof For_) {
931931
$initScope = $scope;
932+
$hasYield = false;
932933
foreach ($stmt->init as $initExpr) {
933934
$initScope = $this->processExprNode($initExpr, $initScope, $nodeCallback, ExpressionContext::createTopLevel())->getScope();
934935
}
@@ -955,8 +956,10 @@ private function processStmtNode(
955956
$bodyScope = $bodyScope->mergeWith($continueExitPoint->getScope());
956957
}
957958
foreach ($stmt->loop as $loopExpr) {
958-
$bodyScope = $this->processExprNode($loopExpr, $bodyScope, static function (): void {
959-
}, ExpressionContext::createTopLevel())->getScope();
959+
$exprResult = $this->processExprNode($loopExpr, $bodyScope, static function (): void {
960+
}, ExpressionContext::createTopLevel());
961+
$bodyScope = $exprResult->getScope();
962+
$hasYield = $hasYield || $exprResult->hasYield();
960963
}
961964

962965
if ($bodyScope->equals($prevScope)) {
@@ -992,7 +995,12 @@ private function processStmtNode(
992995

993996
$finalScope = $finalScope->mergeWith($scope);
994997

995-
return new StatementResult($finalScope, $finalScopeResult->hasYield(), false/* $finalScopeResult->isAlwaysTerminating() && $isAlwaysIterable*/, []);
998+
return new StatementResult(
999+
$finalScope,
1000+
$finalScopeResult->hasYield() || $hasYield,
1001+
false/* $finalScopeResult->isAlwaysTerminating() && $isAlwaysIterable*/,
1002+
[]
1003+
);
9961004
} elseif ($stmt instanceof Switch_) {
9971005
$scope = $this->processExprNode($stmt->cond, $scope, $nodeCallback, ExpressionContext::createDeep())->getScope();
9981006
$scopeForBranches = $scope;

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,12 +396,18 @@ public function bodySpecifiedVoidTReturn3(): \Generator
396396
return 2;
397397
}
398398

399-
public function yieldInExpression(): \Generator
399+
public function yieldInWhileCondition(): \Generator
400400
{
401401
while($foo = yield 'foo') {
402402
}
403403
}
404404

405+
public function yieldInForCondition(): \Generator
406+
{
407+
for($foo = 0; $foo > 0; $foo = yield range(3, 0)) {
408+
}
409+
}
410+
405411
}
406412

407413
class VoidUnion

tests/e2e/magic-setter/test.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php // lint >= 7.4
1+
<?php declare(strict_types = 1);
2+
3+
// lint >= 7.4
24

35
namespace MagicSetter;
46

0 commit comments

Comments
 (0)