Passed
Push — develop ( a0544e...04fdb9 )
by Brent
02:25
created

Config   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 94
Duplicated Lines 10.64 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 88.37%

Importance

Changes 0
Metric Value
dl 10
loc 94
ccs 38
cts 43
cp 0.8837
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 5

8 Methods

Rating   Name   Duplication   Size   Complexity  
B init() 10 28 4
A get() 0 4 1
A all() 0 4 1
A plugins() 0 4 1
A defaults() 0 12 1
A load() 0 16 3
A registerPlugins() 0 4 1
A registerConfiguration() 0 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 16
    public static function init(): void
20
    {
21 16
        self::$env = new Dotenv(File::path());
22
23
        try {
24 16
            self::$env->load();
25
        } catch (InvalidPathException $e) {
26
            throw InvalidConfiguration::dotEnvNotFound(File::path());
27
        }
28
29 16
        $loadedConfiguration = [];
30
31 16 View Code Duplication
        if (is_dir(File::path('config'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
32 16
            $configurationFiles = Finder::create()->files()->in(File::path('config'))->name('*.php')->getIterator();
33
34 16
            $loadedConfiguration = array_merge($loadedConfiguration, self::load($configurationFiles));
0 ignored issues
show
Bug introduced by
It seems like $configurationFiles defined by \Symfony\Component\Finde...'*.php')->getIterator() on line 32 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...
35
        }
36
37 16 View Code Duplication
        if (file_exists(File::path('src/config.php'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
            $sourceConfigurationFile = Finder::create()->files()->in(File::path('src'))->name('config.php')->getIterator();
39
40
            $loadedConfiguration = array_merge($loadedConfiguration, self::load($sourceConfigurationFile));
0 ignored issues
show
Bug introduced by
It seems like $sourceConfigurationFile defined by \Symfony\Component\Finde...ig.php')->getIterator() on line 38 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...
41
        }
42
43 16
        self::registerPlugins($loadedConfiguration);
44
45 16
        self::registerConfiguration($loadedConfiguration);
46 16
    }
47
48 7
    public static function get(string $key)
49
    {
50 7
        return self::$loadedConfiguration[$key] ?? null;
51
    }
52
53 10
    public static function all(): array
54
    {
55 10
        return self::$loadedConfiguration;
56
    }
57
58 10
    public static function plugins(): array
59
    {
60 10
        return self::$plugins;
61
    }
62
63 16
    protected static function defaults(): array
64
    {
65
        return [
66 16
            'rootDirectory' => File::path(),
67 16
            'resourcesPath' => File::path('resources'),
68 16
            'templateRenderer' => 'twig',
69
            'staticFiles' => [],
70
            'cacheStaticFiles' => false,
71
            'cacheImages' => true,
72 16
            'siteUrl' => '',
73
        ];
74
    }
75
76 16
    protected static function load(Iterator $configurationFiles): array
77
    {
78 16
        $loadedConfiguration = [];
79
80 16
        foreach ($configurationFiles as $configurationFile) {
81 16
            $loadedFileConfiguration = require $configurationFile;
82
83 16
            if (! \is_array($loadedFileConfiguration)) {
84
                continue;
85
            }
86
87 16
            $loadedConfiguration = array_merge($loadedConfiguration, $loadedFileConfiguration);
88
        }
89
90 16
        return $loadedConfiguration;
91
    }
92
93 16
    protected static function registerPlugins(array $loadedConfiguration): void
94
    {
95 16
        self::$plugins = $loadedConfiguration['plugins'] ?? [];
96 16
    }
97
98 16
    protected static function registerConfiguration(array $loadedConfiguration): void
99
    {
100 16
        self::$loadedConfiguration = array_merge(
101 16
            self::defaults(),
102 16
            $loadedConfiguration,
103 16
            Arr::dot($loadedConfiguration)
104
        );
105 16
    }
106
}
107