Skip to content

Commit 453c47f

Browse files
authored
Fix PHP Error on mkdir() on existing directory
I cannot do code-coverage because we have a custom error handler that converts PHP Errors into exceptions. It is bad logic to intentionally yield errors. The problem here was that mkdir() was executed before is_dir(). I reversed the direction of these calls (is_dir() before mkdir()) and made them into a function, since the same logic was three times in the class.
1 parent 5218775 commit 453c47f

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

src/Report/Html/Facade.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function process(CodeCoverage $coverage, string $target): void
9494
$id = $node->getId();
9595

9696
if ($node instanceof DirectoryNode) {
97-
if (!@\mkdir($target . $id, 0777, true) && !\is_dir($target . $id)) {
97+
if (!$this->createDirectory($target . $id)) {
9898
throw new \RuntimeException(\sprintf('Directory "%s" was not created', $target . $id));
9999
}
100100

@@ -103,7 +103,7 @@ public function process(CodeCoverage $coverage, string $target): void
103103
} else {
104104
$dir = \dirname($target . $id);
105105

106-
if (!@\mkdir($dir, 0777, true) && !\is_dir($dir)) {
106+
if (!$this->createDirectory($dir)) {
107107
throw new \RuntimeException(\sprintf('Directory "%s" was not created', $dir));
108108
}
109109

@@ -160,8 +160,8 @@ private function getDirectory(string $directory): string
160160
if (\substr($directory, -1, 1) != DIRECTORY_SEPARATOR) {
161161
$directory .= DIRECTORY_SEPARATOR;
162162
}
163-
164-
if (!@\mkdir($directory, 0777, true) && !\is_dir($directory)) {
163+
164+
if (!$this->createDirectory($directory)) {
165165
throw new RuntimeException(
166166
\sprintf(
167167
'Directory "%s" does not exist.',
@@ -172,4 +172,15 @@ private function getDirectory(string $directory): string
172172

173173
return $directory;
174174
}
175+
176+
private function createDirectory(string $directory): bool
177+
{
178+
if (!\is_dir($directory)) {
179+
if (!@\mkdir($directory)) {
180+
return false;
181+
}
182+
}
183+
184+
return true;
185+
}
175186
}

0 commit comments

Comments
 (0)