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

Environment   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 38
ccs 16
cts 16
cp 1
rs 10
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A transformString() 0 6 2
A __construct() 0 6 2
A transform() 0 9 3
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