GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Branch feature/normalise_config_files... (7ea4db)
by Marc
03:05
created

ChapiConfig::loadConfigs()   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
dl 0
loc 16
ccs 7
cts 8
cp 0.875
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 0
crap 3.0175
1
<?php
2
/**
3
 * @package: chapi
4
 *
5
 * @author:  msiebeneicher
6
 * @since:   2017-03-22
7
 *
8
 */
9
10
namespace Chapi\Component\Config;
11
12
use Symfony\Component\Yaml\Parser as YamlParser;
13
14
class ChapiConfig implements ChapiConfigInterface
15
{
16
    /**
17
     * @var string[]
18
     */
19
    private $aDirectoryPaths = [];
20
21
    /**
22
     * @var YamlParser
23
     */
24
    private $oParser;
25
26
    /**
27
     * @var string
28
     */
29
    private $sActiveProfile = '';
30
31
    /**
32
     * @var array
33
     */
34
    private $aConfig = null;
35
36
    /**
37
     * ChapiConfig constructor.
38
     * @param array $aDirectoryPaths
39
     * @param YamlParser $oParser
40
     * @param string $sActiveProfile
41
     */
42 3
    public function __construct(
43
        $aDirectoryPaths,
44
        YamlParser $oParser,
45
        $sActiveProfile
46
    )
47
    {
48 3
        $this->aDirectoryPaths = $aDirectoryPaths;
49 3
        $this->oParser = $oParser;
50 3
        $this->sActiveProfile = $sActiveProfile;
51 3
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56 2
    public function getProfileConfig() {
57 2
        $_aConfig = $this->getConfig();
58 2
        return $_aConfig['profiles'][$this->sActiveProfile];
59
    }
60
61
    /**
62
     * @inheritdoc
63
     */
64 3
    public function getConfig() {
65 3
        if (is_null($this->aConfig)) {
66 3
            $this->aConfig = $this->loadConfigs();
67
        }
68
69 3
        return $this->aConfig;
70
    }
71
72
    /**
73
     * @return array
74
     */
75 3
    private function loadConfigs() {
76 3
        $_aConfig = [];
77
78 3
        foreach ($this->aDirectoryPaths as $_sDirectoryPath)
79
        {
80 3
            if (!is_dir($_sDirectoryPath))
81
            {
82
                throw new \InvalidArgumentException(sprintf('Path "%s" is not valid', $_sDirectoryPath));
83
            }
84
85 3
            $_aConfigPart = $this->loadConfig($_sDirectoryPath);
86 3
            $_aConfig = self::array_merge_recursive_distinct($_aConfig, $_aConfigPart);
87
        }
88
89 3
        return $_aConfig;
90
    }
91
92
    /**
93
     * @param string $sPath
94
     * @return array
1 ignored issue
show
Documentation introduced by
Should the return type not be string|array|\stdClass? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
95
     */
96 3
    private function loadConfig($sPath) {
97 3
        $_sFilePath = $sPath . DIRECTORY_SEPARATOR . self::CONFIG_FILE_NAME;
98
99 3
        if (is_file($_sFilePath)) {
100 3
            $_aConfig = $this->oParser->parse(
101
                file_get_contents($_sFilePath)
102
            );
103
104 3
            return $_aConfig;
105
        }
106
107
        return [];
108
    }
109
110
    /**
111
     * array_merge_recursive does indeed merge arrays, but it converts values with duplicate
112
     * keys to arrays rather than overwriting the value in the first array with the duplicate
113
     * value in the second array, as array_merge does. I.e., with array_merge_recursive,
114
     * this happens (documented behavior):
115
     *
116
     * array_merge_recursive(array('key' => 'org value'), array('key' => 'new value'));
117
     *     => array('key' => array('org value', 'new value'));
118
     *
119
     * array_merge_recursive_distinct does not change the datatypes of the values in the arrays.
120
     * Matching keys' values in the second array overwrite those in the first array, as is the
121
     * case with array_merge, i.e.:
122
     *
123
     * array_merge_recursive_distinct(array('key' => 'org value'), array('key' => 'new value'));
124
     *     => array('key' => array('new value'));
125
     *
126
     * Parameters are passed by reference, though only for performance reasons. They're not
127
     * altered by this function.
128
     *
129
     * @param array $array1
130
     * @param array $array2
131
     * @return array
132
     * @author Daniel <daniel (at) danielsmedegaardbuus (dot) dk>
133
     * @author Gabriel Sobrinho <gabriel (dot) sobrinho (at) gmail (dot) com>
134
     *
135
     * @see http://php.net/manual/de/function.array-merge-recursive.php
136
     */
137 3
    private static function array_merge_recursive_distinct ( array &$array1, array &$array2 )
0 ignored issues
show
Coding Style introduced by
function array_merge_recursive_distinct() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
138
    {
139 3
        $merged = $array1;
140
141 3
        foreach ($array2 as $key => &$value)
142
        {
143 3
            if (is_array($value) && isset ($merged[$key]) && is_array($merged[$key]))
144
            {
145 1
                $merged[$key] = self::array_merge_recursive_distinct($merged[$key], $value);
146
            }
147
            else
148
            {
149
                // add new element for numeric arrays
150 3
                if (isset($merged[$key]) && is_numeric($key))
151
                {
152 1
                    $merged[] = $value;
153
                }
154
                else
155
                {
156 3
                    $merged[$key] = $value;
157
                }
158
            }
159
        }
160
161 3
        return $merged;
162
    }
163
}