Skip to content

Commit 0bddac1

Browse files
committed
SimultaneousTypeTraverserTest for improved UnionType implementation
1 parent 6597ef6 commit 0bddac1

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

tests/PHPStan/Type/SimultaneousTypeTraverserTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPStan\Type;
44

5+
use PHPStan\PhpDoc\TypeStringResolver;
56
use PHPStan\Testing\PHPStanTestCase;
67
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
78
use PHPStan\Type\Constant\ConstantArrayType;
@@ -88,4 +89,47 @@ public function testChangeIntegerIntoString(Type $left, Type $right, string $exp
8889
$this->assertSame($expectedTypeDescription, $actualType->describe(VerbosityLevel::precise()));
8990
}
9091

92+
public static function dataDescriptionBased(): iterable
93+
{
94+
$chooseScalarSubtype = static function (Type $left, Type $right, callable $traverse): Type {
95+
if (!$left->isScalar()->yes()) {
96+
return $traverse($left, $right);
97+
}
98+
if (!$right->isScalar()->yes()) {
99+
return $traverse($left, $right);
100+
}
101+
if (!$left->isSuperTypeOf($right)->yes()) {
102+
return $traverse($left, $right);
103+
}
104+
105+
return $right;
106+
};
107+
yield [
108+
'object|int',
109+
'1|2|3',
110+
$chooseScalarSubtype,
111+
'1|2|3|object',
112+
];
113+
114+
yield [
115+
'Foo|non-empty-string',
116+
"'aaa'",
117+
$chooseScalarSubtype,
118+
"'aaa'|Foo",
119+
];
120+
}
121+
122+
/**
123+
* @param callable(Type $left, Type $right, callable(Type, Type): Type $traverse): Type $callback
124+
*/
125+
#[DataProvider('dataDescriptionBased')]
126+
public function testDescriptionBased(string $left, string $right, callable $callback, string $expectedTypeDescription): void
127+
{
128+
$typeStringResolver = self::getContainer()->getByType(TypeStringResolver::class);
129+
$leftType = $typeStringResolver->resolve($left);
130+
$rightType = $typeStringResolver->resolve($right);
131+
$actualType = SimultaneousTypeTraverser::map($leftType, $rightType, $callback);
132+
$this->assertSame($expectedTypeDescription, $actualType->describe(VerbosityLevel::precise()));
133+
}
134+
91135
}

0 commit comments

Comments
 (0)