Passed
Pull Request — main (#22)
by Andrey
22:20 queued 08:45
created

Directory   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
dl 0
loc 85
rs 10
c 1
b 0
f 0
wmc 18

7 Methods

Rating   Name   Duplication   Size   Complexity  
A isDirectory() 0 7 2
A all() 0 7 2
A exists() 0 3 2
A delete() 0 19 5
A names() 0 13 4
A doesntExist() 0 3 1
A make() 0 7 2
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
     * @param  string  $path
16
     *
17
     * @throws \Helldar\Support\Exceptions\DirectoryNotFoundException
18
     *
19
     * @return DirectoryIterator
20
     */
21
    public function all(string $path): DirectoryIterator
22
    {
23
        if ($this->doesntExist($path)) {
24
            throw new DirectoryNotFoundException($path);
25
        }
26
27
        return new DirectoryIterator($path);
28
    }
29
30
    public function names(string $path): array
31
    {
32
        $items = [];
33
34
        foreach ($this->all($path) as $directory) {
35
            if ($directory->isDir() && ! $directory->isDot()) {
36
                $items[] = $directory->getFilename();
37
            }
38
        }
39
40
        sort($items);
41
42
        return array_values($items);
43
    }
44
45
    public function make(string $path, int $mode = 755): bool
46
    {
47
        if ($this->doesntExist($path)) {
48
            return mkdir($path, $mode, true);
49
        }
50
51
        return true;
52
    }
53
54
    public function delete(string $path): bool
55
    {
56
        if (! $this->isDirectory($path)) {
57
            throw new DirectoryNotFoundException($path);
58
        }
59
60
        $items = new FilesystemIterator($path);
61
62
        $success = true;
63
64
        foreach ($items as $item) {
65
            $item->isDir() && ! $item->isLink()
66
                ? $this->delete($item->getPathname())
67
                : File::delete($item->getPathname());
68
        }
69
70
        @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

70
        /** @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...
71
72
        return $success;
73
    }
74
75
    public function exists(string $path): bool
76
    {
77
        return file_exists($path) && is_dir($path);
78
    }
79
80
    public function doesntExist(string $path): bool
81
    {
82
        return ! $this->exists($path);
83
    }
84
85
    /**
86
     * @param  DirectoryIterator|\SplFileInfo|string  $value
87
     *
88
     * @return bool
89
     */
90
    public function isDirectory($value): bool
91
    {
92
        if (Instance::of($value, [SplFileInfo::class, DirectoryIterator::class])) {
93
            return $value->isDir();
94
        }
95
96
        return is_dir($value);
97
    }
98
}
99