Passed
Push — master ( ba4ca1...142814 )
by Evgeniy
58s queued 11s
created

Environment::transformString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cekta\DI\Provider;
6
7
class Environment extends KeyValue
8
{
9
    private const TRANSFORM = [
10
        'true' => true,
11
        '(true)' => true,
12
        'false' => false,
13
        '(false)' => false,
14
        'null' => null,
15
        '(null)' => null,
16
        'empty' => '',
17
        '(empty)' => '',
18
    ];
19
20 6
    public function __construct(array $params)
21
    {
22 6
        foreach ($params as $key => $value) {
23 3
            $params[$key] = self::transform($value);
24
        }
25 6
        parent::__construct($params);
26 6
    }
27
28 3
    private static function transform($value)
29
    {
30 3
        if (is_string($value)) {
31 3
            if (preg_match('/\A([\'"])(.*)\1\z/', $value, $matches)) {
32 3
                $value = $matches[2];
33
            }
34 3
            $value = self::transformString($value);
35
        }
36 3
        return $value;
37
    }
38
39 3
    private static function transformString(string $value)
40
    {
41 3
        $lower = strtolower($value);
42 3
        return array_key_exists($lower, self::TRANSFORM)
43 3
            ? self::TRANSFORM[$lower]
44 3
            : $value;
45
    }
46
}
47