Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f3e39df

Browse files
committed
skip function-like nodes when checking if a function is a generator
1 parent de3720d commit f3e39df

File tree

5 files changed

+90
-1
lines changed

5 files changed

+90
-1
lines changed

‎src/Reflection/Php/PhpFunctionFromParserNodeReflection.php‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PhpParser\Node\FunctionLike;
88
use PhpParser\Node\Stmt\ClassMethod;
99
use PhpParser\Node\Stmt\Function_;
10+
use PHPStan\Node\AnonymousClassNode;
1011
use PHPStan\Reflection\Assertions;
1112
use PHPStan\Reflection\AttributeReflection;
1213
use PHPStan\Reflection\ExtendedFunctionVariant;
@@ -287,7 +288,11 @@ private function nodeIsOrContainsYield(Node $node): bool
287288
foreach ($node->getSubNodeNames() as $nodeName) {
288289
$nodeProperty = $node->$nodeName;
289290

290-
if ($nodeProperty instanceof Node && $this->nodeIsOrContainsYield($nodeProperty)) {
291+
if ($nodeProperty instanceof Node &&
292+
!$nodeProperty instanceof FunctionLike &&
293+
!$nodeProperty instanceof AnonymousClassNode &&
294+
$this->nodeIsOrContainsYield($nodeProperty)
295+
) {
291296
return true;
292297
}
293298

‎tests/PHPStan/Rules/Functions/ReturnTypeRuleTest.php‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,4 +341,24 @@ 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+
'Function Bug12462\functionRetuningYieldingAnonymousClass() should return int but returns class@anonymous/tests/PHPStan/Rules/Functions/data/bug-12462.php:17.',
359+
17,
360+
],
361+
]);
362+
}
363+
344364
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
}
14+
15+
function functionRetuningYieldingAnonymousClass (): int
16+
{
17+
return new class () {
18+
public function f(): \Generator {
19+
yield '';
20+
}
21+
};
22+
}

‎tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,4 +1232,22 @@ public function testBug1O580(): void
12321232
]);
12331233
}
12341234

1235+
public function testBug12462(): void
1236+
{
1237+
$this->analyse([__DIR__ . '/data/bug-12462.php'], [
1238+
[
1239+
'Method Bug12462\A::methodReturningYieldingClosure() should return int but returns Closure.',
1240+
8,
1241+
],
1242+
[
1243+
'Method Bug12462\A::methodReturningYieldingArrowFunction() should return int but returns Closure.',
1244+
13,
1245+
],
1246+
[
1247+
'Method Bug12462\A::methodRetuningYieldingAnonymousClass() should return int but returns class@anonymous/tests/PHPStan/Rules/Methods/data/bug-12462.php:18.',
1248+
18,
1249+
],
1250+
]);
1251+
}
1252+
12351253
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
16+
function methodRetuningYieldingAnonymousClass (): int
17+
{
18+
return new class () {
19+
public function f(): \Generator {
20+
yield '';
21+
}
22+
};
23+
}
24+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /