Passed
Push — master ( 034b15...36859c )
by Caen
04:33 queued 14s
created

YamlConfigurationRepository::parseYamlFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 3
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Foundation\Internal;
6
7
use Hyde\Hyde;
8
use Illuminate\Support\Arr;
9
use Symfony\Component\Yaml\Yaml;
10
11
use function file_exists;
12
use function file_get_contents;
13
use function array_key_first;
14
15
/**
16
 * @internal Contains shared logic for loading and parsing the YAML configuration file.
17
 *
18
 * @see LoadYamlEnvironmentVariables Which uses this repository to inject environment variables from the YAML configuration file.
19
 * @see LoadYamlConfiguration Which uses this repository to merge the YAML configuration data with the existing configuration.
20
 */
21
class YamlConfigurationRepository
22
{
23
    protected false|string $file;
24
    protected array $data;
25
26
    public function __construct()
27
    {
28
        $this->file = $this->getFilePath();
29
30
        if ($this->hasYamlConfigFile()) {
31
            $data = $this->parseYamlFile();
32
33
            if (! self::configurationContainsNamespaces($data)) {
34
                $data = ['hyde' => $data];
35
            }
36
37
            $this->data = $data;
38
        }
39
    }
40
41
    /** @return array<string, array<string, null|scalar|array>> */
42
    public function getData(): array
43
    {
44
        return $this->data;
45
    }
46
47
    public function hasYamlConfigFile(): bool
48
    {
49
        return $this->file !== false;
50
    }
51
52
    protected function parseYamlFile(): array
53
    {
54
        return Arr::undot((array) Yaml::parse(file_get_contents($this->file)));
0 ignored issues
show
Bug introduced by
It seems like $this->file can also be of type false; however, parameter $filename of file_get_contents() 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

54
        return Arr::undot((array) Yaml::parse(file_get_contents(/** @scrutinizer ignore-type */ $this->file)));
Loading history...
55
    }
56
57
    protected function getFilePath(): string|false
58
    {
59
        return match (true) {
60
            file_exists(Hyde::path('hyde.yml')) => Hyde::path('hyde.yml'),
61
            file_exists(Hyde::path('hyde.yaml')) => Hyde::path('hyde.yaml'),
62
            default => false,
63
        };
64
    }
65
66
    protected static function configurationContainsNamespaces(array $config): bool
67
    {
68
        return array_key_first($config) === 'hyde';
69
    }
70
}
71