Params::paramsRequired()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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