Passed
Pull Request — master (#1)
by David
02:06
created

FileSystem::rmdir()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 9
nc 5
nop 1
dl 0
loc 14
rs 8.8571
c 0
b 0
f 0
1
<?php
2
namespace TheCodingMachine\Funky\Utils;
3
4
use TheCodingMachine\Funky\IoException;
5
6
class FileSystem
7
{
8
    public static function mkdir(string $dir, int $mode = 0777): void
9
    {
10
        if (is_dir($dir)) {
11
            return;
12
        }
13
14
        if (true !== @mkdir($dir, $mode, true)) {
15
            $error = error_get_last();
16
            if (!is_dir($dir)) {
17
                // The directory was not created by a concurrent process. Let's throw an exception with a developer friendly error message if we have one
18
                if ($error) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $error of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
19
                    throw IoException::cannotCreateDirectory($dir, $error['message']);
20
                }
21
                throw IoException::cannotCreateDirectory($dir, 'unknown error');
22
            }
23
        }
24
    }
25
26
    public static function rmdir(string $dir): void
27
    {
28
        if (is_dir($dir)) {
29
            $objects = scandir($dir);
30
            foreach ($objects as $object) {
31
                if ($object !== "." && $object !== "..") {
32
                    if (is_dir($dir. '/' .$object)) {
33
                        self::rmdir($dir. '/' .$object);
34
                    } else {
35
                        unlink($dir. '/' .$object);
36
                    }
37
                }
38
            }
39
            rmdir($dir);
40
        }
41
    }
42
}
43