Fix false positive with Generator when yield is in a "while" condition#286
Fix false positive with Generator when yield is in a "while" condition#286ondrejmirtes merged 8 commits intophpstan:masterfrom dantleech:issue-3669-yield-complains-missing-return-type
Conversation
when determining if scope has yield or not.
| return 2; | ||
| } | ||
|
|
||
| public function yieldInExpression(): \Generator |
There was a problem hiding this comment.
The issue reported was detected by the MissingReturn rule. Not sure if there is a more suitable way to test (the test is that there is no error).
ondrejmirtes
left a comment
There was a problem hiding this comment.
Thank you, looks like the right fix! (I'm sorry for the current build failures on master, will fix them once I have a working computer again this week.)
Could you please also verify that other similar statements (for loop, foreach loop, do while, switch) don't have the same problem? THanks.
| $exprResult = $this->processExprNode($loopExpr, $bodyScope, static function (): void { | ||
| }, ExpressionContext::createTopLevel()); | ||
| $bodyScope = $exprResult->getScope(); | ||
| $hasYield = $hasYield || $exprResult->hasYield(); |
There was a problem hiding this comment.
not sure why this is a special case - i guess we could also get the no, ExpressionResult before this loop on $stmt->looploop is an array of the 3 expressions I guess.
tests/e2e/magic-setter/test.php
Outdated
| <?php // lint >= 7.4 | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| // lint >= 7.4 |
There was a problem hiding this comment.
this was the cs fixer
|
@ondrejmirtes updated for |
|
Looks like a simple if statement can't handle it either: https://phpstan.org/r/5dafc496-79af-4719-bcf2-41fa369da313 |
|
indeed, added |
|
Thank you! |
Relates to: phpstan/phpstan#3669
This PR fixes the above named issue as suggested there.
Not sure about the test however.