Skip to content

Commit

Permalink
[PHPUnit] Change @return array<> to @return Iterator<> on YieldDataPr…
Browse files Browse the repository at this point in the history
…oviderRector (#415)

* YieldDataProviderRector: added failling test

* Update use_data_provider_with_phpdoc.php.inc

* Update rules-tests/CodeQuality/Rector/Class_/YieldDataProviderRector/Fixture/use_data_provider_with_phpdoc.php.inc

Co-authored-by: Abdul Malik Ikhsan <[email protected]>

* Closes #414

---------

Co-authored-by: Markus Staab <[email protected]>
Co-authored-by: Markus Staab <[email protected]>
  • Loading branch information
3 people authored Dec 9, 2024
1 parent 2c63591 commit b650d6e
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
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);
}
}
}

0 comments on commit b650d6e

Please sign in to comment.