EnvValueHandler::handle()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 2
eloc 9
c 2
b 1
f 1
nc 1
nop 1
dl 0
loc 13
ccs 11
cts 11
cp 1
crap 2
rs 9.9666
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