Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\YieldDataProviderRector\Fixture;

use PHPUnit\Framework\TestCase;

final class MultiStmtsReturnTest extends TestCase
{
#[\PHPUnit\Framework\Attributes\DataProvider('dataProvider')]
public function test(string $val1, string $val2): void
{
}

public static function dataProvider(): iterable
{
$a = 1;
$b = 2;
$c = 3;
$d = 4;

return [
[$a, $b],
[$c, $d]
];
}

}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\YieldDataProviderRector\Fixture;

use PHPUnit\Framework\TestCase;

final class MultiStmtsReturnTest extends TestCase
{
#[\PHPUnit\Framework\Attributes\DataProvider('dataProvider')]
public function test(string $val1, string $val2): void
{
}

public static function dataProvider(): \Iterator
{
$a = 1;
$b = 2;
$c = 3;
$d = 4;
yield [$a, $b];
yield [$c, $d];
}

}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\YieldDataProviderRector\Fixture;

use PHPUnit\Framework\TestCase;

final class SkipMultipleYieldFrom extends TestCase
{
#[\PHPUnit\Framework\Attributes\DataProvider('dataProvider')]
public function test(string $val1, string $val2): void
{
}

public static function dataProvider(): iterable
{
yield from [
['value1', 'value2'],
['value3', 'value4'],
];
yield from [
['value5', 'value6'],
['value7', 'value8'],
];
}

}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\YieldDataProviderRector\Fixture;

use PHPUnit\Framework\TestCase;

final class SkipYieldFromExprWithReturn extends TestCase
{
#[\PHPUnit\Framework\Attributes\DataProvider('dataProvider')]
public function test(string $val1, string $val2): void
{
}

public static function dataProvider(): iterable
{
yield from [
['value3', 'value4'],
['value5', 'value6'],
['value7', 'value8'],
];
Comment on lines +16 to +20
Copy link
Member

@samsonasik samsonasik Oct 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add test to allow multiple yield from ? see https://3v4l.org/seVoc

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can add it, but the current rector impl will not work with it ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or should I add a test, that this scenario will be skipped?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think we can fix with if yield form is multiple, just skip. findInstanceof() and count() > 1 should works.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok


if (PHP_VERSION_ID < 80100) {
return;
}

return [['value1', 'value2']];
}

}

?>
29 changes: 25 additions & 4 deletions rules/CodeQuality/Rector/Class_/YieldDataProviderRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

use PhpParser\Node;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\AssignOp;
use PhpParser\Node\Expr\AssignRef;
use PhpParser\Node\Expr\YieldFrom;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Class_;
Expand Down Expand Up @@ -42,7 +45,7 @@ public function __construct(
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly IsClassMethodUsedAnalyzer $isClassMethodUsedAnalyzer,
private readonly PhpDocTypeChanger $phpDocTypeChanger,
private readonly DocBlockUpdater $docBlockUpdater
private readonly DocBlockUpdater $docBlockUpdater,
) {
}

Expand Down Expand Up @@ -127,23 +130,41 @@ private function collectReturnArrayNodesFromClassMethod(ClassMethod $classMethod
return null;
}

$totalStmts = count($classMethod->stmts);
$yieldedFromExpr = null;
foreach ($classMethod->stmts as $statement) {
if ($statement instanceof Expression) {
$statement = $statement->expr;
}

if ($statement instanceof Return_ || ($statement instanceof YieldFrom && $totalStmts === 1)) {
if ($statement instanceof Return_) {
$returnedExpr = $statement->expr;
if (! $returnedExpr instanceof Array_) {
return null;
}

return $returnedExpr;
}

if ($statement instanceof YieldFrom) {
if (! $statement->expr instanceof Array_) {
return null;
}

if ($yieldedFromExpr instanceof Array_) {
return null;
}

$yieldedFromExpr = $statement->expr;
} elseif (
! $statement instanceof Assign
&& ! $statement instanceof AssignRef
&& ! $statement instanceof AssignOp
) {
return null;
}
}

return null;
return $yieldedFromExpr;
}

private function transformArrayToYieldsOnMethodNode(ClassMethod $classMethod, Array_ $array): void
Expand Down