From 3299d8c45b24ff116bfdaacba77b3a9d93b519d4 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Tue, 12 Mar 2024 09:13:57 +0100 Subject: [PATCH] Prevent file IO when not strictly necessary --- .psalm/baseline.xml | 2 -- src/Framework/TestSuite.php | 2 +- src/Runner/Baseline/Issue.php | 7 ++++--- src/Runner/PhptTestCase.php | 12 +++++------- src/Runner/ResultCache/DefaultResultCache.php | 7 ++++--- src/TextUI/Application.php | 9 ++------- src/TextUI/Configuration/Cli/Builder.php | 2 +- src/TextUI/Configuration/TestSuiteBuilder.php | 14 +++++++------- src/Util/Filter.php | 6 +++--- src/Util/PHP/AbstractPhpProcess.php | 8 ++++---- 10 files changed, 31 insertions(+), 38 deletions(-) diff --git a/.psalm/baseline.xml b/.psalm/baseline.xml index 2da4139f093..a1c98407ac5 100644 --- a/.psalm/baseline.xml +++ b/.psalm/baseline.xml @@ -927,8 +927,6 @@ - - diff --git a/src/Framework/TestSuite.php b/src/Framework/TestSuite.php index 9ae97bef017..1b2113cb7de 100644 --- a/src/Framework/TestSuite.php +++ b/src/Framework/TestSuite.php @@ -228,7 +228,7 @@ public function addTestSuite(ReflectionClass $testClass): void */ public function addTestFile(string $filename): void { - if (is_file($filename) && str_ends_with($filename, '.phpt')) { + if (str_ends_with($filename, '.phpt') && is_file($filename)) { try { $this->addTest(new PhptTestCase($filename)); } catch (RunnerException $e) { diff --git a/src/Runner/Baseline/Issue.php b/src/Runner/Baseline/Issue.php index d90b067b982..dfd7d14dc84 100644 --- a/src/Runner/Baseline/Issue.php +++ b/src/Runner/Baseline/Issue.php @@ -123,12 +123,13 @@ public function equals(self $other): bool */ private static function calculateHash(string $file, int $line): string { - if (!is_file($file)) { + $lines = @file($file, FILE_IGNORE_NEW_LINES); + + if ($lines === false && !is_file($file)) { throw new FileDoesNotExistException($file); } - $lines = file($file, FILE_IGNORE_NEW_LINES); - $key = $line - 1; + $key = $line - 1; if (!isset($lines[$key])) { throw new FileDoesNotHaveLineException($file, $line); diff --git a/src/Runner/PhptTestCase.php b/src/Runner/PhptTestCase.php index bc6cdd09485..a5840f052b0 100644 --- a/src/Runner/PhptTestCase.php +++ b/src/Runner/PhptTestCase.php @@ -638,15 +638,13 @@ private function cleanupForCoverage(): RawCodeCoverageData $coverage = RawCodeCoverageData::fromXdebugWithoutPathCoverage([]); $files = $this->getCoverageFiles(); - if (is_file($files['coverage'])) { - $buffer = @file_get_contents($files['coverage']); + $buffer = @file_get_contents($files['coverage']); - if ($buffer !== false) { - $coverage = @unserialize($buffer); + if ($buffer !== false) { + $coverage = @unserialize($buffer); - if ($coverage === false) { - $coverage = RawCodeCoverageData::fromXdebugWithoutPathCoverage([]); - } + if ($coverage === false) { + $coverage = RawCodeCoverageData::fromXdebugWithoutPathCoverage([]); } } diff --git a/src/Runner/ResultCache/DefaultResultCache.php b/src/Runner/ResultCache/DefaultResultCache.php index bc3e8c4492c..090935f98f9 100644 --- a/src/Runner/ResultCache/DefaultResultCache.php +++ b/src/Runner/ResultCache/DefaultResultCache.php @@ -17,7 +17,6 @@ use function file_put_contents; use function is_array; use function is_dir; -use function is_file; use function json_decode; use function json_encode; use PHPUnit\Framework\TestStatus\TestStatus; @@ -86,12 +85,14 @@ public function time(string $id): float public function load(): void { - if (!is_file($this->cacheFilename)) { + $contents = @file_get_contents($this->cacheFilename); + + if ($contents === false) { return; } $data = json_decode( - file_get_contents($this->cacheFilename), + $contents, true, ); diff --git a/src/TextUI/Application.php b/src/TextUI/Application.php index c83534825b4..6fbca1f58c6 100644 --- a/src/TextUI/Application.php +++ b/src/TextUI/Application.php @@ -10,7 +10,6 @@ namespace PHPUnit\TextUI; use const PHP_EOL; -use function is_file; use function is_readable; use function printf; use function realpath; @@ -517,9 +516,7 @@ private function writeRandomSeedInformation(Printer $printer, Configuration $con private function registerLogfileWriters(Configuration $configuration): void { if ($configuration->hasLogEventsText()) { - if (is_file($configuration->logEventsText())) { - unlink($configuration->logEventsText()); - } + @unlink($configuration->logEventsText()); EventFacade::instance()->registerTracer( new EventLogger( @@ -530,9 +527,7 @@ private function registerLogfileWriters(Configuration $configuration): void } if ($configuration->hasLogEventsVerboseText()) { - if (is_file($configuration->logEventsVerboseText())) { - unlink($configuration->logEventsVerboseText()); - } + @unlink($configuration->logEventsVerboseText()); EventFacade::instance()->registerTracer( new EventLogger( diff --git a/src/TextUI/Configuration/Cli/Builder.php b/src/TextUI/Configuration/Cli/Builder.php index db82c9f570d..ed0b6f4239e 100644 --- a/src/TextUI/Configuration/Cli/Builder.php +++ b/src/TextUI/Configuration/Cli/Builder.php @@ -399,7 +399,7 @@ public function fromParameters(array $parameters): Configuration case '--use-baseline': $useBaseline = $option[1]; - if (!is_file($useBaseline) && basename($useBaseline) === $useBaseline) { + if (basename($useBaseline) === $useBaseline && !is_file($useBaseline)) { $useBaseline = getcwd() . DIRECTORY_SEPARATOR . $useBaseline; } diff --git a/src/TextUI/Configuration/TestSuiteBuilder.php b/src/TextUI/Configuration/TestSuiteBuilder.php index d3fff95db42..1b5a09c1ffb 100644 --- a/src/TextUI/Configuration/TestSuiteBuilder.php +++ b/src/TextUI/Configuration/TestSuiteBuilder.php @@ -91,6 +91,13 @@ public function build(Configuration $configuration): TestSuite */ private function testSuiteFromPath(string $path, array $suffixes, ?TestSuite $suite = null): TestSuite { + if (str_ends_with($path, '.phpt') && is_file($path)) { + $suite = $suite ?: TestSuite::empty($path); + $suite->addTestFile($path); + + return $suite; + } + if (is_dir($path)) { $files = (new FileIteratorFacade)->getFilesAsArray($path, $suffixes); @@ -100,13 +107,6 @@ private function testSuiteFromPath(string $path, array $suffixes, ?TestSuite $su return $suite; } - if (is_file($path) && str_ends_with($path, '.phpt')) { - $suite = $suite ?: TestSuite::empty($path); - $suite->addTestFile($path); - - return $suite; - } - try { $testClass = (new TestSuiteLoader)->load($path); } catch (Exception $e) { diff --git a/src/Util/Filter.php b/src/Util/Filter.php index 46bf082bead..2faa3b9154f 100644 --- a/src/Util/Filter.php +++ b/src/Util/Filter.php @@ -89,10 +89,10 @@ private static function shouldPrintFrame(array $frame, false|string $prefix, Exc $script = ''; } - return is_file($file) && + return $fileIsNotPrefixed && + $file !== $script && self::fileIsExcluded($file, $excludeList) && - $fileIsNotPrefixed && - $file !== $script; + is_file($file); } private static function fileIsExcluded(string $file, ExcludeList $excludeList): bool diff --git a/src/Util/PHP/AbstractPhpProcess.php b/src/Util/PHP/AbstractPhpProcess.php index ddc909b0006..6a9b636afe4 100644 --- a/src/Util/PHP/AbstractPhpProcess.php +++ b/src/Util/PHP/AbstractPhpProcess.php @@ -14,7 +14,6 @@ use function array_merge; use function assert; use function escapeshellarg; -use function file_exists; use function file_get_contents; use function ini_get_all; use function restore_error_handler; @@ -139,12 +138,13 @@ public function runTestJob(string $job, Test $test, string $processResultFile): { $_result = $this->runJob($job); - $processResult = ''; + $processResult = @file_get_contents($processResultFile); - if (file_exists($processResultFile)) { - $processResult = file_get_contents($processResultFile); + if ($processResult !== false) { @unlink($processResultFile); + } else { + $processResult = ''; } $this->processChildResult(