Passed
Push — master ( 538fbf...6cd1ce )
by Gerhard
10:06
created

Loader::isArrayLike()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.074

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 5
c 1
b 0
f 1
nc 3
nop 1
dl 0
loc 9
ccs 5
cts 6
cp 0.8333
crap 4.074
rs 10
1
<?php
2
3
namespace Enhavo\Bundle\AppBundle\Endpoint;
4
5
use Symfony\Component\Yaml\Yaml;
6
7
class Loader
8
{
9 9
    public function __construct(
10
        public string $dataPath
11
    )
12
    {
13 9
    }
14
15 5
    public function merge(&$target, $source, ?bool $recursive = false, ?int $depth = null)
16
    {
17 5
        foreach ($source as $key => $value) {
18 5
            if ($recursive && ($depth === null || $depth > 0 ) && isset($target[$key]) && $this->isArrayLike($target[$key]) && $this->isArrayLike($value)) {
19 3
                $depth = is_numeric($depth) ? $depth - 1 : null;
20
                // if we have array like data, we can't pass it directly as a reference parameter, so lets save it
21
                // temporarily in a variable and write the values back later
22 3
                $temp = $target[$key];
23 3
                $this->merge($temp, $value, $recursive, $depth);
24 3
                $target[$key] = $temp;
25
            } else {
26 5
                $target[$key] = $value;
27
            }
28
        }
29
    }
30
31 3
    private function isArrayLike($data): bool
32
    {
33 3
        if (is_array($data)) {
34 3
            return true;
35 3
        } elseif ($data instanceof \ArrayAccess && $data instanceof \Traversable) {
36
            return true;
37
        }
38
39 3
        return false;
40
    }
41
42 4
    public function load($file): array
43
    {
44 4
        $path = realpath($this->dataPath) . '/' . $file;
45 4
        if (!file_exists($path)) {
46
            throw new \Exception(sprintf('File "%s" not exists', $file));
47
        }
48
49 4
        $ext = pathinfo($path, PATHINFO_EXTENSION);
50 4
        $fileData = match ($ext) {
51 4
            'yml', 'yaml' => $this->loadYamlFile($path),
52 4
            'json' => $this->loadJsonFile($path),
53 4
            'php' => $this->loadPHPFile($path),
54 4
            default => throw new \Exception(sprintf('Extension "%s" not supported', $ext))
0 ignored issues
show
Bug introduced by
It seems like $ext can also be of type array; however, parameter $values of sprintf() does only seem to accept double|integer|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

54
            default => throw new \Exception(sprintf('Extension "%s" not supported', /** @scrutinizer ignore-type */ $ext))
Loading history...
55 4
        };
56
57 4
        if (!is_array($fileData)) {
58
            throw new \Exception(sprintf('The file "%s" must return an array', $file));
59
        }
60
61 4
        return $fileData;
62
    }
63
64 1
    private function loadYamlFile($path)
65
    {
66 1
        return Yaml::parse(file_get_contents($path));
67
    }
68
69 1
    private function loadJsonFile($path)
70
    {
71 1
        return json_decode(file_get_contents($path), true);
72
    }
73
74 2
    private function loadPHPFile($path)
75
    {
76 2
        $callable = include $path;
77 2
        if (!is_callable($callable)) {
78
            throw new \Exception(sprintf('The file "%s" must return a callable', $path));
79
        }
80
81 2
        return $callable($this);
82
    }
83
}
84