Skip to content

Commit

Permalink
minimize IO
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Jun 19, 2024
1 parent df9fd94 commit b6a4f4f
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions src/Testing/TypeInferenceTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,14 @@ public function assertFileAsserts(
*/
public static function gatherAssertTypes(string $file): array
{
$asserts = [];
$projectRoot = realpath(__DIR__ . '/../../');
if ($projectRoot === false) {
throw new ShouldNotHappenException();
}
$pathHelper = new SimpleRelativePathHelper($projectRoot);

self::processFile($file, static function (Node $node, Scope $scope) use (&$asserts, $file): void {
$asserts = [];
self::processFile($file, static function (Node $node, Scope $scope) use (&$asserts, $file, $pathHelper): void {
if (!$node instanceof Node\Expr\FuncCall) {
return;
}
Expand All @@ -156,32 +161,35 @@ 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,
$relativeFile,
$pathHelper->getRelativePath($file),
$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()), $relativeFile, $node->getLine()));
self::fail(sprintf(
'Expected type must be a literal string, %s given in %s on line %d.',
$expectedType->describe(VerbosityLevel::precise()),
$pathHelper->getRelativePath($file),
$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()), $relativeFile, $node->getLine()));
self::fail(sprintf(
'Expected type must be a literal string, %s given in %s on line %d.',
$expectedType->describe(VerbosityLevel::precise()),
$pathHelper->getRelativePath($file),
$node->getLine(),
));
}

$actualType = $scope->getNativeType($node->getArgs()[1]->value);
Expand Down Expand Up @@ -239,7 +247,7 @@ public static function gatherAssertTypes(string $file): array
'Function %s imported with wrong namespace %s called in %s on line %d.',
$correctFunction,
$functionName,
$relativeFile,
$pathHelper->getRelativePath($file),
$node->getStartLine(),
));
}
Expand All @@ -248,7 +256,7 @@ public static function gatherAssertTypes(string $file): array
self::fail(sprintf(
'ERROR: Wrong %s() call in %s on line %d.',
$functionName,
$relativeFile,
$pathHelper->getRelativePath($file),
$node->getStartLine(),
));
}
Expand Down

0 comments on commit b6a4f4f

Please sign in to comment.