|
19 | 19 | use PHPStan\TrinaryLogic;
|
20 | 20 | use PHPStan\Type\ConstantScalarType;
|
21 | 21 | use PHPStan\Type\FileTypeMapper;
|
| 22 | +use PHPStan\Type\Type; |
22 | 23 | use PHPStan\Type\VerbosityLevel;
|
23 | 24 | use function array_map;
|
24 | 25 | use function array_merge;
|
@@ -95,11 +96,18 @@ public function assertFileAsserts(
|
95 | 96 | ): void
|
96 | 97 | {
|
97 | 98 | if ($assertType === 'type') {
|
98 |
| - $expectedType = $args[0]; |
99 |
| - $this->assertInstanceOf(ConstantScalarType::class, $expectedType); |
100 |
| - $expected = $expectedType->getValue(); |
101 |
| - $actualType = $args[1]; |
102 |
| - $actual = $actualType->describe(VerbosityLevel::precise()); |
| 99 | + if ($args[0] instanceof Type) { |
| 100 | + // backward compatibility |
| 101 | + $expectedType = $args[0]; |
| 102 | + $this->assertInstanceOf(ConstantScalarType::class, $expectedType); |
| 103 | + $expected = $expectedType->getValue(); |
| 104 | + $actualType = $args[1]; |
| 105 | + $actual = $actualType->describe(VerbosityLevel::precise()); |
| 106 | + } else { |
| 107 | + $expected = $args[0]; |
| 108 | + $actual = $args[1]; |
| 109 | + } |
| 110 | + |
103 | 111 | $this->assertSame(
|
104 | 112 | $expected,
|
105 | 113 | $actual,
|
@@ -142,12 +150,19 @@ public static function gatherAssertTypes(string $file): array
|
142 | 150 | ));
|
143 | 151 | } elseif ($functionName === 'PHPStan\\Testing\\assertType') {
|
144 | 152 | $expectedType = $scope->getType($node->getArgs()[0]->value);
|
| 153 | + if (!$expectedType instanceof ConstantScalarType) { |
| 154 | + self::fail(sprintf('Expected type must be a literal string, %s given on line %d.', $expectedType->describe(VerbosityLevel::precise()), $node->getLine())); |
| 155 | + } |
145 | 156 | $actualType = $scope->getType($node->getArgs()[1]->value);
|
146 |
| - $assert = ['type', $file, $expectedType, $actualType, $node->getLine()]; |
| 157 | + $assert = ['type', $file, $expectedType->getValue(), $actualType->describe(VerbosityLevel::precise()), $node->getLine()]; |
147 | 158 | } elseif ($functionName === 'PHPStan\\Testing\\assertNativeType') {
|
148 | 159 | $expectedType = $scope->getType($node->getArgs()[0]->value);
|
| 160 | + if (!$expectedType instanceof ConstantScalarType) { |
| 161 | + self::fail(sprintf('Expected type must be a literal string, %s given on line %d.', $expectedType->describe(VerbosityLevel::precise()), $node->getLine())); |
| 162 | + } |
| 163 | + |
149 | 164 | $actualType = $scope->getNativeType($node->getArgs()[1]->value);
|
150 |
| - $assert = ['type', $file, $expectedType, $actualType, $node->getLine()]; |
| 165 | + $assert = ['type', $file, $expectedType->getValue(), $actualType->describe(VerbosityLevel::precise()), $node->getLine()]; |
151 | 166 | } elseif ($functionName === 'PHPStan\\Testing\\assertVariableCertainty') {
|
152 | 167 | $certainty = $node->getArgs()[0]->value;
|
153 | 168 | if (!$certainty instanceof StaticCall) {
|
|
0 commit comments