Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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,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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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']];
}

}

?>
6 changes: 5 additions & 1 deletion rules/CodeQuality/Rector/Class_/YieldDataProviderRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,16 @@ private function collectReturnArrayNodesFromClassMethod(ClassMethod $classMethod
}

$totalStmts = count($classMethod->stmts);
if ($totalStmts !== 1) {
return null;
}
Comment thread
samsonasik marked this conversation as resolved.
Outdated

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_ || $statement instanceof YieldFrom) {
$returnedExpr = $statement->expr;
if (! $returnedExpr instanceof Array_) {
return null;
Expand Down