Skip to content

Commit fd45885

Browse files
committed
UnionType - do not put parentheses around IntersectionType if parts of it are hidden
1 parent 1a4f50f commit fd45885

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/Type/UnionType.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,15 @@ public function describe(VerbosityLevel $level): string
133133
$joinTypes = static function (array $types) use ($level): string {
134134
$typeNames = [];
135135
foreach ($types as $type) {
136-
if ($type instanceof IntersectionType || $type instanceof ClosureType || $type instanceof CallableType) {
136+
if ($type instanceof ClosureType || $type instanceof CallableType) {
137137
$typeNames[] = sprintf('(%s)', $type->describe($level));
138+
} elseif ($type instanceof IntersectionType) {
139+
$intersectionDescription = $type->describe($level);
140+
if (strpos($intersectionDescription, '&') !== false) {
141+
$typeNames[] = sprintf('(%s)', $type->describe($level));
142+
} else {
143+
$typeNames[] = $intersectionDescription;
144+
}
138145
} else {
139146
$typeNames[] = $type->describe($level);
140147
}

tests/PHPStan/Type/UnionTypeTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHPStan\Reflection\Native\NativeParameterReflection;
66
use PHPStan\Reflection\PassedByReference;
77
use PHPStan\TrinaryLogic;
8+
use PHPStan\Type\Accessory\AccessoryNumericStringType;
89
use PHPStan\Type\Constant\ConstantArrayType;
910
use PHPStan\Type\Constant\ConstantBooleanType;
1011
use PHPStan\Type\Constant\ConstantFloatType;
@@ -576,6 +577,17 @@ public function dataDescribe(): array
576577
'array()|array(\'foooo\' => \'barrr\')',
577578
'array<string, string>',
578579
],
580+
[
581+
TypeCombinator::union(
582+
new IntegerType(),
583+
new IntersectionType([
584+
new StringType(),
585+
new AccessoryNumericStringType(),
586+
]),
587+
),
588+
'int|(string&numeric)',
589+
'int|string',
590+
],
579591
];
580592
}
581593

0 commit comments

Comments
 (0)