Skip to content

Commit 77d0aee

Browse files
committedMay 23, 2020
Refactor
1 parent d710c1a commit 77d0aee

File tree

7 files changed

+36
-29
lines changed

7 files changed

+36
-29
lines changed
 

‎src/Directory.php

+14-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,20 @@
1111

1212
final class Directory
1313
{
14-
public static function create(string $directory): bool
14+
/**
15+
* @throws DirectoryCouldNotBeCreatedException
16+
*/
17+
public static function create(string $directory): void
1518
{
16-
return !(!\is_dir($directory) && !@\mkdir($directory, 0777, true) && !\is_dir($directory));
19+
$success = !(!\is_dir($directory) && !@\mkdir($directory, 0777, true) && !\is_dir($directory));
20+
21+
if (!$success) {
22+
throw new DirectoryCouldNotBeCreatedException(
23+
\sprintf(
24+
'Directory "%s" could not be created',
25+
$directory
26+
)
27+
);
28+
}
1729
}
1830
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of phpunit/php-code-coverage.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
namespace SebastianBergmann\CodeCoverage;
11+
12+
final class DirectoryCouldNotBeCreatedException extends RuntimeException
13+
{
14+
}

‎src/Report/Clover.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -237,9 +237,7 @@ public function process(CodeCoverage $coverage, ?string $target = null, ?string
237237
$buffer = $xmlDocument->saveXML();
238238

239239
if ($target !== null) {
240-
if (!Directory::create(\dirname($target))) {
241-
throw new \RuntimeException(\sprintf('Directory "%s" was not created', \dirname($target)));
242-
}
240+
Directory::create(\dirname($target));
243241

244242
if (@\file_put_contents($target, $buffer) === false) {
245243
throw new RuntimeException(

‎src/Report/Crap4j.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,7 @@ public function process(CodeCoverage $coverage, ?string $target = null, ?string
117117
$buffer = $document->saveXML();
118118

119119
if ($target !== null) {
120-
if (!Directory::create(\dirname($target))) {
121-
throw new \RuntimeException(\sprintf('Directory "%s" was not created', \dirname($target)));
122-
}
120+
Directory::create(\dirname($target));
123121

124122
if (@\file_put_contents($target, $buffer) === false) {
125123
throw new RuntimeException(

‎src/Report/Html/Facade.php

+3-14
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,14 @@ public function process(CodeCoverage $coverage, string $target): void
9090
$id = $node->getId();
9191

9292
if ($node instanceof DirectoryNode) {
93-
if (!DirectoryUtil::create($target . $id)) {
94-
throw new \RuntimeException(\sprintf('Directory "%s" was not created', $target . $id));
95-
}
93+
DirectoryUtil::create($target . $id);
9694

9795
$directory->render($node, $target . $id . '/index.html');
9896
$dashboard->render($node, $target . $id . '/dashboard.html');
9997
} else {
10098
$dir = \dirname($target . $id);
10199

102-
if (!DirectoryUtil::create($dir)) {
103-
throw new \RuntimeException(\sprintf('Directory "%s" was not created', $dir));
104-
}
100+
DirectoryUtil::create($dir);
105101

106102
$file->render($node, $target . $id . '.html');
107103
}
@@ -145,14 +141,7 @@ private function getDirectory(string $directory): string
145141
$directory .= \DIRECTORY_SEPARATOR;
146142
}
147143

148-
if (!DirectoryUtil::create($directory)) {
149-
throw new RuntimeException(
150-
\sprintf(
151-
'Directory "%s" does not exist.',
152-
$directory
153-
)
154-
);
155-
}
144+
DirectoryUtil::create($directory);
156145

157146
return $directory;
158147
}

‎src/Report/PHP.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ public function process(CodeCoverage $coverage, ?string $target = null): string
4141
);
4242

4343
if ($target !== null) {
44-
if (!Directory::create(\dirname($target))) {
45-
throw new \RuntimeException(\sprintf('Directory "%s" was not created', \dirname($target)));
46-
}
44+
Directory::create(\dirname($target));
4745

4846
if (@\file_put_contents($target, $buffer) === false) {
4947
throw new RuntimeException(

‎src/Report/Xml/Facade.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,9 @@ private function initTargetDirectory(string $directory): void
9090
"'$directory' exists but is not writable."
9191
);
9292
}
93-
} elseif (!DirectoryUtil::create($directory)) {
94-
throw new RuntimeException(
95-
"'$directory' could not be created."
96-
);
9793
}
94+
95+
DirectoryUtil::create($directory);
9896
}
9997

10098
private function processDirectory(DirectoryNode $directory, Node $context): void

0 commit comments

Comments
 (0)
Please sign in to comment.