Skip to content
Merged
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 @@ -10,7 +10,6 @@
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Return_;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use PHPStan\PhpDocParser\Ast\Type\ArrayTypeNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\Type\Constant\ConstantArrayType;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
Expand Down Expand Up @@ -112,7 +111,7 @@ public function refactor(Node $node): ?Node
return null;
}

if ($this->shouldSkipReturnMixedAndEmptyArray($phpDocInfo, $soleReturn->expr)) {
if ($this->shouldSkipEmptyArray($phpDocInfo, $soleReturn->expr)) {
return null;
}

Expand Down Expand Up @@ -146,19 +145,14 @@ public function refactor(Node $node): ?Node
return $node;
}

private function shouldSkipReturnMixedAndEmptyArray(PhpDocInfo $phpDocInfo, Array_ $array): bool
private function shouldSkipEmptyArray(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;
// skip empty array; @return array{} is too narrow, except refining bare "array" to "mixed[]" is still useful
return ! $this->hasBareArrayReturnTag($phpDocInfo);
}

private function hasBareArrayReturnTag(PhpDocInfo $phpDocInfo): bool
Expand Down
Loading