diff --git a/packages/CodingStyle/tests/Rector/ClassConst/VarConstantCommentRector/Fixture/union_nested_and_single_level_array_scalars.php.inc b/packages/CodingStyle/tests/Rector/ClassConst/VarConstantCommentRector/Fixture/union_nested_and_single_level_array_scalars.php.inc new file mode 100644 index 00000000000..73033f68f1d --- /dev/null +++ b/packages/CodingStyle/tests/Rector/ClassConst/VarConstantCommentRector/Fixture/union_nested_and_single_level_array_scalars.php.inc @@ -0,0 +1,37 @@ + ['value2', 1234], + 1 => null, + ]; +} + +?> +----- + ['value2', 1234], + 1 => null, + ]; +} + +?> diff --git a/packages/CodingStyle/tests/Rector/ClassConst/VarConstantCommentRector/Fixture/union_nested_array_scalars.php.inc b/packages/CodingStyle/tests/Rector/ClassConst/VarConstantCommentRector/Fixture/union_nested_array_scalars.php.inc new file mode 100644 index 00000000000..a7e1f5b7fc8 --- /dev/null +++ b/packages/CodingStyle/tests/Rector/ClassConst/VarConstantCommentRector/Fixture/union_nested_array_scalars.php.inc @@ -0,0 +1,65 @@ + 'value', + ]; + + /** + * @var mixed[] + */ + public const NESTED_STRING_INT = [ + 'key2' => ['value2', 1234], + ]; + + /** + * @var mixed[] + */ + public const STRING_AND_NULL = [ + 'key' => 'value', + 1 => null, + ]; +} + +?> +----- + 'value', + ]; + + /** + * @var int[][]|string[][] + */ + public const NESTED_STRING_INT = [ + 'key2' => ['value2', 1234], + ]; + + /** + * @var string[]|null[] + */ + public const STRING_AND_NULL = [ + 'key' => 'value', + 1 => null, + ]; +} + +?> diff --git a/packages/NodeTypeResolver/src/StaticTypeMapper.php b/packages/NodeTypeResolver/src/StaticTypeMapper.php index 5dd06c67a4f..c074928303b 100644 --- a/packages/NodeTypeResolver/src/StaticTypeMapper.php +++ b/packages/NodeTypeResolver/src/StaticTypeMapper.php @@ -114,6 +114,11 @@ final class StaticTypeMapper if ($phpStanType instanceof ArrayType || $phpStanType instanceof IterableType) { $itemTypeNode = $this->mapPHPStanTypeToPHPStanPhpDocTypeNode($phpStanType->getItemType()); + + if ($itemTypeNode instanceof UnionTypeNode) { + return $this->convertUnionArrayTypeNodesToArrayTypeOfUnionTypeNodes($itemTypeNode); + } + return new ArrayTypeNode($itemTypeNode); } @@ -868,4 +873,24 @@ final class StaticTypeMapper return is_a($secondType->getClassName(), $firstType->getClassName(), true); } + + private function convertUnionArrayTypeNodesToArrayTypeOfUnionTypeNodes( + UnionTypeNode $unionTypeNode + ): AttributeAwareUnionTypeNode { + $unionedArrayType = []; + foreach ($unionTypeNode->types as $unionedType) { + if ($unionedType instanceof UnionTypeNode) { + foreach ($unionedType->types as $key => $subUnionedType) { + $unionedType->types[$key] = new ArrayTypeNode($subUnionedType); + } + + $unionedArrayType[] = $unionedType; + continue; + } + + $unionedArrayType[] = new ArrayTypeNode($unionedType); + } + + return new AttributeAwareUnionTypeNode($unionedArrayType); + } }