Skip to content

Commit

Permalink
Add File::throwIfFalse to reduce duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
chadicus committed Feb 22, 2018
1 parent 1446fa3 commit 65fdeaa
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/File.php
Expand Up @@ -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);
}
}

Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 65fdeaa

Please sign in to comment.