EnvValueHandler   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A handle() 0 13 2
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
15
     * @psalm-suppress InvalidArrayOffset, MixedArgumentTypeCoercion
16
     */
17 29
    public function handle(string $input): string
18
    {
19
20 29
        return preg_replace_callback(
21 29
            '/(%)([A-Z_0-9]+)(%)/',
22 29
            function (array $matches) {
23 6
                return
24 6
                    $_ENV[$matches[2]]
25 6
                    ?? $_SERVER[$matches[2]]
26 6
                    ?? (getenv($matches[2]) === false ? $matches[1] . $matches[2] . $matches[3] : getenv($matches[2]));
27 29
            },
28 29
            $input
29 29
        ) ?? '';
30
    }
31
}
32