Skip to content
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

fix: some functions falsely detected as generators #1479

Merged
merged 1 commit into from
Jan 25, 2025

Conversation

sebkehr
Copy link
Contributor

@sebkehr sebkehr commented Jan 25, 2025

Relates to phpstan/phpstan-src#3794 and phpstan/phpstan#12462.

@ondrejmirtes asked me to submit an according fix here as well.

The proposed changes fix functions returning closures, arrow functions or anonymous classes containing yield expressions to be falsely recognized as generators.

Copy link
Member

@Ocramius Ocramius left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to apply CS (and ideally squash that commit, please)

@@ -360,7 +362,11 @@ private function nodeIsOrContainsYield(Node $node): bool
foreach ($node->getSubNodeNames() as $nodeName) {
$nodeProperty = $node->$nodeName;

if ($nodeProperty instanceof Node && $this->nodeIsOrContainsYield($nodeProperty)) {
if ($nodeProperty instanceof Node &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Operator precedence making this a bit of a puzzle to read: let's add some parentheses, please 😁

@@ -236,6 +236,9 @@ public static function generatorProvider(): array
['<?php function foo() { $foo->func(yield $foo); }', true],
['<?php function foo() { new Foo(yield $foo); }', true],
['<?php function foo() { yield from []; }', true],
['<?php function foo() { return fn () => yield $foo; }', false],
['<?php function foo() { return function () { yield $foo; }; }', false],
['<?php function foo() { return new class () { function bar() { yield $foo; } }; }', false]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we even had a PR discussion about this, last year: thanks for fixing this pothole!

@Ocramius Ocramius added the bug label Jan 25, 2025
@Ocramius Ocramius added this to the 6.54.0 milestone Jan 25, 2025
@sebkehr sebkehr force-pushed the fix_function_is_generator branch from d93f26c to 63c18ae Compare January 25, 2025 12:50
…es containing yield expressions falsely detected as generators
@sebkehr sebkehr force-pushed the fix_function_is_generator branch from 63c18ae to 67a14ab Compare January 25, 2025 13:00
Copy link
Member

@Ocramius Ocramius left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @sebkehr!

@Ocramius Ocramius merged commit 18f3778 into Roave:6.54.x Jan 25, 2025
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants