Skip to content

Fix assertSuperType logic #4067

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 2.1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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

-
Expand Down
12 changes: 9 additions & 3 deletions src/Testing/TypeInferenceTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -270,16 +272,20 @@ 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()),
$relativePathHelper->getRelativePath($file),
$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) {
Expand Down
Loading