Passed
Pull Request — main (#123)
by Andrey
26:46 queued 11:34
created

Json::loadTranslations()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 1
nop 1
dl 0
loc 10
ccs 0
cts 0
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Helldar\LaravelLangPublisher\Services\Filesystem;
4
5
use Helldar\PrettyArray\Services\File as Pretty;
6
use Helldar\Support\Facades\Helpers\Ables\Arrayable;
7
use Helldar\Support\Facades\Helpers\Arr;
8
9
final class Json extends Filesystem
10 28
{
11
    public function load(string $path, string $main_path = null): array
12 28
    {
13
        $this->log('Loading the contents of the file:', $path, '(', $main_path, ')');
14 28
15 28
        if ($this->doesntExists($path) || ($main_path && $this->doesntExists($main_path))) {
16
            $path = $this->doesntExists($path) ? $path : $main_path;
17 28
18
            $this->log('File not found:', $path);
19
20 28
            return [];
21
        }
22 28
23
        if (! empty($path) && empty($main_path)) {
24 28
            return $this->loadTranslations($path);
25
        }
26 28
27
        $keys   = $this->loadKeys($path);
28
        $source = $this->loadTranslations($main_path);
0 ignored issues
show
Bug introduced by
It seems like $main_path can also be of type null; however, parameter $path of Helldar\LaravelLangPubli...son::loadTranslations() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

28
        $source = $this->loadTranslations(/** @scrutinizer ignore-type */ $main_path);
Loading history...
29 28
30
        return Arr::only($source, $keys);
31 28
    }
32
33 28
    public function store(string $path, array $content)
34 28
    {
35
        $this->log('Saving an array to a file:', $path);
36
37
        Arr::storeAsJson($path, $content, false, JSON_UNESCAPED_UNICODE ^ JSON_PRETTY_PRINT);
38
    }
39
40
    protected function loadKeys(string $path): array
41
    {
42
        $this->log('Loading keys from a file:', $path);
43
44
        return $this->loadFile($path);
45
    }
46
47
    protected function loadTranslations(string $path): array
48
    {
49
        $this->log('Loading translations from a file:', $path);
50
51
        $items = $this->loadFile($path);
52
53
        return Arrayable::of($items)
54
            ->renameKeys(static function ($key, $value) {
55
                return is_numeric($key) ? $value : $key;
56
            })->get();
57
    }
58
59
    protected function loadFile(string $path): array
60
    {
61
        $this->log('Loading data from a file:', $path);
62
63
        $content = Pretty::make()->loadRaw($path);
64
65
        $items = json_decode($content, true);
66
67
        return $this->correctValues($items);
68
    }
69
}
70