Skip to content

Commit c3088b7

Browse files
[TypeDeclarationDocblocks] Skip empty array return in DocblockReturnArrayFromDirectArrayInstanceRector (#8434)
A @return array{} is the narrowest array type. When added to a method that can be overridden, any child returning a filled array violates it. Empty array{} docblocks carry no useful information, so skip them entirely. Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: TomasVotruba <924196+TomasVotruba@users.noreply.github.com>
1 parent a5c7025 commit c3088b7

3 files changed

Lines changed: 15 additions & 40 deletions

File tree

‎rules-tests/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockReturnArrayFromDirectArrayInstanceRector/Fixture/return_empty.php.inc‎

Lines changed: 0 additions & 30 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\DocblockReturnArrayFromDirectArrayInstanceRector\Fixture;
4+
5+
class SkipReturnEmpty
6+
{
7+
public function run(): array
8+
{
9+
return [];
10+
}
11+
}

‎rules/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockReturnArrayFromDirectArrayInstanceRector.php‎

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use PhpParser\Node\Stmt\Function_;
1111
use PhpParser\Node\Stmt\Return_;
1212
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
13-
use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode;
1413
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
1514
use PHPStan\Type\Constant\ConstantArrayType;
1615
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
@@ -112,7 +111,7 @@ public function refactor(Node $node): ?Node
112111
return null;
113112
}
114113

115-
if ($this->shouldSkipReturnMixedAndEmptyArray($phpDocInfo, $soleReturn->expr)) {
114+
if ($this->shouldSkipEmptyArray($phpDocInfo, $soleReturn->expr)) {
116115
return null;
117116
}
118117

@@ -146,19 +145,14 @@ public function refactor(Node $node): ?Node
146145
return $node;
147146
}
148147

149-
private function shouldSkipReturnMixedAndEmptyArray(PhpDocInfo $phpDocInfo, Array_ $array): bool
148+
private function shouldSkipEmptyArray(PhpDocInfo $phpDocInfo, Array_ $array): bool
150149
{
151150
if ($array->items !== []) {
152151
return false;
153152
}
154153

155-
$returnTagValueNode = $phpDocInfo->getReturnTagValue();
156-
if (! $returnTagValueNode instanceof ReturnTagValueNode) {
157-
return false;
158-
}
159-
160-
// better than array{}
161-
return $returnTagValueNode->type instanceof ArrayTypeNode;
154+
// skip empty array; @return array{} is too narrow, except refining bare "array" to "mixed[]" is still useful
155+
return ! $this->hasBareArrayReturnTag($phpDocInfo);
162156
}
163157

164158
private function hasBareArrayReturnTag(PhpDocInfo $phpDocInfo): bool

0 commit comments

Comments
 (0)