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
Pull Request — master (#8)
by Enjoys
07:17
created

Dotenv::loadEnv()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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