Passed
Pull Request — master (#55)
by Alexander
12:20
created

Params::pushValues()   A

Complexity

Conditions 5
Paths 7

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 14
rs 9.6111
cc 5
nc 7
nop 3
1
<?php
2
3
namespace Yiisoft\Composer\Config\Config;
4
5
/**
6
 * Params class represents output configuration file with params definitions.
7
 */
8
class Params extends Config
9
{
10
    protected function calcValues(array $sources): array
11
    {
12
        return $this->pushEnvVars(parent::calcValues($sources));
13
    }
14
15
    protected function pushEnvVars(array $data): array
16
    {
17
        if (empty($data)) {
18
            return [];
19
        }
20
21
        $env = $this->builder->getConfig('envs')->getValues();
22
23
        return self::pushValues($data, $env);
24
    }
25
26
    public static function pushValues(array $data, array $values, string $prefix = null)
27
    {
28
        foreach ($data as $key => &$value) {
29
            $subkey = $prefix===null ? $key : "${prefix}_$key";
30
31
            $envkey = self::getEnvKey($subkey);
32
            if (isset($values[$envkey])) {
33
                $value = $values[$envkey];
34
            } elseif (is_array($value)) {
35
                $value = self::pushValues($value, $values, $subkey);
36
            }
37
        }
38
39
        return $data;
40
    }
41
42
    private static function getEnvKey(string $key): string
43
    {
44
        return strtoupper(strtr($key, '.-', '__'));
45
    }
46
47
    protected function paramsRequired(): bool
48
    {
49
        return false;
50
    }
51
}
52