Skip to content

Commit

Permalink
Fix ArrayTypeMapper to handle arrays with UnionType items (#8225) (#5568
Browse files Browse the repository at this point in the history
)
  • Loading branch information
pkvach committed Feb 6, 2024
1 parent 7976482 commit 6fe2585
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/PHPStanStaticTypeMapper/TypeMapper/ArrayTypeMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,20 @@ public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
// then e.g. "int" instead of explicit number, and nice arrays
$itemType = $type->getItemType();

if ($itemType instanceof UnionType && ! $type instanceof ConstantArrayType) {
$isGenericArray = $this->isGenericArrayCandidate($type);

if ($itemType instanceof UnionType
&& ! $type instanceof ConstantArrayType
&& ! $isGenericArray
) {
return $this->createArrayTypeNodeFromUnionType($itemType);
}

if ($itemType instanceof ArrayType && $this->isGenericArrayCandidate($itemType)) {
return $this->createGenericArrayType($type, true);
}

if ($this->isGenericArrayCandidate($type)) {
if ($isGenericArray) {
return $this->createGenericArrayType($type, true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,8 @@ public static function provideDataWithKeys(): Iterator

$arrayType = new ArrayType(new StringType(), new IntegerType());
yield [$arrayType, 'array<string, int>'];

$arrayType = new ArrayType(new StringType(), $stringAndIntegerUnionType);
yield [$arrayType, 'array<string, string|int>'];
}
}

0 comments on commit 6fe2585

Please sign in to comment.