Passed
Push — master ( a30881...22e634 )
by Andrii
10:48
created

Params   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 16
c 1
b 0
f 0
dl 0
loc 43
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A calcValues() 0 3 1
A pushEnvVars() 0 9 2
A getEnvKey() 0 3 1
A paramsRequired() 0 3 1
A pushValues() 0 14 5
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
43
    private static function getEnvKey(string $key): string
44
    {
45
        return strtoupper(strtr($key, '.-', '__'));
46
    }
47
48
    protected function paramsRequired(): bool
49
    {
50
        return false;
51
    }
52
}
53