Passed
Push — master ( 07418a...b054ee )
by Enjoys
01:44
created

EnvValueHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 8
c 1
b 0
f 1
dl 0
loc 19
ccs 10
cts 10
cp 1
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 12 4
1
<?php
2
3
declare(strict_types=1);
4
5
6
namespace Enjoys\Config\ValueHandler;
7
8
9
use Enjoys\Config\ValueHandlerInterface;
10
11
final class EnvValueHandler implements ValueHandlerInterface
12
{
13
    /**
14
     * @param mixed $value
15
     * @return  mixed
16
     * @psalm-suppress InvalidArrayOffset, MixedArgumentTypeCoercion
17
     */
18 12
    public function handle($value)
19
    {
20 12
        if (is_array($value) || is_string($value)) {
21 8
            return preg_replace_callback(
22 8
                '/(%)([A-Z_]+)(%)/',
23 8
                function ($matches) {
24 4
                    return $_ENV[$matches[2]] ?? $_SERVER[$matches[2]] ?? (getenv($matches[2]) ?: $matches[2]);
25 8
                },
26 8
                $value
27 8
            );
28
        }
29 4
        return $value;
30
    }
31
}
32