From 65fdeaa1cd8a4ed50359d706a1637fa54e1adbb5 Mon Sep 17 00:00:00 2001 From: chadicus Date: Thu, 22 Feb 2018 11:28:49 -0500 Subject: [PATCH] Add File::throwIfFalse to reduce duplicate code --- src/File.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/File.php b/src/File.php index 27545ac..ca5c9f9 100644 --- a/src/File.php +++ b/src/File.php @@ -37,16 +37,11 @@ public static function deleteDirectoryContents(string $directoryPath) if (is_dir($fullPath)) { self::deleteDirectoryContents($fullPath);//RECURSIVE CALL - if (!rmdir($fullPath)) { - throw new \Exception("cannot delete '{$fullPath}'", 1); - } - + self::throwIfFalse(rmdir($fullPath), "cannot delete '{$fullPath}'", 1); continue; } - if (!unlink($fullPath)) { - throw new \Exception("cannot delete '{$fullPath}'", 2); - } + self::throwIfFalse(unlink($fullPath), "cannot delete '{$fullPath}'", 2); } } @@ -71,9 +66,7 @@ public static function delete(string $path) } try { - if (unlink($path) === false) { - throw new \Exception("unlink returned false for '{$path}'"); - } + self::throwIfFalse(unlink($path), "unlink returned false for '{$path}'"); } catch (\Exception $e) { if (file_exists($path)) { throw $e; @@ -116,4 +109,11 @@ public static function deletePathIfEmpty(string $deletePath, string $stopAtPath //RECURSION!!! self::deletePathIfEmpty(dirname($deletePath), $stopAtPath); } + + private static function throwIfFalse($condition, string $message, int $code = 0) + { + if ($condition === false) { + throw new \Exception($message, $code); + } + } }