Passed
Pull Request — main (#22)
by Andrey
11:54
created

File::store()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Helldar\Support\Helpers\Filesystem;
4
5
use DirectoryIterator;
6
use ErrorException;
7
use Helldar\Support\Exceptions\DirectoryNotFoundException;
8
use Helldar\Support\Facades\Helpers\Arr;
9
use Helldar\Support\Facades\Helpers\Filesystem\Directory;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Helldar\Support\Helpers\Filesystem\Directory. 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...
10
11
class File
12
{
13
    /**
14
     * @param  string  $path
15
     *
16
     * @throws \Helldar\Support\Exceptions\DirectoryNotFoundException
17
     *
18
     * @return array|\SplFileInfo[]
19
     */
20
    public function all(string $path): array
21
    {
22
        if (Directory::doesntExist($path)) {
23
            throw new DirectoryNotFoundException($path);
24
        }
25
26
        $dirs  = new DirectoryIterator($path);
27
        $items = [];
28
29
        foreach ($dirs as $item) {
30
            if ($item->isFile()) {
31
                $items[] = $item->current();
32
            }
33
        }
34
35
        return $items;
36
    }
37
38
    public function store(string $path, string $content): void
39
    {
40
        Directory::make(pathinfo($path, PATHINFO_DIRNAME));
41
42
        file_put_contents($path, $content);
43
    }
44
45
    public function exists(string $path): bool
46
    {
47
        return file_exists($path) && is_file($path);
48
    }
49
50
    /**
51
     * @param  string|string[]  $paths
52
     *
53
     * @return bool
54
     */
55
    public function delete($paths): bool
56
    {
57
        $paths = Arr::wrap($paths);
58
59
        $success = true;
60
61
        foreach ($paths as $path) {
62
            try {
63
                if (! @unlink($path)) {
64
                    $success = false;
65
                }
66
            }
67
            catch (ErrorException $e) {
68
                $success = false;
69
            }
70
        }
71
72
        return $success;
73
    }
74
}
75