Passed
Push — develop ( 9dc7fc...abb005 )
by Brent
02:10
created

Config::load()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0175

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 16
ccs 7
cts 8
cp 0.875
crap 3.0175
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
18 7
    public static function init()
19
    {
20 7
        self::$env = new Dotenv(File::path());
21
22
        try {
23 7
            self::$env->load();
24
        } catch (InvalidPathException $e) {
25
            throw InvalidConfiguration::dotEnvNotFound(File::path());
26
        }
27
28 7
        $configurationFiles = Finder::create()->files()->in(File::path('config'))->name('*.php')->getIterator();
29
30 7
        self::loadDefaults();
31
32 7
        $loadedConfiguration = self::load($configurationFiles);
0 ignored issues
show
Bug introduced by
It seems like $configurationFiles defined by \Symfony\Component\Finde...'*.php')->getIterator() on line 28 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...
33
34 7
        self::$loadedConfiguration = array_merge(self::$loadedConfiguration, Arr::dot($loadedConfiguration));
35 7
    }
36
37 4
    public static function get(string $key)
38
    {
39 4
        return self::$loadedConfiguration[$key] ?? null;
40
    }
41
42 2
    public static function all(): array
43
    {
44 2
        return self::$loadedConfiguration;
45
    }
46
47 7
    protected static function loadDefaults(): void
48
    {
49 7
        self::$loadedConfiguration['rootDirectory'] = File::path();
50 7
        self::$loadedConfiguration['templateRenderer'] = 'twig';
51 7
    }
52
53 7
    protected static function load(Iterator $configurationFiles): array
54
    {
55 7
        $loadedConfiguration = [];
56
57 7
        foreach ($configurationFiles as $configurationFile) {
58 7
            $loadedFileConfiguration = require $configurationFile;
59
60 7
            if (!is_array($loadedFileConfiguration)) {
61
                continue;
62
            }
63
64 7
            $loadedConfiguration = array_merge($loadedConfiguration, $loadedFileConfiguration);
65
        }
66
67 7
        return $loadedConfiguration;
68
    }
69
}
70