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.
Passed
Branch master (e5241c)
by Enjoys
02:12
created

Dotenv::doMerge()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
6
namespace Enjoys\Dotenv;
7
8
use Webmozart\Assert\Assert;
9
10
class Dotenv
11
{
12
    private string $baseDirectory;
13
    /**
14
     * @var string[]
15
     */
16
    private array $envArray = [];
17
18 18
    public function __construct(
19
        string $baseDirectory,
20
        private string $envFilename = '.env',
21
        private string $distEnvFilename = '.env.dist'
22
    ) {
23 18
        $this->baseDirectory = rtrim($baseDirectory, "/") . DIRECTORY_SEPARATOR;
24 18
    }
25
26 17
    public function loadEnv(bool $usePutEnv = false): void
27
    {
28 17
        $this->doMerge($this->getGeneralPaths());
29 16
        $this->doMerge($this->getExtraPaths());
30 16
        $this->doLoad($usePutEnv);
31 10
    }
32
33
    /**
34
     * @return string[]
35
     */
36 16
    private function getExtraPaths(): array
37
    {
38 16
        $env = (getenv('APP_ENV') ?: null) ?? $this->envArray['APP_ENV'] ?? null;
39
40 16
        if ($env === '' || $env === null) {
41 11
            return [];
42
        }
43 5
        $path = realpath($this->baseDirectory . $this->envFilename . '.' . $env);
44
45 5
        if ($path === false) {
46 1
            return [];
47
        }
48
49 4
        return [$path];
50
    }
51
52
    /**
53
     * @return string[]
54
     */
55 17
    private function getGeneralPaths(): array
56
    {
57 17
        $paths = [
58 17
            realpath($this->baseDirectory . $this->distEnvFilename),
59 17
            realpath($this->baseDirectory . $this->envFilename)
60
        ];
61
62 17
        return array_filter($paths, function ($item) {
63 17
            return is_string($item);
64 17
        });
65
    }
66
67
    /**
68
     * @param string[] $array
69
     */
70 17
    private function doMerge(array $array): void
71
    {
72 17
        foreach ($array as $path) {
73 17
            $this->envArray = array_merge($this->envArray, $this->getArrayData($path));
74
        }
75 16
    }
76
77
    /**
78
     * @return string[]
79
     */
80 17
    private function getArrayData(string $path): array
81
    {
82 17
        $result = [];
83
84 17
        $data = file_get_contents($path);
85
86
        /**
87
         * @var string $key
88
         * @var string $value
89
         */
90 17
        foreach ($this->parseToArray($data) as $key => $value) {
91 16
            $result[$key] = $value;
92
        }
93 16
        return $result;
94
    }
95
96
97 17
    private function parseToArray(string $input): \Generator
98
    {
99 17
        foreach (preg_split("/\R/", $input) as $line) {
100 17
            $line = trim($line);
101 17
            if ($this->isComment($line)) {
102 3
                continue;
103
            }
104 17
            $fields = array_map('trim', explode('=', $line, 2));
105
106 17
            if (count($fields) == 2) {
107 17
                list($key, $value) = $fields;
108 17
                Assert::regex(
109 17
                    $key,
110 17
                    '/^([A-Z_0-9]+)$/i',
111 17
                    'The key %s have invalid chars. The key must have only letters (A-Z) digits (0-9) and _'
112
                );
113
114
115 16
                yield $key => $this->parseValue($value);
116
            }
117
        }
118 16
    }
119
120
121 16
    private function doLoad(bool $usePutEnv): void
122
    {
123
        /** @var string $key */
124 16
        foreach ($this->envArray as $key => $value) {
125 16
            $value = ValuesHandler::quotes($value);
126 16
            $value = ValuesHandler::handleVariables($key, $value, $this);
127
128 16
            if (getenv($key)) {
129 3
                $value = getenv($key);
130
            }
131
132
            /** @var string $value */
133 16
            $_ENV[$key] = ValuesHandler::cast($value);
134
135 11
            if (!getenv($key) && $usePutEnv === true) {
136 1
                putenv("$key=$value");
137
            }
138
        }
139 10
    }
140
141 17
    private function isComment(string $line): bool
142
    {
143 17
        return (bool)preg_match('/^#/', $line);
144
    }
145
146 16
    private function parseValue(string $value): string
147
    {
148 16
        preg_match('/^[\"\'](.+)[\"\']$|(.[^#]+)/', $value, $matches);
149
150 16
        return trim($matches[2] ?? $value);
151
    }
152
153
    /**
154
     * @return string[]
155
     */
156 5
    public function getEnvArray(): array
157
    {
158 5
        return $this->envArray;
159
    }
160
161
}