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   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 34
c 2
b 1
f 0
dl 0
loc 65
ccs 41
cts 41
cp 1
rs 10
wmc 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A scalarValueToString() 0 6 3
A resolve() 0 34 5
A resolveDefaultValue() 0 15 3
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