Skip to content

Commit

Permalink
Merge pull request #1479 from sport-iat/fix_function_is_generator
Browse files Browse the repository at this point in the history
fix: some functions falsely detected as generators
  • Loading branch information
Ocramius authored Jan 25, 2025
2 parents c1326ba + 67a14ab commit 18f3778
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Reflection/ReflectionFunctionAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use PhpParser\Node\Expr\Throw_ as NodeThrow;
use PhpParser\Node\Expr\Yield_ as YieldNode;
use PhpParser\Node\Expr\YieldFrom as YieldFromNode;
use PhpParser\Node\FunctionLike as FunctionLikeNode;
use PhpParser\Node\Stmt\Class_ as ClassNode;
use PhpParser\Node\Stmt\ClassMethod as MethodNode;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\FindingVisitor;
Expand Down Expand Up @@ -360,7 +362,12 @@ 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 &&
! ($nodeProperty instanceof ClassNode) &&
! ($nodeProperty instanceof FunctionLikeNode) &&
$this->nodeIsOrContainsYield($nodeProperty)
) {
return true;
}

Expand Down
3 changes: 3 additions & 0 deletions test/unit/Reflection/ReflectionFunctionAbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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],
];
}

Expand Down

0 comments on commit 18f3778

Please sign in to comment.