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 2 commits 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
4 changes: 2 additions & 2 deletions 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 Expand Up @@ -2022,7 +2022,7 @@ parameters:
-
message: '#^Access to constant on internal class PHPUnit\\Framework\\AssertionFailedError\.$#'
identifier: classConstant.internalClass
count: 1
count: 2
path: tests/PHPStan/Testing/TypeInferenceTestCaseTest.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
43 changes: 43 additions & 0 deletions tests/PHPStan/Testing/TypeInferenceTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ public static function dataFileAssertionFailedErrors(): iterable
$fileHelper->normalizePath('tests/PHPStan/Testing/data/assert-type-missing-namespace.php'),
),
];
yield [
__DIR__ . '/data/assert-super-type-missing-namespace.php',
sprintf(
'Missing use statement for assertSuperType() in %s on line 6.',
$fileHelper->normalizePath('tests/PHPStan/Testing/data/assert-super-type-missing-namespace.php'),
),
];
yield [
__DIR__ . '/data/assert-certainty-wrong-namespace.php',
sprintf(
Expand All @@ -58,6 +65,13 @@ public static function dataFileAssertionFailedErrors(): iterable
$fileHelper->normalizePath('tests/PHPStan/Testing/data/assert-type-wrong-namespace.php'),
),
];
yield [
__DIR__ . '/data/assert-super-type-wrong-namespace.php',
sprintf(
'Function PHPStan\Testing\assertSuperType imported with wrong namespace SomeWrong\Namespace\assertSuperType called in %s on line 8.',
$fileHelper->normalizePath('tests/PHPStan/Testing/data/assert-super-type-wrong-namespace.php'),
),
];
yield [
__DIR__ . '/data/assert-certainty-case-insensitive.php',
sprintf(
Expand All @@ -79,6 +93,13 @@ public static function dataFileAssertionFailedErrors(): iterable
$fileHelper->normalizePath('tests/PHPStan/Testing/data/assert-type-case-insensitive.php'),
),
];
yield [
__DIR__ . '/data/assert-super-type-case-insensitive.php',
sprintf(
'Missing use statement for assertSuperTYPe() in %s on line 6.',
$fileHelper->normalizePath('tests/PHPStan/Testing/data/assert-super-type-case-insensitive.php'),
),
];
}

#[DataProvider('dataFileAssertionFailedErrors')]
Expand All @@ -100,6 +121,28 @@ public function testVariableOrOffsetDescription(): void
$this->assertSame("offset 'email'", $offsetAssert[4]);
}

public function testSuperType(): void
{
foreach (self::gatherAssertTypes(__DIR__ . '/data/assert-super-type.php') as $data) {
$this->assertFileAsserts(...$data);
}
}

public static function dataSuperTypeFailed(): array
{
return self::gatherAssertTypes(__DIR__ . '/data/assert-super-type-failed.php');
}

/**
* @param mixed ...$args
*/
#[DataProvider('dataSuperTypeFailed')]
public function testSuperTypeFailed(...$args): void
{
$this->expectException(AssertionFailedError::class);
$this->assertFileAsserts(...$args);
}

public function testNonexistentClassInAnalysedFile(): void
{
foreach (self::gatherAssertTypes(__DIR__ . '/../../notAutoloaded/nonexistentClasses.php') as $data) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php // lint >= 8.0

namespace MissingTypeCaseSensitive;

function doFoo(string $s) {
assertSuperTYPe('string', $s);
}

9 changes: 9 additions & 0 deletions tests/PHPStan/Testing/data/assert-super-type-failed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

use function PHPStan\Testing\assertSuperType;

$a = 'Alice';

assertSuperType('never', $a);
assertSuperType('bool', $a);
assertSuperType('"Bob"', $a);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php // lint >= 8.0

namespace MissingAssertTypeNamespace;

function doFoo(string $s) {
assertSuperType('string', $s);
}

10 changes: 10 additions & 0 deletions tests/PHPStan/Testing/data/assert-super-type-wrong-namespace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php // lint >= 8.0

namespace WrongAssertTypeNamespace;

use function SomeWrong\Namespace\assertSuperType;

function doFoo(string $s) {
assertSuperType('string', $s);
}

8 changes: 8 additions & 0 deletions tests/PHPStan/Testing/data/assert-super-type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

use function PHPStan\Testing\assertSuperType;

$a = 'Alice';

assertSuperType('string', $a);
assertSuperType('mixed', $a);
Loading