Passed
Pull Request — master (#1)
by David
01:54
created

FileSystem::mkdir()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 8
nc 3
nop 2
dl 0
loc 15
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.
18
                // Let's throw an exception with a developer friendly error message if we have one
19
                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...
20
                    throw IoException::cannotCreateDirectory($dir, $error['message']);
21
                }
22
                throw IoException::cannotCreateDirectory($dir, 'unknown error');
23
            }
24
        }
25
    }
26
27
    public static function rmdir(string $dir): void
28
    {
29
        if (is_dir($dir)) {
30
            $objects = scandir($dir);
31
            foreach ($objects as $object) {
32
                if ($object !== "." && $object !== "..") {
33
                    if (is_dir($dir. '/' .$object)) {
34
                        self::rmdir($dir. '/' .$object);
35
                    } else {
36
                        unlink($dir. '/' .$object);
37
                    }
38
                }
39
            }
40
            rmdir($dir);
41
        }
42
    }
43
}
44