Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\DocblockReturnArrayFromDirectArrayInstanceRector\Fixture;

class SkipReturnEmpty
{
public function run(): array
{
return [];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Return_;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode;
use PHPStan\Type\Constant\ConstantArrayType;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTypeChanger;
use Rector\Rector\AbstractRector;
Expand Down Expand Up @@ -110,7 +107,8 @@ public function refactor(Node $node): ?Node
return null;
}

if ($this->shouldSkipReturnMixedAndEmptyArray($phpDocInfo, $soleReturn->expr)) {
// skip empty array; @return array{} is the narrowest array type and breaks any child override that returns a filled array
if ($soleReturn->expr->items === []) {
return null;
}

Expand All @@ -132,19 +130,4 @@ public function refactor(Node $node): ?Node

return $node;
}

private function shouldSkipReturnMixedAndEmptyArray(PhpDocInfo $phpDocInfo, Array_ $array): bool
{
if ($array->items !== []) {
return false;
}

$returnTagValueNode = $phpDocInfo->getReturnTagValue();
if (! $returnTagValueNode instanceof ReturnTagValueNode) {
return false;
}

// better than array{}
return $returnTagValueNode->type instanceof ArrayTypeNode;
}
}