Skip to content

Commit ce2b370

Browse files
authored
[TypeDeclarationDocblocks] Avoid array<mixed, mixed> on return empty array on DocblockReturnArrayFromDirectArrayInstanceRector (#7260)
* [TypeDeclarationDocblocks] Avoid array<mixed, mixed> on return empty array on DocblockReturnArrayFromDirectArrayInstanceRector * return * fix * fix
1 parent 45af749 commit ce2b370

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\DocblockReturnArrayFromDirectArrayInstanceRector\Fixture;
4+
5+
class ReturnEmpty
6+
{
7+
public function run(): array
8+
{
9+
return [];
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\DocblockReturnArrayFromDirectArrayInstanceRector\Fixture;
18+
19+
class ReturnEmpty
20+
{
21+
/**
22+
* @return array<int, mixed>
23+
*/
24+
public function run(): array
25+
{
26+
return [];
27+
}
28+
}
29+
30+
?>

rules/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockReturnArrayFromDirectArrayInstanceRector.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use PHPStan\Type\FloatType;
1818
use PHPStan\Type\IntegerType;
1919
use PHPStan\Type\MixedType;
20+
use PHPStan\Type\NeverType;
2021
use PHPStan\Type\StringType;
2122
use PHPStan\Type\Type;
2223
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
@@ -152,6 +153,10 @@ private function createGenericArrayTypeFromConstantArrayType(ConstantArrayType $
152153
{
153154
$genericKeyType = $this->constantToGenericType($constantArrayType->getKeyType());
154155

156+
if ($constantArrayType->getItemType() instanceof NeverType) {
157+
$genericKeyType = new IntegerType();
158+
}
159+
155160
$itemType = $constantArrayType->getItemType();
156161
if ($itemType instanceof ConstantArrayType) {
157162
$genericItemType = $this->createGenericArrayTypeFromConstantArrayType($itemType);

0 commit comments

Comments
 (0)