Passed
Push — master ( c97482...68f9e3 )
by Andrey
07:00 queued 04:30
created

File   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 17
eloc 30
c 0
b 0
f 0
dl 0
loc 88
ccs 40
cts 40
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 3 1
A files() 0 3 1
A isJson() 0 5 1
A save() 0 23 3
A load() 0 11 4
A exists() 0 3 1
A directoryExist() 0 4 3
A fileExist() 0 4 3
1
<?php
2
3
namespace Helldar\LaravelLangPublisher\Support;
4
5
use DirectoryIterator;
6
use Helldar\LaravelLangPublisher\Exceptions\SourceLocaleDirectoryDoesntExist;
7
use Helldar\LaravelLangPublisher\Exceptions\SourceLocaleFileDoesntExist;
8
use Helldar\LaravelLangPublisher\Facades\Config;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Helldar\LaravelLangPublisher\Support\Config. 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\PrettyArray\Services\File as PrettyFile;
10
use Helldar\PrettyArray\Services\Formatter;
11
use Helldar\Support\Facades\Str;
12
13
final class File
14
{
15 12
    public function files(string $path): DirectoryIterator
16
    {
17 12
        return new DirectoryIterator($path);
18
    }
19
20
    /**
21
     * @param  string  $path
22
     * @param  bool  $return_empty
23
     *
24
     * @throws \Helldar\PrettyArray\Exceptions\FileDoesntExistsException
25
     *
26
     * @return array
27
     */
28 30
    public function load(string $path, bool $return_empty = false): array
29
    {
30 30
        if ($return_empty && ! $this->exists($path)) {
31 12
            return [];
32
        }
33
34 30
        $pretty = PrettyFile::make();
35
36 30
        return $this->isJson($path)
37 18
            ? json_decode($pretty->loadRaw($path), true)
38 30
            : $pretty->load($path);
39
    }
40
41
    /**
42
     * @param  string  $path
43
     * @param  array  $data
44
     *
45
     * @throws \Helldar\PrettyArray\Exceptions\UnknownCaseTypeException
46
     */
47 30
    public function save(string $path, array $data): void
48
    {
49 30
        ksort($data);
50
51 30
        if ($this->isJson($path)) {
52 18
            PrettyFile::make(
53 18
                json_encode($data, JSON_PRETTY_PRINT)
54 18
            )->storeRaw($path);
55
56 18
            return;
57
        }
58
59 12
        $service = Formatter::make();
60 12
        $service->setKeyAsString();
61 12
        $service->setCase(Config::getCase());
62
63 12
        if (Config::isAlignment()) {
64 12
            $service->setEqualsAlign();
65
        }
66
67 12
        PrettyFile::make(
68 12
            $service->raw($data)
69 12
        )->store($path);
70 12
    }
71
72 36
    public function directoryExist(string $path, string $locale): void
73
    {
74 36
        if (! $path || ! $this->exists($path)) {
75 12
            throw new SourceLocaleDirectoryDoesntExist($locale);
76
        }
77 24
    }
78
79 42
    public function fileExist(string $path, string $locale): void
80
    {
81 42
        if (! $path || ! $this->exists($path)) {
82 12
            throw new SourceLocaleFileDoesntExist($locale);
83
        }
84 30
    }
85
86 54
    public function exists(string $path): bool
87
    {
88 54
        return file_exists($path);
89
    }
90
91 30
    public function name(string $path): string
92
    {
93 30
        return pathinfo($path, PATHINFO_FILENAME);
94
    }
95
96 30
    public function isJson(string $path): bool
97
    {
98 30
        $extension = pathinfo($path, PATHINFO_EXTENSION);
99
100 30
        return Str::lower($extension) === 'json';
101
    }
102
}
103