Skip to content

Commit c5e7624

Browse files
authored
[TypeDeclarationDocblocks] Use int key on inner array over mixed key on DocblockVarArrayFromPropertyDefaultsRector (#7374)
* [TypeDeclarationDocblocks] Use int key on inner array over mixed key on DocblockVarArrayFromPropertyDefaultsRector * Fix * use increment
1 parent c96c2e7 commit c5e7624

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\DocblockVarArrayFromPropertyDefaultsRector\Fixture;
4+
5+
final class IntKeyInside
6+
{
7+
private array $array = [
8+
'a' => [
9+
'b' => [
10+
['c' => 'e', 'f' => 1],
11+
['d' => 2],
12+
],
13+
],
14+
];
15+
}
16+
17+
?>
18+
-----
19+
<?php
20+
21+
namespace Rector\Tests\TypeDeclarationDocblocks\Rector\Class_\DocblockVarArrayFromPropertyDefaultsRector\Fixture;
22+
23+
final class IntKeyInside
24+
{
25+
/**
26+
* @var array<string, array<string, array<int, array<string, string|int>>>>
27+
*/
28+
private array $array = [
29+
'a' => [
30+
'b' => [
31+
['c' => 'e', 'f' => 1],
32+
['d' => 2],
33+
],
34+
],
35+
];
36+
}
37+
38+
?>

rules/Privatization/TypeManipulator/TypeNormalizer.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ public function generalizeConstantBoolTypes(Type $type): Type
5757
*/
5858
public function generalizeConstantTypes(Type $type): Type
5959
{
60-
return TypeTraverser::map($type, function (Type $type, callable $traverseCallback): Type {
60+
$deep = 0;
61+
return TypeTraverser::map($type, function (Type $type, callable $traverseCallback) use (&$deep): Type {
62+
++$deep;
6163
if ($type instanceof AccessoryNonFalsyStringType || $type instanceof AccessoryLiteralStringType || $type instanceof AccessoryNonEmptyStringType) {
6264
return new StringType();
6365
}
@@ -81,7 +83,7 @@ public function generalizeConstantTypes(Type $type): Type
8183
if ($type instanceof ConstantArrayType) {
8284
// is relevant int constantArrayType?
8385
if ($this->isImplicitNumberedListKeyType($type)) {
84-
$keyType = new MixedType();
86+
$keyType = $deep === 1 ? new MixedType() : new IntegerType();
8587
} else {
8688
$keyType = $this->generalizeConstantTypes($type->getKeyType());
8789
}

0 commit comments

Comments
 (0)