-
Notifications
You must be signed in to change notification settings - Fork 577
Detect generators by scanning the body for yield, including unreachable code and excluding nested functions
#5910
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
phpstan-bot
wants to merge
1
commit into
phpstan:2.2.x
Choose a base branch
from
phpstan-bot:create-pull-request/patch-q71sugo
base: 2.2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Parser; | ||
|
|
||
| use Override; | ||
| use PhpParser\Node; | ||
| use PhpParser\Node\Expr\Yield_; | ||
| use PhpParser\Node\Expr\YieldFrom; | ||
| use PhpParser\NodeTraverser; | ||
| use PhpParser\NodeVisitor; | ||
| use PhpParser\NodeVisitorAbstract; | ||
|
|
||
| /** | ||
| * Collects all `yield`/`yield from` expressions that belong to a single function-like | ||
| * (a function/method/closure makes itself a generator), regardless of whether they are | ||
| * reachable. Nested function-likes are not descended into, because a `yield` inside a | ||
| * nested closure makes the nested closure a generator, not the outer scope. | ||
| */ | ||
| final class YieldFindingVisitor extends NodeVisitorAbstract | ||
| { | ||
|
|
||
| /** @var list<Yield_|YieldFrom> */ | ||
| private array $yieldNodes = []; | ||
|
|
||
| #[Override] | ||
| public function enterNode(Node $node): ?int | ||
| { | ||
| if ($node instanceof Node\FunctionLike) { | ||
| return NodeVisitor::DONT_TRAVERSE_CHILDREN; | ||
| } | ||
|
|
||
| if ($node instanceof Yield_ || $node instanceof YieldFrom) { | ||
| $this->yieldNodes[] = $node; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * @param Node[] $nodes | ||
| * @return list<Yield_|YieldFrom> | ||
| */ | ||
| public static function findInNodes(array $nodes): array | ||
| { | ||
| $visitor = new self(); | ||
| $traverser = new NodeTraverser($visitor); | ||
| $traverser->traverse($nodes); | ||
|
|
||
| return $visitor->yieldNodes; | ||
| } | ||
|
|
||
| } |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug4100ReturnType; | ||
|
|
||
| function returnsStringWithNestedGenerator(): int | ||
| { | ||
| $inner = function (): \Generator { | ||
| yield 1; | ||
| }; | ||
| $inner; | ||
|
|
||
| return 'hello'; | ||
| } |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug4100; | ||
|
|
||
| use Generator; | ||
|
|
||
| function foo(): Generator | ||
| { | ||
| while (false) { | ||
| return; | ||
| yield 2; | ||
| } | ||
| } | ||
|
|
||
| function withThrow(): Generator | ||
| { | ||
| if (rand(0, 1)) { | ||
| throw new \Exception(); | ||
| yield 1; | ||
| } | ||
| } | ||
|
|
||
| class Foo | ||
| { | ||
|
|
||
| public function method(): Generator | ||
| { | ||
| while (false) { | ||
| return; | ||
| yield 2; | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| function closures(): void | ||
| { | ||
| $c = function (): Generator { | ||
| while (false) { | ||
| return; | ||
| yield 2; | ||
| } | ||
| }; | ||
| $c; | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead of another NodeVisitorAbstract traversal across all statements, instead put a attribute on the function to mark it a generator-function, similar to other *Visitors in src/Parser