Skip to content

Commit 3e0fe8d

Browse files
authored
[TypeDeclarationDocblocks] Skip mixed[] and mixed|null on DocblockGetterReturnArrayFromPropertyDocblockVarRector (#7261)
1 parent ce2b370 commit 3e0fe8d

3 files changed

Lines changed: 40 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\DocblockGetterReturnArrayFromPropertyDocblockVarRector\Fixture;
4+
5+
final class SkipMixedArray
6+
{
7+
private array $names = [];
8+
9+
public function getNames(): array
10+
{
11+
return $this->names;
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\DocblockGetterReturnArrayFromPropertyDocblockVarRector\Fixture;
4+
5+
final class SkipNullableMixedArray
6+
{
7+
private ?array $names = [];
8+
9+
public function getNames(): array
10+
{
11+
return $this->names;
12+
}
13+
}

rules/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockGetterReturnArrayFromPropertyDocblockVarRector.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
use PhpParser\Node\Stmt\ClassMethod;
1010
use PhpParser\Node\Stmt\Return_;
1111
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
12+
use PHPStan\Type\ArrayType;
13+
use PHPStan\Type\MixedType;
14+
use PHPStan\Type\UnionType;
1215
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
1316
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
1417
use Rector\Rector\AbstractRector;
@@ -99,6 +102,17 @@ public function refactor(Node $node): ?Node
99102
}
100103

101104
$propertyFetchType = $this->getType($propertyFetch);
105+
if ($propertyFetchType instanceof ArrayType
106+
&& $propertyFetchType->getKeyType() instanceof MixedType
107+
&& $propertyFetchType->getItemType() instanceof MixedType
108+
) {
109+
return null;
110+
}
111+
112+
if ($propertyFetchType instanceof UnionType) {
113+
return null;
114+
}
115+
102116
$propertyFetchDocTypeNode = $this->staticTypeMapper->mapPHPStanTypeToPHPStanPhpDocTypeNode($propertyFetchType);
103117

104118
$returnTagValueNode = new ReturnTagValueNode($propertyFetchDocTypeNode, '');

0 commit comments

Comments
 (0)