Skip to content

Commit d93f26c

Browse files
author
Sebastian Kehr
committed
fix: functions returning closures, arrow functions or anonymous classes containing yield expressions falsely detected as generators
1 parent c1326ba commit d93f26c

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/Reflection/ReflectionFunctionAbstract.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use PhpParser\Node\Expr\Throw_ as NodeThrow;
99
use PhpParser\Node\Expr\Yield_ as YieldNode;
1010
use PhpParser\Node\Expr\YieldFrom as YieldFromNode;
11+
use PhpParser\Node\Stmt\Class_ as ClassNode;
12+
use PhpParser\Node\FunctionLike as FunctionLikeNode;
1113
use PhpParser\Node\Stmt\ClassMethod as MethodNode;
1214
use PhpParser\NodeTraverser;
1315
use PhpParser\NodeVisitor\FindingVisitor;
@@ -360,7 +362,11 @@ private function nodeIsOrContainsYield(Node $node): bool
360362
foreach ($node->getSubNodeNames() as $nodeName) {
361363
$nodeProperty = $node->$nodeName;
362364

363-
if ($nodeProperty instanceof Node && $this->nodeIsOrContainsYield($nodeProperty)) {
365+
if ($nodeProperty instanceof Node &&
366+
!$nodeProperty instanceof ClassNode &&
367+
!$nodeProperty instanceof FunctionLikeNode &&
368+
$this->nodeIsOrContainsYield($nodeProperty)
369+
) {
364370
return true;
365371
}
366372

test/unit/Reflection/ReflectionFunctionAbstractTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,9 @@ public static function generatorProvider(): array
236236
['<?php function foo() { $foo->func(yield $foo); }', true],
237237
['<?php function foo() { new Foo(yield $foo); }', true],
238238
['<?php function foo() { yield from []; }', true],
239+
['<?php function foo() { return fn () => yield $foo; }', false],
240+
['<?php function foo() { return function () { yield $foo; }; }', false],
241+
['<?php function foo() { return new class () { function bar() { yield $foo; } }; }', false]
239242
];
240243
}
241244

0 commit comments

Comments
 (0)