From d197ab36ed98cfa2c3676053b80ebbb8a2691e20 Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Tue, 15 Jan 2019 10:28:36 +0100 Subject: [PATCH] Eliminate code duplication --- src/Util/Filesystem.php | 5 +++++ src/Util/Printer.php | 7 +------ src/Util/TestResultCache.php | 8 ++------ 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/Util/Filesystem.php b/src/Util/Filesystem.php index a2ea804f7ec..068a3b0cce3 100644 --- a/src/Util/Filesystem.php +++ b/src/Util/Filesystem.php @@ -27,4 +27,9 @@ public static function classNameToFilename(string $className): string $className ) . '.php'; } + + public static function createDirectory(string $directory): bool + { + return !(!\is_dir($directory) && !@\mkdir($directory, 0777, true) && !\is_dir($directory)); + } } diff --git a/src/Util/Printer.php b/src/Util/Printer.php index 505bce6157d..5e5d67520a7 100644 --- a/src/Util/Printer.php +++ b/src/Util/Printer.php @@ -53,7 +53,7 @@ public function __construct($out = null) $this->out = \fsockopen($out[0], $out[1]); } else { - if (\strpos($out, 'php://') === false && !$this->createDirectory(\dirname($out))) { + if (\strpos($out, 'php://') === false && !Filesystem::createDirectory(\dirname($out))) { throw new Exception(\sprintf('Directory "%s" was not created', \dirname($out))); } @@ -132,9 +132,4 @@ public function setAutoFlush(bool $autoFlush): void { $this->autoFlush = $autoFlush; } - - private function createDirectory(string $directory): bool - { - return !(!\is_dir($directory) && !@\mkdir($directory, 0777, true) && !\is_dir($directory)); - } } diff --git a/src/Util/TestResultCache.php b/src/Util/TestResultCache.php index 8cb290091c8..3fdfc7d23c8 100644 --- a/src/Util/TestResultCache.php +++ b/src/Util/TestResultCache.php @@ -10,6 +10,7 @@ namespace PHPUnit\Runner; use PHPUnit\Framework\Test; +use PHPUnit\Util\Filesystem; class TestResultCache implements \Serializable, TestResultCacheInterface { @@ -79,7 +80,7 @@ public function saveToFile(): void return; } - if (!$this->createDirectory(\dirname($this->cacheFilename))) { + if (!Filesystem::createDirectory(\dirname($this->cacheFilename))) { throw new Exception( \sprintf( 'Cannot create directory "%s" for result cache file', @@ -187,9 +188,4 @@ public function unserialize($serialized): void } } } - - private function createDirectory(string $directory): bool - { - return !(!\is_dir($directory) && !@\mkdir($directory, 0777, true) && !\is_dir($directory)); - } }