Skip to content

Commit 331149d

Browse files
committed
Fix
1 parent 8a9aaa8 commit 331149d

1 file changed

Lines changed: 22 additions & 2 deletions

File tree

src/PHPStanStaticTypeMapper/TypeMapper/UnionTypeMapper.php

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use PHPStan\Type\Type;
2020
use PHPStan\Type\UnionType;
2121
use Rector\BetterPhpDocParser\ValueObject\Type\BracketsAwareUnionTypeNode;
22+
use Rector\BetterPhpDocParser\ValueObject\Type\SpacingAwareArrayTypeNode;
2223
use Rector\NodeAnalyzer\PropertyAnalyzer;
2324
use Rector\Php\PhpVersionProvider;
2425
use Rector\PHPStanStaticTypeMapper\Contract\TypeMapperInterface;
@@ -57,13 +58,32 @@ public function getNodeClass(): string
5758
public function mapToPHPStanPhpDocTypeNode(Type $type): TypeNode
5859
{
5960
$unionTypesNodes = [];
61+
$existingTypes = [];
62+
6063
foreach ($type->getTypes() as $unionedType) {
6164
if ($unionedType instanceof ArrayType && $unionedType->getItemType() instanceof NeverType) {
6265
$unionedType = new ArrayType($unionedType->getKeyType(), new MixedType());
6366
}
6467

65-
// here to debug ...
66-
$unionTypesNodes[] = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($unionedType);
68+
$unionedType = $this->phpStanStaticTypeMapper->mapToPHPStanPhpDocTypeNode($unionedType);
69+
70+
if ($unionedType instanceof SpacingAwareArrayTypeNode && $unionedType->type instanceof BracketsAwareUnionTypeNode) {
71+
foreach ($unionedType->type->types as $key => $innerTypeNode) {
72+
$printedInnerType = (string) $innerTypeNode;
73+
if (in_array($printedInnerType, $existingTypes, true)) {
74+
unset($unionedType->type->types[$key]);
75+
continue;
76+
}
77+
78+
$existingTypes[] = $printedInnerType;
79+
}
80+
81+
if ($unionedType->type->types === []) {
82+
continue;
83+
}
84+
}
85+
86+
$unionTypesNodes[] = $unionedType;
6787
}
6888

6989
return new BracketsAwareUnionTypeNode($unionTypesNodes);

0 commit comments

Comments
 (0)