From 850807ed192c9fb53e0e8b82b4acda0a382affea Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 30 Sep 2025 09:11:03 +0700 Subject: [PATCH 1/3] [TypeDeclarationDocblocks] Use int key on inner array over mixed key on DocblockVarArrayFromPropertyDefaultsRector --- .../Fixture/int_key_inside.php.inc | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 rules-tests/TypeDeclarationDocblocks/Rector/Class_/DocblockVarArrayFromPropertyDefaultsRector/Fixture/int_key_inside.php.inc diff --git a/rules-tests/TypeDeclarationDocblocks/Rector/Class_/DocblockVarArrayFromPropertyDefaultsRector/Fixture/int_key_inside.php.inc b/rules-tests/TypeDeclarationDocblocks/Rector/Class_/DocblockVarArrayFromPropertyDefaultsRector/Fixture/int_key_inside.php.inc new file mode 100644 index 00000000000..5c16a52841d --- /dev/null +++ b/rules-tests/TypeDeclarationDocblocks/Rector/Class_/DocblockVarArrayFromPropertyDefaultsRector/Fixture/int_key_inside.php.inc @@ -0,0 +1,38 @@ + [ + 'b' => [ + ['c' => 'e', 'f' => 1], + ['d' => 2], + ], + ], + ]; +} + +?> +----- +>>> + */ + private array $array = [ + 'a' => [ + 'b' => [ + ['c' => 'e', 'f' => 1], + ['d' => 2], + ], + ], + ]; +} + +?> \ No newline at end of file From e504202654589b2256f59d01fef5b8e6a652f301 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 30 Sep 2025 09:24:44 +0700 Subject: [PATCH 2/3] Fix --- rules/Privatization/TypeManipulator/TypeNormalizer.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/rules/Privatization/TypeManipulator/TypeNormalizer.php b/rules/Privatization/TypeManipulator/TypeNormalizer.php index b1b47fcec2d..3de402ccd41 100644 --- a/rules/Privatization/TypeManipulator/TypeNormalizer.php +++ b/rules/Privatization/TypeManipulator/TypeNormalizer.php @@ -57,7 +57,8 @@ public function generalizeConstantBoolTypes(Type $type): Type */ public function generalizeConstantTypes(Type $type): Type { - return TypeTraverser::map($type, function (Type $type, callable $traverseCallback): Type { + $originalType = $type; + return TypeTraverser::map($type, function (Type $type, callable $traverseCallback) use ($originalType): Type { if ($type instanceof AccessoryNonFalsyStringType || $type instanceof AccessoryLiteralStringType || $type instanceof AccessoryNonEmptyStringType) { return new StringType(); } @@ -81,7 +82,7 @@ public function generalizeConstantTypes(Type $type): Type if ($type instanceof ConstantArrayType) { // is relevant int constantArrayType? if ($this->isImplicitNumberedListKeyType($type)) { - $keyType = new MixedType(); + $keyType = $originalType === $type ? new MixedType() : new IntegerType(); } else { $keyType = $this->generalizeConstantTypes($type->getKeyType()); } From 4b36054461dde8be6dbacda224c45697a9051168 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 30 Sep 2025 09:34:44 +0700 Subject: [PATCH 3/3] use increment --- rules/Privatization/TypeManipulator/TypeNormalizer.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/rules/Privatization/TypeManipulator/TypeNormalizer.php b/rules/Privatization/TypeManipulator/TypeNormalizer.php index 3de402ccd41..fae0c78b942 100644 --- a/rules/Privatization/TypeManipulator/TypeNormalizer.php +++ b/rules/Privatization/TypeManipulator/TypeNormalizer.php @@ -57,8 +57,9 @@ public function generalizeConstantBoolTypes(Type $type): Type */ public function generalizeConstantTypes(Type $type): Type { - $originalType = $type; - return TypeTraverser::map($type, function (Type $type, callable $traverseCallback) use ($originalType): Type { + $deep = 0; + return TypeTraverser::map($type, function (Type $type, callable $traverseCallback) use (&$deep): Type { + ++$deep; if ($type instanceof AccessoryNonFalsyStringType || $type instanceof AccessoryLiteralStringType || $type instanceof AccessoryNonEmptyStringType) { return new StringType(); } @@ -82,7 +83,7 @@ public function generalizeConstantTypes(Type $type): Type if ($type instanceof ConstantArrayType) { // is relevant int constantArrayType? if ($this->isImplicitNumberedListKeyType($type)) { - $keyType = $originalType === $type ? new MixedType() : new IntegerType(); + $keyType = $deep === 1 ? new MixedType() : new IntegerType(); } else { $keyType = $this->generalizeConstantTypes($type->getKeyType()); }