|
2 | 2 |
|
3 | 3 | namespace PHPStan\Type; |
4 | 4 |
|
| 5 | +use PHPStan\PhpDoc\TypeStringResolver; |
5 | 6 | use PHPStan\Testing\PHPStanTestCase; |
6 | 7 | use PHPStan\Type\Accessory\AccessoryNonEmptyStringType; |
7 | 8 | use PHPStan\Type\Constant\ConstantArrayType; |
@@ -88,4 +89,47 @@ public function testChangeIntegerIntoString(Type $left, Type $right, string $exp |
88 | 89 | $this->assertSame($expectedTypeDescription, $actualType->describe(VerbosityLevel::precise())); |
89 | 90 | } |
90 | 91 |
|
| 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 | + |
91 | 135 | } |
0 commit comments