Passed
Push — develop ( 388ba5...5ea0af )
by Brent
06:57 queued 58s
created

Config::defaults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Pageon;
4
5
use Dotenv\Dotenv;
6
use Dotenv\Exception\InvalidPathException;
7
use Illuminate\Support\Arr;
8
use Iterator;
9
use Stitcher\Exception\InvalidConfiguration;
10
use Stitcher\File;
11
use Symfony\Component\Finder\Finder;
12
13
class Config
14
{
15
    protected static $env;
16
    protected static $loadedConfiguration = [];
17
    protected static $plugins = [];
18
19 8
    public static function init()
20
    {
21 8
        self::$env = new Dotenv(File::path());
22
23
        try {
24 8
            self::$env->load();
25
        } catch (InvalidPathException $e) {
26
            throw InvalidConfiguration::dotEnvNotFound(File::path());
27
        }
28
29 8
        $configurationFiles = Finder::create()->files()->in(File::path('config'))->name('*.php')->getIterator();
30
31 8
        $loadedConfiguration = self::load($configurationFiles);
0 ignored issues
show
Bug introduced by
It seems like $configurationFiles defined by \Symfony\Component\Finde...'*.php')->getIterator() on line 29 can also be of type array<integer,object<Sym...nt\Finder\SplFileInfo>>; however, Pageon\Config::load() does only seem to accept object<Iterator>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
32
33 8
        self::registerPlugins($loadedConfiguration);
34
35 8
        self::registerConfiguration($loadedConfiguration);
36 8
    }
37
38 4
    public static function get(string $key)
39
    {
40 4
        return self::$loadedConfiguration[$key] ?? null;
41
    }
42
43 3
    public static function all(): array
44
    {
45 3
        return self::$loadedConfiguration;
46
    }
47
48 3
    public static function plugins(): array
49
    {
50 3
        return self::$plugins;
51
    }
52
53 8
    protected static function defaults(): array
54
    {
55
        return [
56 8
            'rootDirectory' => File::path(),
57 8
            'templateRenderer' => 'twig',
58
        ];
59
    }
60
61 8
    protected static function load(Iterator $configurationFiles): array
62
    {
63 8
        $loadedConfiguration = [];
64
65 8
        foreach ($configurationFiles as $configurationFile) {
66 8
            $loadedFileConfiguration = require $configurationFile;
67
68 8
            if (!is_array($loadedFileConfiguration)) {
69
                continue;
70
            }
71
72 8
            $loadedConfiguration = array_merge($loadedConfiguration, $loadedFileConfiguration);
73
        }
74
75 8
        return $loadedConfiguration;
76
    }
77
78 8
    protected static function registerPlugins(array $loadedConfiguration): void
79
    {
80 8
        self::$plugins = $loadedConfiguration['plugins'] ?? [];
81 8
    }
82
83 8
    protected static function registerConfiguration(array $loadedConfiguration): void
84
    {
85 8
        self::$loadedConfiguration = array_merge(self::defaults(), Arr::dot($loadedConfiguration));
86 8
    }
87
}
88