Skip to content

fix: some functions falsely detected as generators#1479

Merged
Ocramius merged 1 commit intoRoave:6.54.xfrom
sport-iat:fix_function_is_generator
Jan 25, 2025
Merged

fix: some functions falsely detected as generators#1479
Ocramius merged 1 commit intoRoave:6.54.xfrom
sport-iat:fix_function_is_generator

Conversation

@sebkehr
Copy link

@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)

$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 😁

['<?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