Skip to content

Commit 566b3be

Browse files
authored
fix mixed[] override with empty array in DocblockReturnArrayFromDirectArrayInstanceRector (#7793)
1 parent a52f120 commit 566b3be

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\DocblockReturnArrayFromDirectArrayInstanceRector\Fixture;
4+
5+
final class SkipEmptyArrayMixed
6+
{
7+
/**
8+
* @return mixed[]
9+
*/
10+
private static function toArray(): array
11+
{
12+
return [];
13+
}
14+
}

rules/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockReturnArrayFromDirectArrayInstanceRector.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
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;
13+
use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode;
1214
use PHPStan\Type\Constant\ConstantArrayType;
15+
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
1316
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
1417
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
1518
use Rector\Rector\AbstractRector;
@@ -100,6 +103,10 @@ public function refactor(Node $node): ?Node
100103
return null;
101104
}
102105

106+
if ($this->shouldSkipReturnMixedAndEmptyArray($phpDocInfo, $soleReturn->expr)) {
107+
return null;
108+
}
109+
103110
// resolve simple type
104111
$returnedType = $this->getType($soleReturn->expr);
105112
if (! $returnedType instanceof ConstantArrayType) {
@@ -116,4 +123,19 @@ public function refactor(Node $node): ?Node
116123

117124
return $node;
118125
}
126+
127+
private function shouldSkipReturnMixedAndEmptyArray(PhpDocInfo $phpDocInfo, Array_ $array): bool
128+
{
129+
if ($array->items !== []) {
130+
return false;
131+
}
132+
133+
$returnTagValueNode = $phpDocInfo->getReturnTagValue();
134+
if (! $returnTagValueNode instanceof ReturnTagValueNode) {
135+
return false;
136+
}
137+
138+
// better than array{}
139+
return $returnTagValueNode->type instanceof ArrayTypeNode;
140+
}
119141
}

0 commit comments

Comments
 (0)