ArgumentTranslator::process()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 11
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Antidot\SymfonyConfigTranslator;
6
7
use function str_replace;
8
use function strpos;
9
10
class ArgumentTranslator
11
{
12 6
    public function process(array $config, array $service): array
13
    {
14 6
        $arguments = [];
15
        /**
16
         * @var string $argument
17
         */
18 6
        foreach ($service['arguments'] as $argument => $value) {
19 6
            $arguments[str_replace('$', '', $argument)] = $this->getArguments($config, $value);
20
        }
21
22 6
        return $arguments;
23
    }
24
25
    /**
26
     * @param array<mixed> $config
27
     * @param mixed $value
28
     * @return array<mixed>|mixed|string|string[]
29
     */
30 6
    private function getArguments(array $config, $value)
31
    {
32 6
        if (is_array($value)) {
33
            return $value;
34
        }
35
36 6
        $isService = 0 === strpos($value, '@');
37 6
        if ($isService) {
38 2
            return str_replace('@', '', $value);
39
        }
40
        /** @var int|string $index */
41 6
        $index = str_replace(['%config%', '%config.', '%'], '', $value);
42 6
        if (array_key_exists($index, $config)) {
43 2
            return $config[$index];
44
        }
45
46 4
        return $value;
47
    }
48
}
49