Passed
Pull Request — main (#22)
by Andrey
18:33 queued 03:32
created

Directory   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
dl 0
loc 71
rs 10
c 1
b 0
f 0
wmc 16

6 Methods

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

68
        /** @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...
69
70
        return $success;
71
    }
72
73
    public function exists(string $path): bool
74
    {
75
        return file_exists($path) && is_dir($path);
76
    }
77
78
    public function doesntExist(string $path): bool
79
    {
80
        return ! $this->exists($path);
81
    }
82
}
83