GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Variables::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 2
ccs 1
cts 1
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
6
namespace Enjoys\Dotenv;
7
8
9
use Enjoys\Dotenv\Exception\InvalidArgumentException;
10
11
final class Variables
12
{
13 52
    public function __construct(private Dotenv $dotenv)
14
    {
15 52
    }
16
17 28
    public static function scalarValueToString(string|bool|int|float|null $value): string
18
    {
19 28
        if (gettype($value) === 'boolean') {
20 6
            return $value ? 'true' : 'false';
21
        }
22 25
        return (string)$value;
23
    }
24
25 48
    public function resolve(string $key, ?string $value): ?string
26
    {
27 48
        if ($value === null) {
28 4
            return null;
29
        }
30
31 47
        $result = preg_replace_callback(
32 47
            '/(\${(?<variable>.+?)(?<default_value>:[-=?][^}]*)?})/',
33 47
            function (array $matches): string {
34 21
                $env = getenv($matches['variable']) !== false ? getenv($matches['variable']) : null;
35
36
                /** @var string|bool|int|float|null $val */
37 21
                $val =
38 21
                    $env ??
39 21
                    $this->dotenv->getEnvCollection()->get($matches['variable']) ??
40 21
                    $this->dotenv->getEnvRawArray()[$matches['variable']] ??
41 21
                    (($matches['default_value'] ?? null) !== null
42 7
                        ? $this->resolveDefaultValue(
43 7
                            $matches['default_value'],
44 7
                            $matches['variable']
45 7
                        ) : null)
46 16
                    ?? '';
47
48 19
                return self::scalarValueToString($val);
49 47
            },
50 47
            $value,
51 47
            flags: PREG_UNMATCHED_AS_NULL
52 47
        );
53
54 46
        if (preg_match('/(\${(.+?)})/', $result ?? '')) {
55 1
            return $this->resolve($key, $result);
56
        }
57
58 46
        return $result;
59
    }
60
61 6
    private function resolveDefaultValue(string $default_value, string $variable): string
62
    {
63 6
        $value = substr($default_value, 2);
64
65 6
        if ('?' === $default_value[1]) {
66 2
            throw new InvalidArgumentException(sprintf('Not set variable ${%s}. %s', $variable, $value));
67
        }
68
69 4
        if ('=' === $default_value[1]) {
70 3
            $this->dotenv->populate(
71 3
                $variable,
72 3
                $value,
73 3
            );
74
        }
75 4
        return $value;
76
    }
77
}
78