From f452724974ba1584bd405d8c135652004f09b5b4 Mon Sep 17 00:00:00 2001 From: Markus Staab <maggus.staab@googlemail.com> Date: Mon, 16 Dec 2024 11:46:15 +0100 Subject: [PATCH] Reduce unnecessary IO --- src/Data/RawCodeCoverageData.php | 14 +++++--------- src/StaticAnalysis/CachingFileAnalyser.php | 7 ++++--- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/Data/RawCodeCoverageData.php b/src/Data/RawCodeCoverageData.php index 058ec199..a16aef8e 100644 --- a/src/Data/RawCodeCoverageData.php +++ b/src/Data/RawCodeCoverageData.php @@ -16,10 +16,8 @@ use function array_intersect_key; use function array_map; use function count; -use function explode; -use function file_get_contents; +use function file; use function in_array; -use function is_file; use function preg_replace; use function range; use function str_ends_with; @@ -269,13 +267,11 @@ private function getEmptyLinesForFile(string $filename): array if (!isset(self::$emptyLineCache[$filename])) { self::$emptyLineCache[$filename] = []; - if (is_file($filename)) { - $sourceLines = explode("\n", file_get_contents($filename)); + $sourceLines = @file($filename) ?: []; - foreach ($sourceLines as $line => $source) { - if (trim($source) === '') { - self::$emptyLineCache[$filename][] = ($line + 1); - } + foreach ($sourceLines as $line => $source) { + if (trim($source) === '') { + self::$emptyLineCache[$filename][] = ($line + 1); } } } diff --git a/src/StaticAnalysis/CachingFileAnalyser.php b/src/StaticAnalysis/CachingFileAnalyser.php index f9acc2d6..d455310d 100644 --- a/src/StaticAnalysis/CachingFileAnalyser.php +++ b/src/StaticAnalysis/CachingFileAnalyser.php @@ -13,7 +13,6 @@ use function file_get_contents; use function file_put_contents; use function implode; -use function is_file; use function md5; use function serialize; use function unserialize; @@ -169,12 +168,14 @@ private function read(string $filename): array|false { $cacheFile = $this->cacheFile($filename); - if (!is_file($cacheFile)) { + $contents = @file_get_contents($cacheFile); + + if ($contents === false) { return false; } return unserialize( - file_get_contents($cacheFile), + $contents, [ 'allowed_classes' => [ Class_::class,