Skip to content

Commit 04de69a

Browse files
dktappsondrejmirtes
authored andcommitted
added checks for yield from also
1 parent da60c46 commit 04de69a

3 files changed

Lines changed: 45 additions & 0 deletions

File tree

src/Rules/Generators/YieldFromTypeRule.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PHPStan\Type\MixedType;
1414
use PHPStan\Type\TypeWithClassName;
1515
use PHPStan\Type\VerbosityLevel;
16+
use PHPStan\Type\VoidType;
1617

1718
/**
1819
* @implements \PHPStan\Rules\Rule<\PhpParser\Node\Expr\YieldFrom>
@@ -130,6 +131,10 @@ public function processNode(Node $node, Scope $scope): array
130131
))->build();
131132
}
132133

134+
if ($scope->getType($node) instanceof VoidType && !$scope->isInFirstLevelStatement()) {
135+
$messages[] = RuleErrorBuilder::message('Result of yield from (void) is used.')->build();
136+
}
137+
133138
return $messages;
134139
}
135140

tests/PHPStan/Rules/Generators/YieldFromTypeRuleTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ public function testRule(): void
4040
'Generator expects value type array(DateTime, DateTime, stdClass, DateTimeImmutable), array(0 => DateTime, 1 => DateTime, 2 => stdClass, 4 => DateTimeImmutable) given.',
4141
74,
4242
],
43+
[
44+
'Result of yield from (void) is used.',
45+
111,
46+
],
4347
]);
4448
}
4549

tests/PHPStan/Rules/Generators/data/yield-from.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,40 @@ public function doArrayShape2(): \Generator
7474
yield from $this->doArrayShape();
7575
}
7676

77+
/**
78+
* @return \Generator<int, int>
79+
*/
80+
public function yieldWithImplicitReturn() : \Generator{
81+
yield 1;
82+
return 1;
83+
}
84+
85+
/**
86+
* @return \Generator<int, int, void, int>
87+
*/
88+
public function yieldWithExplicitReturn() : \Generator{
89+
yield 1;
90+
return 1;
91+
}
92+
93+
/**
94+
* @return \Generator<int, int, void, void>
95+
*/
96+
public function yieldWithVoidReturn() : \Generator{
97+
yield 1;
98+
}
99+
100+
/**
101+
* @return \Generator<int, int, void, void>
102+
*/
103+
public function yieldFromResult() : \Generator{
104+
yield from $this->yieldWithImplicitReturn();
105+
$mixed = yield from $this->yieldWithImplicitReturn();
106+
107+
yield from $this->yieldWithExplicitReturn();
108+
$int = yield from $this->yieldWithExplicitReturn();
109+
110+
yield from $this->yieldWithVoidReturn();
111+
$void = yield from $this->yieldWithVoidReturn();
112+
}
77113
}

0 commit comments

Comments
 (0)