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
Push — master ( 0d3ad3...f8c354 )
by Enjoys
02:18
created

Dotenv::setEnvArrayOne()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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