Skip to content

Commit 710646b

Browse files
committed
skip function-like nodes when checking if a function is a generator
1 parent 213eb65 commit 710646b

File tree

5 files changed

+62
-1
lines changed

5 files changed

+62
-1
lines changed

src/Reflection/Php/PhpFunctionFromParserNodeReflection.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,10 @@ private function nodeIsOrContainsYield(Node $node): bool
281281
foreach ($node->getSubNodeNames() as $nodeName) {
282282
$nodeProperty = $node->$nodeName;
283283

284-
if ($nodeProperty instanceof Node && $this->nodeIsOrContainsYield($nodeProperty)) {
284+
if ($nodeProperty instanceof Node &&
285+
!$nodeProperty instanceof FunctionLike &&
286+
$this->nodeIsOrContainsYield($nodeProperty)
287+
) {
285288
return true;
286289
}
287290

tests/PHPStan/Rules/Functions/ReturnTypeRuleTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -341,4 +341,20 @@ public function testBug11301(): void
341341
]);
342342
}
343343

344+
public function testBug12462(): void
345+
{
346+
$this->checkExplicitMixed = true;
347+
$this->checkNullables = true;
348+
$this->analyse([__DIR__ . '/data/bug-12462.php'], [
349+
[
350+
'Function Bug12462\functionReturningYieldingClosure() should return int but returns Closure.',
351+
7,
352+
],
353+
[
354+
'Function Bug12462\functionReturningYieldingArrowFunction() should return int but returns Closure.',
355+
12,
356+
],
357+
]);
358+
}
359+
344360
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug12462;
4+
5+
function functionReturningYieldingClosure (): int
6+
{
7+
return function () { yield ''; };
8+
}
9+
10+
function functionReturningYieldingArrowFunction (): int
11+
{
12+
return fn () => yield '';
13+
}

tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php

+14
Original file line numberDiff line numberDiff line change
@@ -1227,4 +1227,18 @@ public function testBug1O580(): void
12271227
]);
12281228
}
12291229

1230+
public function testBug12462(): void
1231+
{
1232+
$this->analyse([__DIR__ . '/data/bug-12462.php'], [
1233+
[
1234+
'Method Bug12462\A::methodReturningYieldingClosure() should return int but returns Closure.',
1235+
8,
1236+
],
1237+
[
1238+
'Method Bug12462\A::methodReturningYieldingArrowFunction() should return int but returns Closure.',
1239+
13,
1240+
],
1241+
]);
1242+
}
1243+
12301244
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug12462;
4+
5+
class A {
6+
function methodReturningYieldingClosure (): int
7+
{
8+
return function () { yield ''; };
9+
}
10+
11+
function methodReturningYieldingArrowFunction (): int
12+
{
13+
return fn () => yield '';
14+
}
15+
}

0 commit comments

Comments
 (0)