Skip to content

Commit 3299d8c

Browse files
staabmsebastianbergmann
authored andcommitted
Prevent file IO when not strictly necessary
1 parent 81bcbcd commit 3299d8c

File tree

10 files changed

+31
-38
lines changed

10 files changed

+31
-38
lines changed

.psalm/baseline.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -927,8 +927,6 @@
927927
<code><![CDATA[listTestsXml]]></code>
928928
<code><![CDATA[logEventsText]]></code>
929929
<code><![CDATA[logEventsText]]></code>
930-
<code><![CDATA[logEventsText]]></code>
931-
<code><![CDATA[logEventsVerboseText]]></code>
932930
<code><![CDATA[logEventsVerboseText]]></code>
933931
<code><![CDATA[logEventsVerboseText]]></code>
934932
<code><![CDATA[logfileJunit]]></code>

src/Framework/TestSuite.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ public function addTestSuite(ReflectionClass $testClass): void
228228
*/
229229
public function addTestFile(string $filename): void
230230
{
231-
if (is_file($filename) && str_ends_with($filename, '.phpt')) {
231+
if (str_ends_with($filename, '.phpt') && is_file($filename)) {
232232
try {
233233
$this->addTest(new PhptTestCase($filename));
234234
} catch (RunnerException $e) {

src/Runner/Baseline/Issue.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,13 @@ public function equals(self $other): bool
123123
*/
124124
private static function calculateHash(string $file, int $line): string
125125
{
126-
if (!is_file($file)) {
126+
$lines = @file($file, FILE_IGNORE_NEW_LINES);
127+
128+
if ($lines === false && !is_file($file)) {
127129
throw new FileDoesNotExistException($file);
128130
}
129131

130-
$lines = file($file, FILE_IGNORE_NEW_LINES);
131-
$key = $line - 1;
132+
$key = $line - 1;
132133

133134
if (!isset($lines[$key])) {
134135
throw new FileDoesNotHaveLineException($file, $line);

src/Runner/PhptTestCase.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -638,15 +638,13 @@ private function cleanupForCoverage(): RawCodeCoverageData
638638
$coverage = RawCodeCoverageData::fromXdebugWithoutPathCoverage([]);
639639
$files = $this->getCoverageFiles();
640640

641-
if (is_file($files['coverage'])) {
642-
$buffer = @file_get_contents($files['coverage']);
641+
$buffer = @file_get_contents($files['coverage']);
643642

644-
if ($buffer !== false) {
645-
$coverage = @unserialize($buffer);
643+
if ($buffer !== false) {
644+
$coverage = @unserialize($buffer);
646645

647-
if ($coverage === false) {
648-
$coverage = RawCodeCoverageData::fromXdebugWithoutPathCoverage([]);
649-
}
646+
if ($coverage === false) {
647+
$coverage = RawCodeCoverageData::fromXdebugWithoutPathCoverage([]);
650648
}
651649
}
652650

src/Runner/ResultCache/DefaultResultCache.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use function file_put_contents;
1818
use function is_array;
1919
use function is_dir;
20-
use function is_file;
2120
use function json_decode;
2221
use function json_encode;
2322
use PHPUnit\Framework\TestStatus\TestStatus;
@@ -86,12 +85,14 @@ public function time(string $id): float
8685

8786
public function load(): void
8887
{
89-
if (!is_file($this->cacheFilename)) {
88+
$contents = @file_get_contents($this->cacheFilename);
89+
90+
if ($contents === false) {
9091
return;
9192
}
9293

9394
$data = json_decode(
94-
file_get_contents($this->cacheFilename),
95+
$contents,
9596
true,
9697
);
9798

src/TextUI/Application.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
namespace PHPUnit\TextUI;
1111

1212
use const PHP_EOL;
13-
use function is_file;
1413
use function is_readable;
1514
use function printf;
1615
use function realpath;
@@ -517,9 +516,7 @@ private function writeRandomSeedInformation(Printer $printer, Configuration $con
517516
private function registerLogfileWriters(Configuration $configuration): void
518517
{
519518
if ($configuration->hasLogEventsText()) {
520-
if (is_file($configuration->logEventsText())) {
521-
unlink($configuration->logEventsText());
522-
}
519+
@unlink($configuration->logEventsText());
523520

524521
EventFacade::instance()->registerTracer(
525522
new EventLogger(
@@ -530,9 +527,7 @@ private function registerLogfileWriters(Configuration $configuration): void
530527
}
531528

532529
if ($configuration->hasLogEventsVerboseText()) {
533-
if (is_file($configuration->logEventsVerboseText())) {
534-
unlink($configuration->logEventsVerboseText());
535-
}
530+
@unlink($configuration->logEventsVerboseText());
536531

537532
EventFacade::instance()->registerTracer(
538533
new EventLogger(

src/TextUI/Configuration/Cli/Builder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ public function fromParameters(array $parameters): Configuration
399399
case '--use-baseline':
400400
$useBaseline = $option[1];
401401

402-
if (!is_file($useBaseline) && basename($useBaseline) === $useBaseline) {
402+
if (basename($useBaseline) === $useBaseline && !is_file($useBaseline)) {
403403
$useBaseline = getcwd() . DIRECTORY_SEPARATOR . $useBaseline;
404404
}
405405

src/TextUI/Configuration/TestSuiteBuilder.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ public function build(Configuration $configuration): TestSuite
9191
*/
9292
private function testSuiteFromPath(string $path, array $suffixes, ?TestSuite $suite = null): TestSuite
9393
{
94+
if (str_ends_with($path, '.phpt') && is_file($path)) {
95+
$suite = $suite ?: TestSuite::empty($path);
96+
$suite->addTestFile($path);
97+
98+
return $suite;
99+
}
100+
94101
if (is_dir($path)) {
95102
$files = (new FileIteratorFacade)->getFilesAsArray($path, $suffixes);
96103

@@ -100,13 +107,6 @@ private function testSuiteFromPath(string $path, array $suffixes, ?TestSuite $su
100107
return $suite;
101108
}
102109

103-
if (is_file($path) && str_ends_with($path, '.phpt')) {
104-
$suite = $suite ?: TestSuite::empty($path);
105-
$suite->addTestFile($path);
106-
107-
return $suite;
108-
}
109-
110110
try {
111111
$testClass = (new TestSuiteLoader)->load($path);
112112
} catch (Exception $e) {

src/Util/Filter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ private static function shouldPrintFrame(array $frame, false|string $prefix, Exc
8989
$script = '';
9090
}
9191

92-
return is_file($file) &&
92+
return $fileIsNotPrefixed &&
93+
$file !== $script &&
9394
self::fileIsExcluded($file, $excludeList) &&
94-
$fileIsNotPrefixed &&
95-
$file !== $script;
95+
is_file($file);
9696
}
9797

9898
private static function fileIsExcluded(string $file, ExcludeList $excludeList): bool

src/Util/PHP/AbstractPhpProcess.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use function array_merge;
1515
use function assert;
1616
use function escapeshellarg;
17-
use function file_exists;
1817
use function file_get_contents;
1918
use function ini_get_all;
2019
use function restore_error_handler;
@@ -139,12 +138,13 @@ public function runTestJob(string $job, Test $test, string $processResultFile):
139138
{
140139
$_result = $this->runJob($job);
141140

142-
$processResult = '';
141+
$processResult = @file_get_contents($processResultFile);
143142

144-
if (file_exists($processResultFile)) {
145-
$processResult = file_get_contents($processResultFile);
143+
if ($processResult !== false) {
146144

147145
@unlink($processResultFile);
146+
} else {
147+
$processResult = '';
148148
}
149149

150150
$this->processChildResult(

0 commit comments

Comments
 (0)