Propagate hasYield from yield in ternary branches - #5904
Merged
staabm merged 4 commits intoJun 21, 2026
Conversation
staabm
reviewed
Jun 21, 2026
hasYield from yield in ternary branches and throw expressionshasYield from yield in ternary branches
staabm
reviewed
Jun 21, 2026
…ressions - `TernaryHandler::processExpr()` only reported `hasYield` from the condition, ignoring `yield` placed in the `if`/`else` branches (and the default branch of the short `?:`). It now ORs the condition result with both branch results. - `ThrowHandler::processExpr()` hardcoded `hasYield: false`, dropping a `yield` inside a `throw` expression (e.g. `throw yield $x`). It now propagates `hasYield` from the thrown expression. - Without these, a closure/function whose only `yield` lives inside a ternary branch or a throw expression was not recognised as a generator by `MissingReturnRule`, producing a false "should return Generator but return statement is missing" error.
`yield` within a `throw` expression is too artificial to support for now, so drop the `ThrowHandler` change and its test case, keeping only the ternary-branch fix. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This reverts commit ef453d8.
staabm
force-pushed
the
create-pull-request/patch-c2he1eh
branch
from
June 21, 2026 13:49
a65179a to
d34c952
Compare
Contributor
|
It is still reported without the throw Exception ? |
Contributor
|
I can't think I have ever seen code like this |
Contributor
1 search-hit across github.com: https://github.com/search?q=%22throw+yield+%24%22&type=code |
Contributor
I mean https://phpstan.org/r/1cda3b84-18c7-4b3e-a3e1-e51fff27c908 |
VincentLanglet
approved these changes
Jun 21, 2026
Contributor
But seems like it's already not reported with an if |
Contributor
|
Yes - the bug only happened on short-if |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
A closure that only
yields inside a ternary branch (e.g.$x = $cond ? yield foo() : false;) was wrongly reported withAnonymous function should return Generator<...> but return statement is missing.even though the presence of
yieldanywhere in the body makes the function agenerator, so no explicit
returnis required.The root cause was that the ternary expression handler did not propagate the
hasYieldflag from its branches. While fixing it I found the same class of bugin the
throwexpression handler.Changes
src/Analyser/ExprHandler/TernaryHandler.php—processExpr()now ORshasYieldfrom the condition with the results of theifandelsebranches(and the default branch of the short ternary
?:), instead of only using thecondition's
hasYield.tests/PHPStan/Rules/Missing/data/bug-5681.phpandtests/PHPStan/Rules/Missing/MissingReturnRuleTest.php— regression tests.I audited every other
ExprHandleron the same axis (sub-expressions that maycontain a
yield):Coalesce,Match,BinaryOp,BooleanAnd,BooleanOr,AssignOp,Assign,Array,InterpolatedString,Isset,New,MethodCall,StaticCall, the various call handlers and the unary handlers allalready OR
hasYieldfrom all their sub-expressions correctly. Closure andarrow-function handlers correctly return
hasYield: false(an inner closure'syieldbelongs to the inner closure, not the outer function).Root cause
MissingReturnRuletreats a function as a generator (and therefore notrequiring a
return) when the statement result reportshasYield. This flag iscomputed bottom-up through the expression handlers. Each handler must OR the
hasYieldof every sub-expression that can execute ayield.TernaryHandlerreported only the condition's
hasYield.Test
tests/PHPStan/Rules/Missing/data/bug-5681.phpreproduces the reported case(
yieldin theifbranch of a ternary) plus three analogous cases:yieldin the
elsebranch,yieldin the default branch of?:. All three produced a falsereturn.missingerrorbefore the fix and none do after. Wired up via
MissingReturnRuleTest::testBug5681().Fixes phpstan/phpstan#5681