Passed
Push — develop ( f39ca2...6ce970 )
by Brent
03:12 queued 01:08
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 $loadedConfigfiguration = [];
17
18 6
    public static function init()
19
    {
20 6
        self::$env = new Dotenv(File::path());
21
22
        try {
23 6
            self::$env->load();
24
        } catch (InvalidPathException $e) {
25
            throw InvalidConfiguration::dotEnvNotFound(File::path());
26
        }
27
28 6
        $configurationFiles = Finder::create()->files()->in(File::path('config'))->name('*.php')->getIterator();
29
30 6
        $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...
31
32 6
        self::$loadedConfigfiguration = Arr::dot($loadedConfiguration);
33 6
    }
34
35 4
    public static function get(string $key)
36
    {
37 4
        return self::$loadedConfigfiguration[$key] ?? null;
38
    }
39
40 1
    public static function all(): array
41
    {
42 1
        return self::$loadedConfigfiguration;
43
    }
44
45 6
    protected static function load(Iterator $configurationFiles): array
46
    {
47 6
        $loadedConfiguration = [];
48
49 6
        foreach ($configurationFiles as $configurationFile) {
50 6
            $loadedFileConfiguration = require $configurationFile;
51
52 6
            if (!is_array($loadedFileConfiguration)) {
53
                continue;
54
            }
55
56 6
            $loadedConfiguration = array_merge($loadedConfiguration, $loadedFileConfiguration);
57
        }
58
59 6
        return $loadedConfiguration;
60
    }
61
}
62