Skip to content

Commit d4b8a03

Browse files
authored
[TypeDeclaration] Skip already has @ return list<> docblock on AddReturnArrayDocblockBasedOnArrayMapRector (#7315)
* [TypeDeclaration] Skip already has @ return docblock on AddReturnArrayDocblockBasedOnArrayMapRector * Fix
1 parent ae9d6bb commit d4b8a03

3 files changed

Lines changed: 24 additions & 5 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\AddReturnArrayDocblockBasedOnArrayMapRector\Fixture;
4+
5+
use stdClass;
6+
7+
final class SkipAlreadyHasReturnDoc
8+
{
9+
/**
10+
* @return list<stdClass>
11+
*/
12+
public function process(array $items)
13+
{
14+
return array_map(function ($item): stdClass {
15+
return $item;
16+
}, $items);
17+
}
18+
}

rules/TypeDeclaration/Rector/ClassMethod/AddReturnArrayDocblockBasedOnArrayMapRector.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PhpParser\Node\Stmt\ClassMethod;
1414
use PhpParser\Node\Stmt\Function_;
1515
use PHPStan\Type\ArrayType;
16+
use PHPStan\Type\IntersectionType;
1617
use PHPStan\Type\MixedType;
1718
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
1819
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
@@ -125,6 +126,11 @@ public function refactor(Node $node): null|Function_|ClassMethod
125126

126127
$functionLikePhpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
127128

129+
$returnOriginalType = $functionLikePhpDocInfo->getReturnType();
130+
if ($returnOriginalType instanceof IntersectionType && $returnOriginalType->isArray()->yes()) {
131+
return null;
132+
}
133+
128134
$hasChanged = $this->phpDocTypeChanger->changeReturnType($node, $functionLikePhpDocInfo, $arrayType);
129135
if ($hasChanged) {
130136
return $node;

rules/TypeDeclarationDocblocks/Rector/ClassMethod/AddReturnDocblockForCommonObjectDenominatorRector.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use PhpParser\Node\Stmt\ClassMethod;
1010
use PhpParser\Node\Stmt\Function_;
1111
use PhpParser\Node\Stmt\Return_;
12-
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
1312
use PHPStan\Reflection\ReflectionProvider;
1413
use PHPStan\Type\ArrayType;
1514
use PHPStan\Type\Constant\ConstantArrayType;
@@ -109,10 +108,6 @@ public function getNodeTypes(): array
109108
public function refactor(Node $node): ?Node
110109
{
111110
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
112-
if ($phpDocInfo->getReturnTagValue() instanceof ReturnTagValueNode) {
113-
return null;
114-
}
115-
116111
$returnType = $phpDocInfo->getReturnType();
117112

118113
if (! $returnType instanceof MixedType || $returnType->isExplicitMixed()) {

0 commit comments

Comments
 (0)