Passed
Pull Request — main (#110)
by Andrey
10:40
created

Directory::names()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 10

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 14
nc 10
nop 3
dl 0
loc 28
ccs 9
cts 9
cp 1
crap 10
rs 7.6666
c 1
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 as FileHelper;
9
use Helldar\Support\Facades\Helpers\Instance;
10
use Helldar\Support\Facades\Helpers\Str;
11
use SplFileInfo;
12
13
final class Directory
14
{
15
    /**
16
     * Get a list of files and folders in a directory.
17
     *
18
     * @param  string  $path
19
     *
20
     * @throws \Helldar\Support\Exceptions\DirectoryNotFoundException
21
     *
22
     * @return DirectoryIterator
23 14
     */
24
    public function all(string $path): DirectoryIterator
25 14
    {
26 4
        if ($this->doesntExist($path)) {
27
            throw new DirectoryNotFoundException($path);
28
        }
29 10
30
        return new DirectoryIterator($path);
31
    }
32
33
    /**
34
     * Get a list of directory names along a path.
35
     *
36
     * @param  string  $path
37
     * @param  callable|null  $callback
38
     * @param  bool  $recursive
39
     *
40
     * @throws \Helldar\Support\Exceptions\DirectoryNotFoundException
41
     *
42 4
     * @return array
43
     */
44 4
    public function names(string $path, callable $callback = null, bool $recursive = false): array
45
    {
46
        $items = [];
47 4
48 4
        /** @var \DirectoryIterator $directory */
49 4
        foreach ($this->all($path) as $directory) {
50
            if ($directory->isDir() && ! $directory->isDot()) {
51 4
                $name = $directory->getFilename();
52 4
53
                if (! is_callable($callback) || $callback($name)) {
54
                    $items[] = $name;
55
                }
56
            }
57 4
58
            if ($recursive && $directory->isDir() && ! $directory->isDot()) {
59 4
                $prefix = (string) Str::of($directory->getRealPath())
60
                    ->after(realpath($path))
61
                    ->trim('\\/');
62
63
                foreach ($this->names($directory->getRealPath(), $callback, $recursive) as $value) {
64
                    $items[] = $prefix . '/' . $value;
65
                }
66
            }
67
        }
68
69
        sort($items);
70 32
71
        return array_values($items);
72 32
    }
73
74
    /**
75
     * Create a directory at the specified path.
76
     *
77
     * @param  string  $path
78
     * @param  int  $mode
79
     *
80
     * @return bool
81
     */
82
    public function make(string $path, int $mode = 0755): bool
83
    {
84 34
        return ! $this->doesntExist($path) || mkdir($path, $mode, true);
85
    }
86 34
87 4
    /**
88
     * Delete the directory with all contents in the specified path.
89
     *
90 32
     * @param  string  $path
91
     *
92 32
     * @throws \Helldar\Support\Exceptions\DirectoryNotFoundException
93
     *
94 32
     * @return bool
95 30
     */
96 30
    public function delete(string $path): bool
97 24
    {
98
        if (! $this->isDirectory($path)) {
99
            throw new DirectoryNotFoundException($path);
100 32
        }
101
102 32
        $items = new FilesystemIterator($path);
103
104
        $success = true;
105
106
        foreach ($items as $item) {
107
            $item->isDir() && ! $item->isLink()
108
                ? $this->delete($item->getPathname())
109
                : FileHelper::delete($item->getPathname());
110
        }
111
112 468
        @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

112
        /** @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...
113
114 468
        return $success;
115
    }
116
117
    /**
118
     * Check if the directory exists.
119
     *
120
     * @param  string  $path
121
     *
122
     * @return bool
123
     */
124 48
    public function exists(string $path): bool
125
    {
126 48
        return file_exists($path) && is_dir($path);
127
    }
128
129
    /**
130
     * Check if the directory doesn't exists.
131
     *
132
     * @param  string  $path
133
     *
134
     * @return bool
135
     */
136 46
    public function doesntExist(string $path): bool
137
    {
138 46
        return ! $this->exists($path);
139 2
    }
140
141
    /**
142 44
     * Check if object or path is a directory.
143
     *
144
     * @param  DirectoryIterator|\SplFileInfo|string  $value
145
     *
146
     * @return bool
147
     */
148
    public function isDirectory($value): bool
149
    {
150
        if (Instance::of($value, [SplFileInfo::class, DirectoryIterator::class])) {
151
            return $value->isDir();
152 8
        }
153
154 8
        return is_dir($value);
155 4
    }
156
157 4
    /**
158
     * Checks the existence of a directory.
159
     *
160
     * @param  DirectoryIterator|\SplFileInfo|string  $path
161
     *
162
     * @throws \Helldar\Support\Exceptions\DirectoryNotFoundException
163
     */
164
    public function validate($path): void
165
    {
166
        if (! $this->isDirectory($path)) {
167
            throw new DirectoryNotFoundException($path);
168 4
        }
169
    }
170 4
171
    /**
172 2
     * Checks the existence of a directory and return full path if exist.
173
     *
174
     * @param  DirectoryIterator|\SplFileInfo|string  $path
175
     *
176
     * @throws \Helldar\Support\Exceptions\DirectoryNotFoundException
177
     *
178
     * @return string
179
     */
180
    public function validated($path): string
181
    {
182
        $this->validate($path);
183
184
        return realpath($path);
185
    }
186
}
187