Skip to content

Commit 7ecd72d

Browse files
authored
[TypeDeclarationDocblocks] Handle true[]|false[] on DocblockReturnArrayFromDirectArrayInstanceRector (#7356)
1 parent 5a54f35 commit 7ecd72d

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function FalseAndTrue()
1919
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\ClassMethod\DocblockReturnArrayFromDirectArrayInstanceRector\Fixture;
2020

2121
/**
22-
* @return array<string, true[]|false[]>
22+
* @return array<string, bool[]>
2323
*/
2424
function FalseAndTrue()
2525
{

rules/Privatization/TypeManipulator/TypeNormalizer.php

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use PHPStan\Type\TypeTraverser;
2121
use PHPStan\Type\UnionType;
2222
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
23+
use Rector\NodeTypeResolver\PHPStan\TypeHasher;
2324
use Rector\StaticTypeMapper\StaticTypeMapper;
2425

2526
final readonly class TypeNormalizer
@@ -32,7 +33,8 @@
3233
public function __construct(
3334
private TypeFactory $typeFactory,
3435
private StaticTypeMapper $staticTypeMapper,
35-
private ArrayTypeLeastCommonDenominatorResolver $arrayTypeLeastCommonDenominatorResolver
36+
private ArrayTypeLeastCommonDenominatorResolver $arrayTypeLeastCommonDenominatorResolver,
37+
private TypeHasher $typeHasher
3638
) {
3739

3840
}
@@ -94,7 +96,41 @@ public function generalizeConstantTypes(Type $type): Type
9496
if ($type instanceof UnionType) {
9597
$generalizedUnionedTypes = [];
9698
foreach ($type->getTypes() as $unionedType) {
97-
$generalizedUnionedTypes[] = $this->generalizeConstantTypes($unionedType);
99+
$generalizedUnionedType = $this->generalizeConstantTypes($unionedType);
100+
101+
if ($generalizedUnionedType instanceof ArrayType) {
102+
$keyType = $this->typeHasher->createTypeHash($generalizedUnionedType->getKeyType());
103+
104+
foreach ($generalizedUnionedTypes as $key => $existingUnionedType) {
105+
if (! $existingUnionedType instanceof ArrayType) {
106+
continue;
107+
}
108+
109+
$existingKeyType = $this->typeHasher->createTypeHash(
110+
$existingUnionedType->getKeyType()
111+
);
112+
113+
if ($keyType !== $existingKeyType) {
114+
continue;
115+
}
116+
117+
$uniqueTypes = $this->typeFactory->uniquateTypes(
118+
[$existingUnionedType->getItemType(), $generalizedUnionedType->getItemType()]
119+
);
120+
121+
if (count($uniqueTypes) !== 1) {
122+
continue;
123+
}
124+
125+
$generalizedUnionedTypes[$key] = new ArrayType(
126+
$existingUnionedType->getKeyType(),
127+
$uniqueTypes[0]
128+
);
129+
continue 2;
130+
}
131+
}
132+
133+
$generalizedUnionedTypes[] = $generalizedUnionedType;
98134
}
99135

100136
$uniqueGeneralizedUnionTypes = $this->typeFactory->uniquateTypes($generalizedUnionedTypes);

0 commit comments

Comments
 (0)