Skip to content

Commit eb751d4

Browse files
samsonasikstaabm
andauthored
[CodeQuality] Skip with if on YieldDataProviderRector with yield from (#556)
* YieldDataProviderRector: Added failling test * [CodeQuality] Skip with if on YieldDataProviderRector with yield from --------- Co-authored-by: Markus Staab <maggus.staab@googlemail.com>
1 parent 3ea365a commit eb751d4

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\YieldDataProviderRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class SkipWithIf extends TestCase
8+
{
9+
#[\PHPUnit\Framework\Attributes\DataProvider('dataProvider')]
10+
public function test(string $val1, string $val2): void
11+
{
12+
}
13+
14+
public static function dataProvider(): iterable
15+
{
16+
yield from [
17+
['value3', 'value4'],
18+
['value5', 'value6'],
19+
['value7', 'value8'],
20+
];
21+
22+
if (PHP_VERSION_ID < 80100) {
23+
return 1;
24+
}
25+
26+
yield ['value1', 'value2'];
27+
}
28+
29+
}

rules/CodeQuality/Rector/Class_/YieldDataProviderRector.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,13 @@ private function collectReturnArrayNodesFromClassMethod(ClassMethod $classMethod
127127
return null;
128128
}
129129

130+
$totalStmts = count($classMethod->stmts);
130131
foreach ($classMethod->stmts as $statement) {
131132
if ($statement instanceof Expression) {
132133
$statement = $statement->expr;
133134
}
134135

135-
if ($statement instanceof Return_ || $statement instanceof YieldFrom) {
136+
if ($statement instanceof Return_ || ($statement instanceof YieldFrom && $totalStmts === 1)) {
136137
$returnedExpr = $statement->expr;
137138
if (! $returnedExpr instanceof Array_) {
138139
return null;

0 commit comments

Comments
 (0)