Skip to content

Commit 732444f

Browse files
committed
keep unioned types scalar
1 parent 3879898 commit 732444f

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

rules/CodingStyle/Rector/Enum_/EnumCaseToPascalCaseRector.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ private function isEnumCase(ClassReflection $classReflection, string $name, stri
195195
if ($classReflection->hasEnumCase($name)) {
196196
return true;
197197
}
198+
198199
return $classReflection->hasEnumCase($pascalName);
199200
}
200201

rules/TypeDeclarationDocblocks/Rector/ClassMethod/DocblockReturnArrayFromDirectArrayInstanceRector.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@
1616
use PHPStan\Type\Constant\ConstantArrayType;
1717
use PHPStan\Type\FloatType;
1818
use PHPStan\Type\IntegerType;
19+
use PHPStan\Type\IntersectionType;
1920
use PHPStan\Type\MixedType;
2021
use PHPStan\Type\NeverType;
2122
use PHPStan\Type\StringType;
2223
use PHPStan\Type\Type;
24+
use PHPStan\Type\UnionType;
2325
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
2426
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
27+
use Rector\NodeTypeResolver\PHPStan\Type\TypeFactory;
2528
use Rector\Rector\AbstractRector;
2629
use Rector\StaticTypeMapper\StaticTypeMapper;
2730
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@@ -35,7 +38,8 @@ final class DocblockReturnArrayFromDirectArrayInstanceRector extends AbstractRec
3538
public function __construct(
3639
private readonly PhpDocInfoFactory $phpDocInfoFactory,
3740
private readonly DocBlockUpdater $docBlockUpdater,
38-
private readonly StaticTypeMapper $staticTypeMapper
41+
private readonly StaticTypeMapper $staticTypeMapper,
42+
private readonly TypeFactory $typeFactory
3943
) {
4044
}
4145

@@ -145,6 +149,20 @@ private function constantToGenericType(Type $type): Type
145149
return new FloatType();
146150
}
147151

152+
if ($type instanceof UnionType || $type instanceof IntersectionType) {
153+
$genericComplexTypes = [];
154+
foreach ($type->getTypes() as $splitType) {
155+
$genericComplexTypes[] = $this->constantToGenericType($splitType);
156+
}
157+
158+
$genericComplexTypes = $this->typeFactory->uniquateTypes($genericComplexTypes);
159+
if (count($genericComplexTypes) > 1) {
160+
return new UnionType($genericComplexTypes);
161+
}
162+
163+
return $genericComplexTypes[0];
164+
}
165+
148166
// unclear
149167
return new MixedType();
150168
}

0 commit comments

Comments
 (0)