Skip to content

Commit da87a65

Browse files
committed
TypeInferenceTestCase - potential fix for performance problem with PHPUnit 11
See phpstan/phpstan#10757
1 parent fd2e0a6 commit da87a65

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

src/Testing/TypeInferenceTestCase.php

+22-7
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use PHPStan\TrinaryLogic;
2020
use PHPStan\Type\ConstantScalarType;
2121
use PHPStan\Type\FileTypeMapper;
22+
use PHPStan\Type\Type;
2223
use PHPStan\Type\VerbosityLevel;
2324
use function array_map;
2425
use function array_merge;
@@ -95,11 +96,18 @@ public function assertFileAsserts(
9596
): void
9697
{
9798
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+
103111
$this->assertSame(
104112
$expected,
105113
$actual,
@@ -142,12 +150,19 @@ public static function gatherAssertTypes(string $file): array
142150
));
143151
} elseif ($functionName === 'PHPStan\\Testing\\assertType') {
144152
$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+
}
145156
$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()];
147158
} elseif ($functionName === 'PHPStan\\Testing\\assertNativeType') {
148159
$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+
149164
$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()];
151166
} elseif ($functionName === 'PHPStan\\Testing\\assertVariableCertainty') {
152167
$certainty = $node->getArgs()[0]->value;
153168
if (!$certainty instanceof StaticCall) {

0 commit comments

Comments
 (0)