|
11 | 11 | use PhpParser\Node\Stmt\Return_; |
12 | 12 | use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode; |
13 | 13 | use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode; |
| 14 | +use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode; |
14 | 15 | use PHPStan\Type\Constant\ConstantArrayType; |
15 | 16 | use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo; |
16 | 17 | use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory; |
17 | 18 | use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger; |
| 19 | +use Rector\BetterPhpDocParser\ValueObject\Type\SpacingAwareArrayTypeNode; |
18 | 20 | use Rector\Rector\AbstractRector; |
19 | 21 | use Rector\TypeDeclarationDocblocks\NodeFinder\ReturnNodeFinder; |
20 | 22 | use Rector\TypeDeclarationDocblocks\TagNodeAnalyzer\UsefulArrayTagNodeAnalyzer; |
@@ -114,6 +116,17 @@ public function refactor(Node $node): ?Node |
114 | 116 | return null; |
115 | 117 | } |
116 | 118 |
|
| 119 | + // bare "@return array" with "return []" -> "mixed[]", better than "array{}" |
| 120 | + if ($soleReturn->expr->items === [] && $this->hasBareArrayReturnTag($phpDocInfo)) { |
| 121 | + $this->phpDocTypeChanger->changeReturnTypeNode( |
| 122 | + $node, |
| 123 | + $phpDocInfo, |
| 124 | + new SpacingAwareArrayTypeNode(new IdentifierTypeNode('mixed')) |
| 125 | + ); |
| 126 | + |
| 127 | + return $node; |
| 128 | + } |
| 129 | + |
117 | 130 | // resolve simple type |
118 | 131 | $returnedType = $this->getType($soleReturn->expr); |
119 | 132 | if (! $returnedType instanceof ConstantArrayType) { |
@@ -147,4 +160,14 @@ private function shouldSkipReturnMixedAndEmptyArray(PhpDocInfo $phpDocInfo, Arra |
147 | 160 | // better than array{} |
148 | 161 | return $returnTagValueNode->type instanceof ArrayTypeNode; |
149 | 162 | } |
| 163 | + |
| 164 | + private function hasBareArrayReturnTag(PhpDocInfo $phpDocInfo): bool |
| 165 | + { |
| 166 | + $returnTagValueNode = $phpDocInfo->getReturnTagValue(); |
| 167 | + if (! $returnTagValueNode instanceof ReturnTagValueNode) { |
| 168 | + return false; |
| 169 | + } |
| 170 | + |
| 171 | + return $returnTagValueNode->type instanceof IdentifierTypeNode && $returnTagValueNode->type->name === 'array'; |
| 172 | + } |
150 | 173 | } |
0 commit comments