From 980b7bfd355df68b00aa1ab0c8b6b35385706d7a Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 14 Jul 2025 16:50:31 +0700 Subject: [PATCH 1/6] [DowngradePhp80] Handle not match @return with native return type on DowngradeUnionTypeDeclarationRector --- .../not_match_return_doc_with_native.php.inc | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 rules-tests/DowngradePhp80/Rector/FunctionLike/DowngradeUnionTypeDeclarationRector/Fixture/not_match_return_doc_with_native.php.inc diff --git a/rules-tests/DowngradePhp80/Rector/FunctionLike/DowngradeUnionTypeDeclarationRector/Fixture/not_match_return_doc_with_native.php.inc b/rules-tests/DowngradePhp80/Rector/FunctionLike/DowngradeUnionTypeDeclarationRector/Fixture/not_match_return_doc_with_native.php.inc new file mode 100644 index 00000000..e31bdd03 --- /dev/null +++ b/rules-tests/DowngradePhp80/Rector/FunctionLike/DowngradeUnionTypeDeclarationRector/Fixture/not_match_return_doc_with_native.php.inc @@ -0,0 +1,31 @@ + +----- + From 0f6f1b06f61a3612a2f8b6f7b6d9abeb5617b28b Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 14 Jul 2025 17:01:17 +0700 Subject: [PATCH 2/6] Fix --- src/PhpDocDecorator/PhpDocFromTypeDeclarationDecorator.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/PhpDocDecorator/PhpDocFromTypeDeclarationDecorator.php b/src/PhpDocDecorator/PhpDocFromTypeDeclarationDecorator.php index 927411fc..90d75e74 100644 --- a/src/PhpDocDecorator/PhpDocFromTypeDeclarationDecorator.php +++ b/src/PhpDocDecorator/PhpDocFromTypeDeclarationDecorator.php @@ -74,7 +74,9 @@ public function decorateReturn(ClassMethod|Function_|Closure|ArrowFunction $func $phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($functionLike); $returnTagValueNode = $phpDocInfo->getReturnTagValue(); - $returnType = $returnTagValueNode instanceof ReturnTagValueNode + + $returnType = $this->staticTypeMapper->mapPhpParserNodePHPStanType($functionLike->returnType); + $returnDocType = $returnTagValueNode instanceof ReturnTagValueNode ? $this->staticTypeMapper->mapPHPStanPhpDocTypeToPHPStanType($returnTagValueNode, $functionLike->returnType) : $this->staticTypeMapper->mapPhpParserNodePHPStanType($functionLike->returnType); @@ -88,7 +90,7 @@ public function decorateReturn(ClassMethod|Function_|Closure|ArrowFunction $func return; } - $this->phpDocTypeChanger->changeReturnType($functionLike, $phpDocInfo, $returnType); + $this->phpDocTypeChanger->changeReturnType($functionLike, $phpDocInfo, $returnDocType); $functionLike->returnType = null; if (! $functionLike instanceof ClassMethod) { From a36f6b5de62dd8959fdfc525f3b07e156b5564f6 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 14 Jul 2025 17:04:15 +0700 Subject: [PATCH 3/6] Fix --- .../Fixture/not_match_return_doc_with_native.php.inc | 3 --- .../PhpDocFromTypeDeclarationDecorator.php | 8 +++++++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/rules-tests/DowngradePhp80/Rector/FunctionLike/DowngradeUnionTypeDeclarationRector/Fixture/not_match_return_doc_with_native.php.inc b/rules-tests/DowngradePhp80/Rector/FunctionLike/DowngradeUnionTypeDeclarationRector/Fixture/not_match_return_doc_with_native.php.inc index e31bdd03..1dc02171 100644 --- a/rules-tests/DowngradePhp80/Rector/FunctionLike/DowngradeUnionTypeDeclarationRector/Fixture/not_match_return_doc_with_native.php.inc +++ b/rules-tests/DowngradePhp80/Rector/FunctionLike/DowngradeUnionTypeDeclarationRector/Fixture/not_match_return_doc_with_native.php.inc @@ -20,9 +20,6 @@ namespace Rector\Tests\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeDecl final class NotMatchReturnDocWithNative { - /** - * @return \stdClass[]|null - */ public function run($value) { } diff --git a/src/PhpDocDecorator/PhpDocFromTypeDeclarationDecorator.php b/src/PhpDocDecorator/PhpDocFromTypeDeclarationDecorator.php index 90d75e74..c878cc3b 100644 --- a/src/PhpDocDecorator/PhpDocFromTypeDeclarationDecorator.php +++ b/src/PhpDocDecorator/PhpDocFromTypeDeclarationDecorator.php @@ -90,7 +90,13 @@ public function decorateReturn(ClassMethod|Function_|Closure|ArrowFunction $func return; } - $this->phpDocTypeChanger->changeReturnType($functionLike, $phpDocInfo, $returnDocType); + if (! $this->isMatchingType($returnType, [$returnDocType])) { + // not match between @return and native return type, just remove the doc + $phpDocInfo->removeByName(ReturnTagValueNode::class); + } else { + $this->phpDocTypeChanger->changeReturnType($functionLike, $phpDocInfo, $returnDocType); + } + $functionLike->returnType = null; if (! $functionLike instanceof ClassMethod) { From ec7497b72641b6fe0aacc973d3adde3f75e2c02c Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Mon, 14 Jul 2025 10:04:55 +0000 Subject: [PATCH 4/6] [ci-review] Rector Rectify --- src/PhpDocDecorator/PhpDocFromTypeDeclarationDecorator.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/PhpDocDecorator/PhpDocFromTypeDeclarationDecorator.php b/src/PhpDocDecorator/PhpDocFromTypeDeclarationDecorator.php index c878cc3b..de40d443 100644 --- a/src/PhpDocDecorator/PhpDocFromTypeDeclarationDecorator.php +++ b/src/PhpDocDecorator/PhpDocFromTypeDeclarationDecorator.php @@ -97,7 +97,6 @@ public function decorateReturn(ClassMethod|Function_|Closure|ArrowFunction $func $this->phpDocTypeChanger->changeReturnType($functionLike, $phpDocInfo, $returnDocType); } - $functionLike->returnType = null; if (! $functionLike instanceof ClassMethod) { return; From 0ce53e089a8ec4526f06c77770377d4fafb269d8 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 14 Jul 2025 17:12:51 +0700 Subject: [PATCH 5/6] keep doc --- .../Fixture/not_match_return_doc_with_native.php.inc | 3 +++ .../PhpDocFromTypeDeclarationDecorator.php | 9 ++------- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/rules-tests/DowngradePhp80/Rector/FunctionLike/DowngradeUnionTypeDeclarationRector/Fixture/not_match_return_doc_with_native.php.inc b/rules-tests/DowngradePhp80/Rector/FunctionLike/DowngradeUnionTypeDeclarationRector/Fixture/not_match_return_doc_with_native.php.inc index 1dc02171..e31bdd03 100644 --- a/rules-tests/DowngradePhp80/Rector/FunctionLike/DowngradeUnionTypeDeclarationRector/Fixture/not_match_return_doc_with_native.php.inc +++ b/rules-tests/DowngradePhp80/Rector/FunctionLike/DowngradeUnionTypeDeclarationRector/Fixture/not_match_return_doc_with_native.php.inc @@ -20,6 +20,9 @@ namespace Rector\Tests\DowngradePhp80\Rector\FunctionLike\DowngradeUnionTypeDecl final class NotMatchReturnDocWithNative { + /** + * @return \stdClass[]|null + */ public function run($value) { } diff --git a/src/PhpDocDecorator/PhpDocFromTypeDeclarationDecorator.php b/src/PhpDocDecorator/PhpDocFromTypeDeclarationDecorator.php index de40d443..df2ac01f 100644 --- a/src/PhpDocDecorator/PhpDocFromTypeDeclarationDecorator.php +++ b/src/PhpDocDecorator/PhpDocFromTypeDeclarationDecorator.php @@ -56,7 +56,7 @@ public function __construct( private readonly ReflectionResolver $reflectionResolver, private readonly PhpAttributeAnalyzer $phpAttributeAnalyzer, private readonly PhpVersionProvider $phpVersionProvider, - private readonly AstResolver $astResolver, + private readonly AstResolver $astResolver ) { $this->classMethodWillChangeReturnTypes = [ // @todo how to make list complete? is the method list needed or can we use just class names? @@ -90,12 +90,7 @@ public function decorateReturn(ClassMethod|Function_|Closure|ArrowFunction $func return; } - if (! $this->isMatchingType($returnType, [$returnDocType])) { - // not match between @return and native return type, just remove the doc - $phpDocInfo->removeByName(ReturnTagValueNode::class); - } else { - $this->phpDocTypeChanger->changeReturnType($functionLike, $phpDocInfo, $returnDocType); - } + $this->phpDocTypeChanger->changeReturnType($functionLike, $phpDocInfo, $returnDocType); $functionLike->returnType = null; if (! $functionLike instanceof ClassMethod) { From 683ce263b55982cb82fb513da722128f8e20f4a1 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Mon, 14 Jul 2025 17:13:03 +0700 Subject: [PATCH 6/6] keep doc --- .../match_return_doc_with_native.php.inc | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 rules-tests/DowngradePhp80/Rector/FunctionLike/DowngradeUnionTypeDeclarationRector/Fixture/match_return_doc_with_native.php.inc diff --git a/rules-tests/DowngradePhp80/Rector/FunctionLike/DowngradeUnionTypeDeclarationRector/Fixture/match_return_doc_with_native.php.inc b/rules-tests/DowngradePhp80/Rector/FunctionLike/DowngradeUnionTypeDeclarationRector/Fixture/match_return_doc_with_native.php.inc new file mode 100644 index 00000000..b05fbe8e --- /dev/null +++ b/rules-tests/DowngradePhp80/Rector/FunctionLike/DowngradeUnionTypeDeclarationRector/Fixture/match_return_doc_with_native.php.inc @@ -0,0 +1,31 @@ + +----- +