Skip to content

Commit c41f730

Browse files
staabmsebastianbergmann
authored andcommittedDec 16, 2024··
Reduce unnecessary IO
1 parent fd75e48 commit c41f730

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed
 

‎src/Data/RawCodeCoverageData.php

+5-9
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@
1616
use function array_intersect_key;
1717
use function array_map;
1818
use function count;
19-
use function explode;
20-
use function file_get_contents;
19+
use function file;
2120
use function in_array;
22-
use function is_file;
2321
use function preg_replace;
2422
use function range;
2523
use function str_ends_with;
@@ -269,13 +267,11 @@ private function getEmptyLinesForFile(string $filename): array
269267
if (!isset(self::$emptyLineCache[$filename])) {
270268
self::$emptyLineCache[$filename] = [];
271269

272-
if (is_file($filename)) {
273-
$sourceLines = explode("\n", file_get_contents($filename));
270+
$sourceLines = @file($filename) ?: [];
274271

275-
foreach ($sourceLines as $line => $source) {
276-
if (trim($source) === '') {
277-
self::$emptyLineCache[$filename][] = ($line + 1);
278-
}
272+
foreach ($sourceLines as $line => $source) {
273+
if (trim($source) === '') {
274+
self::$emptyLineCache[$filename][] = ($line + 1);
279275
}
280276
}
281277
}

‎src/StaticAnalysis/CachingFileAnalyser.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use function file_get_contents;
1414
use function file_put_contents;
1515
use function implode;
16-
use function is_file;
1716
use function md5;
1817
use function serialize;
1918
use function unserialize;
@@ -169,12 +168,14 @@ private function read(string $filename): array|false
169168
{
170169
$cacheFile = $this->cacheFile($filename);
171170

172-
if (!is_file($cacheFile)) {
171+
$contents = @file_get_contents($cacheFile);
172+
173+
if ($contents === false) {
173174
return false;
174175
}
175176

176177
return unserialize(
177-
file_get_contents($cacheFile),
178+
$contents,
178179
[
179180
'allowed_classes' => [
180181
Class_::class,

4 commit comments

Comments
 (4)

sebastianbergmann commented on Dec 20, 2024

@sebastianbergmann
Owner

@staabm Not entirely sure, but I suspect this change broke something: https://github.com/sebastianbergmann/phpunit/actions/runs/12426606434/job/34695291633

staabm commented on Dec 20, 2024

@staabm
ContributorAuthor

I think you are right that the problem is triggered from this code here.

the root cause seems to be some error handler which turns warnings into exceptions
(and does not respect the @ operator)

sebastianbergmann commented on Dec 20, 2024

@sebastianbergmann
Owner

staabm commented on Dec 20, 2024

@staabm
ContributorAuthor

ahh.. the stacktrace of your first link makes the problem obvious. working on a fix

Please sign in to comment.