Total Complexity | 46 |
Total Lines | 246 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Env often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Env, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | trait Env{ |
||
25 | |||
26 | /** |
||
27 | * a backup environment in case everything goes downhill |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | private $_ENV = []; |
||
32 | |||
33 | /** |
||
34 | * Sets the global $_ENV if true. Otherwise all variables are being kept internally |
||
35 | * in $this->_ENV to avoid leaks, making them only accessible via Env::__getEnv(). |
||
36 | * |
||
37 | * @var bool |
||
38 | */ |
||
39 | private $_global; |
||
40 | |||
41 | /** |
||
42 | * @param string $path |
||
43 | * @param string|null $filename |
||
44 | * @param bool|null $overwrite |
||
45 | * @param array|null $required |
||
46 | * @param bool|null $global |
||
47 | * |
||
48 | * @return $this |
||
49 | */ |
||
50 | protected function __loadEnv(string $path, string $filename = null, bool $overwrite = null, array $required = null, bool $global = null){ |
||
57 | ; |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @param string $var |
||
62 | * |
||
63 | * @return bool|mixed |
||
64 | */ |
||
65 | protected function __getEnv(string $var){ |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @param string $var |
||
90 | * @param string $value |
||
91 | * |
||
92 | * @return $this |
||
93 | */ |
||
94 | protected function __setEnv(string $var, string $value = null){ |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * @param string $var |
||
119 | * |
||
120 | * @return bool |
||
121 | */ |
||
122 | public function __issetEnv(string $var):bool { |
||
123 | return |
||
124 | ($this->_global && ( |
||
125 | isset($_ENV[$var]) |
||
126 | || getenv($var) |
||
127 | || (function_exists('apache_getenv') && apache_getenv($var)) |
||
128 | )) |
||
129 | || array_key_exists($var, $this->_ENV); |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * @param string $var |
||
134 | * |
||
135 | * @return $this |
||
136 | */ |
||
137 | protected function __unsetEnv(string $var){ |
||
138 | $var = strtoupper($var); |
||
139 | |||
140 | if($this->_global === true){ |
||
141 | unset($_ENV[$var]); |
||
142 | putenv($var); |
||
143 | } |
||
144 | |||
145 | unset($this->_ENV[$var]); |
||
146 | |||
147 | return $this; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * use with caution! |
||
152 | * |
||
153 | * @return $this |
||
154 | */ |
||
155 | protected function __clearEnv(){ |
||
156 | |||
157 | if($this->_global === true){ |
||
158 | $_ENV = []; |
||
159 | } |
||
160 | |||
161 | $this->_ENV = []; |
||
162 | |||
163 | return $this; |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * @param string $file |
||
168 | * |
||
169 | * @return array |
||
170 | * @throws \chillerlan\Traits\TraitException |
||
171 | */ |
||
172 | private function __read(string $file):array{ |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * @param array $data |
||
193 | * @param bool $overwrite |
||
194 | * |
||
195 | * @return $this |
||
196 | */ |
||
197 | private function __load(array $data, bool $overwrite){ |
||
198 | |||
199 | foreach($data as $line){ |
||
200 | |||
201 | // skip empty lines and comments |
||
202 | if(empty($line) || strpos($line, '#') === 0){ |
||
203 | continue; |
||
204 | } |
||
205 | |||
206 | $kv = array_map('trim', explode('=', $line, 2)); |
||
207 | |||
208 | // skip empty and numeric keys, keys with spaces, existing keys that shall not be overwritten |
||
209 | if(empty($kv[0]) || is_numeric($kv[0]) || strpos($kv[0], ' ') !== false || (!$overwrite && $this->__getEnv($kv[0]) !== false)){ |
||
210 | continue; |
||
211 | } |
||
212 | |||
213 | $this->__setEnv($kv[0], isset($kv[1]) ? trim($kv[1]) : null); |
||
214 | } |
||
215 | |||
216 | return $this; |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * @param string $value |
||
221 | * |
||
222 | * @return string|null |
||
223 | */ |
||
224 | private function __parse(string $value = null){ |
||
225 | |||
226 | if($value !== null){ |
||
227 | |||
228 | $q = $value[0] ?? null; |
||
229 | |||
230 | $value = in_array($q, ["'", '"'], true) |
||
231 | // handle quoted strings |
||
232 | ? preg_replace("/^$q((?:[^$q\\\\]|\\\\\\\\|\\\\$q)*)$q.*$/mx", '$1', $value) |
||
233 | // skip inline comments |
||
234 | : trim(explode('#', $value, 2)[0]); |
||
235 | |||
236 | // handle multiline values |
||
237 | $value = implode(PHP_EOL, explode('\\n', $value)); |
||
238 | |||
239 | // handle nested ${VARS} |
||
240 | if(strpos($value, '$') !== false){ |
||
241 | $value = preg_replace_callback('/\${(?<var>[_a-z\d]+)}/i', function($matches){ |
||
242 | return $this->__getEnv($matches['var']); |
||
243 | }, $value); |
||
244 | } |
||
245 | |||
246 | } |
||
247 | |||
248 | return $value; |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * @param array|null $required |
||
253 | * |
||
254 | * @return $this |
||
255 | * @throws \chillerlan\Traits\TraitException |
||
256 | */ |
||
257 | private function __check(array $required = null){ |
||
270 | } |
||
271 | } |
||
272 |