From b6a4f4fa51edec58d99ccb59b8933c0b0396f27c Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 19 Jun 2024 09:48:47 +0200 Subject: [PATCH] minimize IO --- src/Testing/TypeInferenceTestCase.php | 36 ++++++++++++++++----------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/src/Testing/TypeInferenceTestCase.php b/src/Testing/TypeInferenceTestCase.php index 88fa721d8b6..43a9cdaba8a 100644 --- a/src/Testing/TypeInferenceTestCase.php +++ b/src/Testing/TypeInferenceTestCase.php @@ -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; } @@ -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); @@ -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(), )); } @@ -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(), )); }