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

Json::load()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 11
rs 10
1
<?php
2
3
/*
4
 * This file is part of the "andrey-helldar/laravel-lang-publisher" project.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @author Andrey Helldar <[email protected]>
10
 *
11
 * @copyright 2021 Andrey Helldar
12
 *
13
 * @license MIT
14
 *
15
 * @see https://github.com/andrey-helldar/laravel-lang-publisher
16
 */
17
18
declare(strict_types=1);
19
20
namespace Helldar\LaravelLangPublisher\Support\Filesystem;
21
22
use Helldar\PrettyArray\Services\File as Pretty;
23
use Helldar\Support\Facades\Helpers\Ables\Arrayable;
24
use Helldar\Support\Facades\Helpers\Arr;
25
26
class Json extends Base
27
{
28
    public function load(string $path): array
29
    {
30
        if ($this->doesntExists($path)) {
31
            return [];
32
        }
33
34
        $content = Pretty::make()->loadRaw($path);
35
36
        $items = json_decode($content, true);
37
38
        return $this->correct($items);
39
    }
40
41
    public function store(string $path, $content): string
42
    {
43
        Arr::storeAsJson($path, $content, false, JSON_UNESCAPED_UNICODE ^ JSON_PRETTY_PRINT);
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return string. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
44
    }
45
46
    protected function correct(array $items): array
47
    {
48
        $items = Arrayable::of($items)
49
            ->renameKeys(static function ($key, string $value) {
50
                return $value;
51
            })->get();
52
53
        return parent::correct($items);
54
    }
55
}
56