Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PHPUnit] Change @return array<> to @return Iterator<> on YieldDataProviderRector #415

Merged
merged 4 commits into from
Dec 9, 2024
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,61 @@
<?php

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

use PHPUnit\Framework\TestCase;

final class UseDataProviderTestPhpdoc extends TestCase
{
#[\PHPUnit\Framework\Attributes\DataProvider('dataProvider')]
#[\PHPUnit\Framework\Attributes\DataProvider('provideDataForProvider')]
public function test(string $filePath): void
{
}

/** @return array<int, array<string>> */
public static function provideDataForProvider()
{
return [
['<?php implode("", $foo, );', '<?php implode($foo, "", );']
];
}

public static function dataProvider()
{
return [
['<?php implode(\'\', $foo, );', '<?php implode($foo, );'],
['<?php implode(\'\', $foo, );', '<?php implode($foo, );']
];
}
}

?>
-----
<?php

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

use PHPUnit\Framework\TestCase;

final class UseDataProviderTestPhpdoc extends TestCase
{
#[\PHPUnit\Framework\Attributes\DataProvider('dataProvider')]
#[\PHPUnit\Framework\Attributes\DataProvider('provideDataForProvider')]
public function test(string $filePath): void
{
}

/** @return \Iterator<int, array<string>> */
public static function provideDataForProvider(): \Iterator
{
yield ['<?php implode("", $foo, );', '<?php implode($foo, "", );'];
}

public static function dataProvider(): \Iterator
{
yield ['<?php implode(\'\', $foo, );', '<?php implode($foo, );'];
yield ['<?php implode(\'\', $foo, );', '<?php implode($foo, );'];
}
}

?>
22 changes: 20 additions & 2 deletions rules/CodeQuality/Rector/Class_/YieldDataProviderRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Return_;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Generic\GenericObjectType;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\DeadCode\NodeAnalyzer\IsClassMethodUsedAnalyzer;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\NodeTransformer;
Expand All @@ -35,7 +38,8 @@ public function __construct(
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
private readonly DataProviderClassMethodFinder $dataProviderClassMethodFinder,
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly IsClassMethodUsedAnalyzer $isClassMethodUsedAnalyzer
private readonly IsClassMethodUsedAnalyzer $isClassMethodUsedAnalyzer,
private readonly PhpDocTypeChanger $phpDocTypeChanger,
) {
}

Expand Down Expand Up @@ -166,6 +170,20 @@ private function transformArrayToYieldsOnMethodNode(ClassMethod $classMethod, Ar
private function removeReturnTag(ClassMethod $classMethod): void
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($classMethod);
$phpDocInfo->removeByType(ReturnTagValueNode::class);

if ($phpDocInfo->getReturnType() instanceof ArrayType) {
$keyType = $phpDocInfo->getReturnType()
->getIterableKeyType();
$itemType = $phpDocInfo->getReturnType()
->getIterableValueType();

$this->phpDocTypeChanger->changeReturnType(
$classMethod,
$phpDocInfo,
new GenericObjectType('Iterator', [$keyType, $itemType])
);
} else {
$phpDocInfo->removeByType(ReturnTagValueNode::class);
}
}
}
Loading