Passed
Pull Request — main (#22)
by Andrey
28:59 queued 16:23
created

Directory::delete()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 6
nop 1
dl 0
loc 19
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
namespace Helldar\Support\Helpers\Filesystem;
4
5
use DirectoryIterator;
6
use FilesystemIterator;
7
use Helldar\Support\Exceptions\DirectoryNotFoundException;
8
use Helldar\Support\Facades\Helpers\Filesystem\File;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Helldar\Support\Helpers\Filesystem\File. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
9
use Helldar\Support\Facades\Helpers\Instance;
10
use SplFileInfo;
11
12
final class Directory
13
{
14
    /**
15
     * Get a list of files and folders in a directory.
16
     *
17
     * @param  string  $path
18
     *
19
     * @throws \Helldar\Support\Exceptions\DirectoryNotFoundException
20
     *
21
     * @return DirectoryIterator
22
     */
23
    public function all(string $path): DirectoryIterator
24
    {
25
        if ($this->doesntExist($path)) {
26
            throw new DirectoryNotFoundException($path);
27
        }
28
29
        return new DirectoryIterator($path);
30
    }
31
32
    /**
33
     * Get a list of directory names along a path.
34
     *
35
     * @param  string  $path
36
     *
37
     * @throws \Helldar\Support\Exceptions\DirectoryNotFoundException
38
     *
39
     * @return array
40
     */
41
    public function names(string $path): array
42
    {
43
        $items = [];
44
45
        foreach ($this->all($path) as $directory) {
46
            if ($directory->isDir() && ! $directory->isDot()) {
47
                $items[] = $directory->getFilename();
48
            }
49
        }
50
51
        sort($items);
52
53
        return array_values($items);
54
    }
55
56
    /**
57
     * Create a directory at the specified path.
58
     *
59
     * @param  string  $path
60
     * @param  int  $mode
61
     *
62
     * @return bool
63
     */
64
    public function make(string $path, int $mode = 755): bool
65
    {
66
        if ($this->doesntExist($path)) {
67
            return mkdir($path, $mode, true);
68
        }
69
70
        return true;
71
    }
72
73
    /**
74
     * Delete the directory with all contents in the specified path.
75
     *
76
     * @param  string  $path
77
     *
78
     * @throws \Helldar\Support\Exceptions\DirectoryNotFoundException
79
     *
80
     * @return bool
81
     */
82
    public function delete(string $path): bool
83
    {
84
        if (! $this->isDirectory($path)) {
85
            throw new DirectoryNotFoundException($path);
86
        }
87
88
        $items = new FilesystemIterator($path);
89
90
        $success = true;
91
92
        foreach ($items as $item) {
93
            $item->isDir() && ! $item->isLink()
94
                ? $this->delete($item->getPathname())
95
                : File::delete($item->getPathname());
96
        }
97
98
        @rmdir($path);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for rmdir(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

98
        /** @scrutinizer ignore-unhandled */ @rmdir($path);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
99
100
        return $success;
101
    }
102
103
    /**
104
     * Check if the directory exists.
105
     *
106
     * @param  string  $path
107
     *
108
     * @return bool
109
     */
110
    public function exists(string $path): bool
111
    {
112
        return file_exists($path) && is_dir($path);
113
    }
114
115
    /**
116
     * Check if the directory doesn't exists.
117
     *
118
     * @param  string  $path
119
     *
120
     * @return bool
121
     */
122
    public function doesntExist(string $path): bool
123
    {
124
        return ! $this->exists($path);
125
    }
126
127
    /**
128
     * Check if object or path is a directory.
129
     *
130
     * @param  DirectoryIterator|\SplFileInfo|string  $value
131
     *
132
     * @return bool
133
     */
134
    public function isDirectory($value): bool
135
    {
136
        if (Instance::of($value, [SplFileInfo::class, DirectoryIterator::class])) {
137
            return $value->isDir();
138
        }
139
140
        return is_dir($value);
141
    }
142
}
143