From b650d6e4e563f565fe90a5dde3378f9b8c659a04 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 10 Dec 2024 00:42:27 +0700 Subject: [PATCH] [PHPUnit] Change @return array<> to @return Iterator<> on YieldDataProviderRector (#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 * Closes #414 --------- Co-authored-by: Markus Staab Co-authored-by: Markus Staab --- .../use_data_provider_with_phpdoc.php.inc | 61 +++++++++++++++++++ .../Rector/Class_/YieldDataProviderRector.php | 22 ++++++- 2 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 rules-tests/CodeQuality/Rector/Class_/YieldDataProviderRector/Fixture/use_data_provider_with_phpdoc.php.inc diff --git a/rules-tests/CodeQuality/Rector/Class_/YieldDataProviderRector/Fixture/use_data_provider_with_phpdoc.php.inc b/rules-tests/CodeQuality/Rector/Class_/YieldDataProviderRector/Fixture/use_data_provider_with_phpdoc.php.inc new file mode 100644 index 00000000..77decd6f --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Class_/YieldDataProviderRector/Fixture/use_data_provider_with_phpdoc.php.inc @@ -0,0 +1,61 @@ +> */ + public static function provideDataForProvider() + { + return [ + [' +----- +> */ + public static function provideDataForProvider(): \Iterator + { + yield [' diff --git a/rules/CodeQuality/Rector/Class_/YieldDataProviderRector.php b/rules/CodeQuality/Rector/Class_/YieldDataProviderRector.php index 27da6148..5263408b 100644 --- a/rules/CodeQuality/Rector/Class_/YieldDataProviderRector.php +++ b/rules/CodeQuality/Rector/Class_/YieldDataProviderRector.php @@ -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; @@ -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, ) { } @@ -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); + } } }