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... (35a6c3)
by Marc
03:27
created

ChapiConfig::arrayMergeRecursiveDistinct()   C

Complexity

Conditions 7
Paths 4

Size

Total Lines 26
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 9
cts 9
cp 1
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 11
nc 4
nop 2
crap 7
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::arrayMergeRecursiveDistinct($_aConfig, $_aConfigPart);
87
        }
88
89 3
        return $_aConfig;
90
    }
91
92
    /**
93
     * @param string $sPath
94
     * @return array
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 arrayMergeRecursiveDistinct(array &$array1, array &$array2)
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::arrayMergeRecursiveDistinct($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
}