Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Jun 19, 2024
1 parent 81e5c42 commit df9fd94
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
21 changes: 16 additions & 5 deletions src/Testing/TypeInferenceTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
use PHPStan\DependencyInjection\Type\DynamicThrowTypeExtensionProvider;
use PHPStan\DependencyInjection\Type\ParameterClosureTypeExtensionProvider;
use PHPStan\File\FileHelper;
use PHPStan\File\SimpleRelativePathHelper;
use PHPStan\Php\PhpVersion;
use PHPStan\PhpDoc\PhpDocInheritanceResolver;
use PHPStan\PhpDoc\StubPhpDocProvider;
use PHPStan\Reflection\InitializerExprTypeResolver;
use PHPStan\Reflection\SignatureMap\SignatureMapProvider;
use PHPStan\Rules\Properties\ReadWritePropertiesExtensionProvider;
use PHPStan\ShouldNotHappenException;
use PHPStan\TrinaryLogic;
use PHPStan\Type\ConstantScalarType;
use PHPStan\Type\FileTypeMapper;
Expand All @@ -33,6 +35,7 @@
use function is_dir;
use function is_string;
use function preg_match;
use function realpath;
use function sprintf;
use function stripos;
use function strpos;
Expand Down Expand Up @@ -142,6 +145,7 @@ public function assertFileAsserts(
public static function gatherAssertTypes(string $file): array
{
$asserts = [];

self::processFile($file, static function (Node $node, Scope $scope) use (&$asserts, $file): void {
if (!$node instanceof Node\Expr\FuncCall) {
return;
Expand All @@ -152,25 +156,32 @@ public static function gatherAssertTypes(string $file): array
return;
}

$projectRoot = realpath(__DIR__ . '/../../');
if ($projectRoot === false) {
throw new ShouldNotHappenException();
}
$pathHelper = new SimpleRelativePathHelper($projectRoot);
$relativeFile = $pathHelper->getRelativePath($file);

$functionName = $nameNode->toString();
if (in_array(strtolower($functionName), ['asserttype', 'assertnativetype', 'assertvariablecertainty'], true)) {
self::fail(sprintf(
'Missing use statement for %s() in %s on line %d.',
$functionName,
$file,
$relativeFile,
$node->getStartLine(),
));
} elseif ($functionName === 'PHPStan\\Testing\\assertType') {
$expectedType = $scope->getType($node->getArgs()[0]->value);
if (!$expectedType instanceof ConstantScalarType) {
self::fail(sprintf('Expected type must be a literal string, %s given in %s on line %d.', $expectedType->describe(VerbosityLevel::precise()), $file, $node->getLine()));
self::fail(sprintf('Expected type must be a literal string, %s given in %s on line %d.', $expectedType->describe(VerbosityLevel::precise()), $relativeFile, $node->getLine()));
}
$actualType = $scope->getType($node->getArgs()[1]->value);
$assert = ['type', $file, $expectedType->getValue(), $actualType->describe(VerbosityLevel::precise()), $node->getStartLine()];
} elseif ($functionName === 'PHPStan\\Testing\\assertNativeType') {
$expectedType = $scope->getType($node->getArgs()[0]->value);
if (!$expectedType instanceof ConstantScalarType) {
self::fail(sprintf('Expected type must be a literal string, %s given in %s on line %d.', $expectedType->describe(VerbosityLevel::precise()), $file, $node->getLine()));
self::fail(sprintf('Expected type must be a literal string, %s given in %s on line %d.', $expectedType->describe(VerbosityLevel::precise()), $relativeFile, $node->getLine()));
}

$actualType = $scope->getNativeType($node->getArgs()[1]->value);
Expand Down Expand Up @@ -228,7 +239,7 @@ public static function gatherAssertTypes(string $file): array
'Function %s imported with wrong namespace %s called in %s on line %d.',
$correctFunction,
$functionName,
$file,
$relativeFile,
$node->getStartLine(),
));
}
Expand All @@ -237,7 +248,7 @@ public static function gatherAssertTypes(string $file): array
self::fail(sprintf(
'ERROR: Wrong %s() call in %s on line %d.',
$functionName,
$file,
$relativeFile,
$node->getStartLine(),
));
}
Expand Down
18 changes: 9 additions & 9 deletions tests/PHPStan/Testing/TypeInferenceTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,39 @@ public static function dataFileAssertionFailedErrors(): iterable
{
yield [
__DIR__ . '/data/assert-certainty-missing-namespace.php',
'Missing use statement for assertVariableCertainty() on line 8.',
'Missing use statement for assertVariableCertainty() in tests/PHPStan/Testing/data/assert-certainty-missing-namespace.php on line 8.',
];
yield [
__DIR__ . '/data/assert-native-type-missing-namespace.php',
'Missing use statement for assertNativeType() on line 6.',
'Missing use statement for assertNativeType() in tests/PHPStan/Testing/data/assert-native-type-missing-namespace.php on line 6.',
];
yield [
__DIR__ . '/data/assert-type-missing-namespace.php',
'Missing use statement for assertType() on line 6.',
'Missing use statement for assertType() in tests/PHPStan/Testing/data/assert-type-missing-namespace.php on line 6.',
];
yield [
__DIR__ . '/data/assert-certainty-wrong-namespace.php',
'Function PHPStan\Testing\assertVariableCertainty imported with wrong namespace SomeWrong\Namespace\assertVariableCertainty called on line 9.',
'Function PHPStan\Testing\assertVariableCertainty imported with wrong namespace SomeWrong\Namespace\assertVariableCertainty called in tests/PHPStan/Testing/data/assert-certainty-wrong-namespace.php on line 9.',
];
yield [
__DIR__ . '/data/assert-native-type-wrong-namespace.php',
'Function PHPStan\Testing\assertNativeType imported with wrong namespace SomeWrong\Namespace\assertNativeType called on line 8.',
'Function PHPStan\Testing\assertNativeType imported with wrong namespace SomeWrong\Namespace\assertNativeType called in tests/PHPStan/Testing/data/assert-native-type-wrong-namespace.php on line 8.',
];
yield [
__DIR__ . '/data/assert-type-wrong-namespace.php',
'Function PHPStan\Testing\assertType imported with wrong namespace SomeWrong\Namespace\assertType called on line 8.',
'Function PHPStan\Testing\assertType imported with wrong namespace SomeWrong\Namespace\assertType called in tests/PHPStan/Testing/data/assert-type-wrong-namespace.php on line 8.',
];
yield [
__DIR__ . '/data/assert-certainty-case-insensitive.php',
'Missing use statement for assertvariablecertainty() on line 8.',
'Missing use statement for assertvariablecertainty() in tests/PHPStan/Testing/data/assert-certainty-case-insensitive.php on line 8.',
];
yield [
__DIR__ . '/data/assert-native-type-case-insensitive.php',
'Missing use statement for assertNATIVEType() on line 6.',
'Missing use statement for assertNATIVEType() in tests/PHPStan/Testing/data/assert-native-type-case-insensitive.php on line 6.',
];
yield [
__DIR__ . '/data/assert-type-case-insensitive.php',
'Missing use statement for assertTYPe() on line 6.',
'Missing use statement for assertTYPe() in tests/PHPStan/Testing/data/assert-type-case-insensitive.php on line 6.',
];
}

Expand Down

0 comments on commit df9fd94

Please sign in to comment.