diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 94d21ed70e..a8434f20f9 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -780,7 +780,7 @@ parameters: - message: '#^Doing instanceof PHPStan\\Type\\ConstantScalarType is error\-prone and deprecated\. Use Type\:\:isConstantScalarValue\(\) or Type\:\:getConstantScalarTypes\(\) or Type\:\:getConstantScalarValues\(\) instead\.$#' identifier: phpstanApi.instanceofType - count: 3 + count: 2 path: src/Testing/TypeInferenceTestCase.php - diff --git a/src/Testing/TypeInferenceTestCase.php b/src/Testing/TypeInferenceTestCase.php index 1793d7b1ec..227ab22fe1 100644 --- a/src/Testing/TypeInferenceTestCase.php +++ b/src/Testing/TypeInferenceTestCase.php @@ -17,6 +17,7 @@ use PHPStan\Php\PhpVersion; use PHPStan\PhpDoc\PhpDocInheritanceResolver; use PHPStan\PhpDoc\StubPhpDocProvider; +use PHPStan\PhpDoc\TypeStringResolver; use PHPStan\Reflection\AttributeReflectionFactory; use PHPStan\Reflection\Deprecation\DeprecationProvider; use PHPStan\Reflection\InitializerExprTypeResolver; @@ -203,12 +204,13 @@ public static function gatherAssertTypes(string $file): array $relativePathHelper = new SystemAgnosticSimpleRelativePathHelper($fileHelper); $reflectionProvider = self::getContainer()->getByType(ReflectionProvider::class); + $typeStringResolver = self::getContainer()->getByType(TypeStringResolver::class); $file = $fileHelper->normalizePath($file); $asserts = []; $delayedErrors = []; - self::processFile($file, static function (Node $node, Scope $scope) use (&$asserts, &$delayedErrors, $file, $relativePathHelper, $reflectionProvider): void { + self::processFile($file, static function (Node $node, Scope $scope) use (&$asserts, &$delayedErrors, $file, $relativePathHelper, $reflectionProvider, $typeStringResolver): void { if ($node instanceof InClassNode) { if (!$reflectionProvider->hasClass($node->getClassReflection()->getName())) { $delayedErrors[] = sprintf( @@ -270,7 +272,8 @@ public static function gatherAssertTypes(string $file): array $assert = ['type', $file, $expectedType->getValue(), $actualType->describe(VerbosityLevel::precise()), $node->getStartLine()]; } elseif ($functionName === 'PHPStan\\Testing\\assertSuperType') { $expectedType = $scope->getType($node->getArgs()[0]->value); - if (!$expectedType instanceof ConstantScalarType) { + $expectedTypeStrings = $expectedType->getConstantStrings(); + if (count($expectedTypeStrings) !== 1) { self::fail(sprintf( 'Expected super type must be a literal string, %s given in %s on line %d.', $expectedType->describe(VerbosityLevel::precise()), @@ -278,8 +281,11 @@ public static function gatherAssertTypes(string $file): array $node->getStartLine(), )); } + $actualType = $scope->getType($node->getArgs()[1]->value); - $assert = ['superType', $file, $expectedType->getValue(), $actualType->describe(VerbosityLevel::precise()), $expectedType->isSuperTypeOf($actualType)->yes(), $node->getStartLine()]; + $isCorrect = $typeStringResolver->resolve($expectedTypeStrings[0]->getValue())->isSuperTypeOf($actualType)->yes(); + + $assert = ['superType', $file, $expectedTypeStrings[0]->getValue(), $actualType->describe(VerbosityLevel::precise()), $isCorrect, $node->getStartLine()]; } elseif ($functionName === 'PHPStan\\Testing\\assertVariableCertainty') { $certainty = $node->getArgs()[0]->value; if (!$certainty instanceof StaticCall) {