Conditions | 77 |
Paths | 17 |
Total Lines | 293 |
Code Lines | 216 |
Lines | 14 |
Ratio | 4.78 % |
Changes | 4 | ||
Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
160 | protected function __construct($file, $target, $included = false) |
||
161 | { |
||
162 | $this->file = $file; |
||
163 | $this->target = $target; |
||
164 | $this->revision = ++Object::$lastRevision; |
||
165 | $this->data = file_get_contents($file); |
||
166 | |||
167 | if (substr($this->data, 0, 2) === '#!') { |
||
168 | if (!is_executable($file)) { |
||
169 | $this->raiseError('Shebang (#!) detected in the first line, but file hasn\'t +x mode.'); |
||
170 | return; |
||
171 | } |
||
172 | $this->data = shell_exec($file); |
||
173 | } |
||
174 | |||
175 | $this->data = str_replace("\r", '', $this->data); |
||
176 | $this->length = mb_orig_strlen($this->data); |
||
177 | $this->state[] = [static::T_ALL, $this->target]; |
||
178 | $this->tokens = [ |
||
179 | static::T_COMMENT => function ($c) { |
||
180 | if ($c === "\n") { |
||
181 | array_pop($this->state); |
||
182 | } |
||
183 | }, |
||
184 | static::T_STRING_DOUBLE => function ($q) { |
||
185 | $str = ''; |
||
186 | ++$this->p; |
||
187 | |||
188 | for (; $this->p < $this->length; ++$this->p) { |
||
189 | $c = $this->getCurrentChar(); |
||
190 | |||
191 | if ($c === $q) { |
||
192 | ++$this->p; |
||
193 | break; |
||
194 | } elseif ($c === '\\') { |
||
195 | next: |
||
196 | $n = $this->getNextChar(); |
||
197 | if ($n === $q) { |
||
198 | $str .= $q; |
||
199 | ++$this->p; |
||
200 | } elseif (ctype_digit($n)) { |
||
201 | $def = $n; |
||
202 | ++$this->p; |
||
203 | View Code Duplication | for (; $this->p < min($this->length, $this->p + 2); ++$this->p) { |
|
204 | $n = $this->getNextChar(); |
||
205 | if (!ctype_digit($n)) { |
||
206 | break; |
||
207 | } |
||
208 | $def .= $n; |
||
209 | } |
||
210 | $str .= chr((int) $def); |
||
211 | } elseif (($n === 'x') || ($n === 'X')) { |
||
212 | $def = $n; |
||
213 | ++$this->p; |
||
214 | View Code Duplication | for (; $this->p < min($this->length, $this->p + 2); ++$this->p) { |
|
215 | $n = $this->getNextChar(); |
||
216 | if (!ctype_xdigit($n)) { |
||
217 | break; |
||
218 | } |
||
219 | $def .= $n; |
||
220 | } |
||
221 | $str .= chr((int) hexdec($def)); |
||
222 | } else { |
||
223 | $str .= $c; |
||
224 | } |
||
225 | } else { |
||
226 | $str .= $c; |
||
227 | } |
||
228 | } |
||
229 | |||
230 | if ($this->p >= $this->length) { |
||
231 | $this->raiseError('Unexpected End-Of-File.'); |
||
232 | } |
||
233 | return $str; |
||
234 | }, |
||
235 | static::T_STRING => function ($q) { |
||
236 | $str = ''; |
||
237 | ++$this->p; |
||
238 | |||
239 | for (; $this->p < $this->length; ++$this->p) { |
||
240 | $c = $this->getCurrentChar(); |
||
241 | |||
242 | if ($c === $q) { |
||
243 | ++$this->p; |
||
244 | break; |
||
245 | } elseif ($c === '\\') { |
||
246 | if ($this->getNextChar() === $q) { |
||
247 | $str .= $q; |
||
248 | ++$this->p; |
||
249 | } else { |
||
250 | $str .= $c; |
||
251 | } |
||
252 | } else { |
||
253 | $str .= $c; |
||
254 | } |
||
255 | } |
||
256 | |||
257 | if ($this->p >= $this->length) { |
||
258 | $this->raiseError('Unexpected End-Of-File.'); |
||
259 | } |
||
260 | return $str; |
||
261 | }, |
||
262 | static::T_ALL => function ($c) { |
||
263 | if (ctype_space($c)) { |
||
264 | } elseif ($c === '#') { |
||
265 | $this->state[] = [static::T_COMMENT]; |
||
266 | } elseif ($c === '}') { |
||
267 | if (sizeof($this->state) > 1) { |
||
268 | $this->purgeScope($this->getCurrentScope()); |
||
269 | array_pop($this->state); |
||
270 | } else { |
||
271 | $this->raiseError('Unexpected \'}\''); |
||
272 | } |
||
273 | } elseif (ctype_alnum($c) || $c === '\\') { |
||
274 | $elements = ['']; |
||
275 | $elTypes = [null]; |
||
276 | $i = 0; |
||
277 | $tokenType = 0; |
||
278 | $newLineDetected = null; |
||
279 | |||
280 | for (; $this->p < $this->length; ++$this->p) { |
||
281 | $prePoint = [$this->line, $this->col - 1]; |
||
282 | $c = $this->getCurrentChar(); |
||
283 | |||
284 | if (ctype_space($c) || $c === '=' || $c === ',') { |
||
285 | if ($c === "\n") { |
||
286 | $newLineDetected = $prePoint; |
||
287 | } |
||
288 | if ($elTypes[$i] !== null) { |
||
289 | ++$i; |
||
290 | $elTypes[$i] = null; |
||
291 | } |
||
292 | } elseif ($c === '\'') { |
||
293 | if ($elTypes[$i] !== null) { |
||
294 | $this->raiseError('Unexpected T_STRING.'); |
||
295 | } |
||
296 | |||
297 | $string = $this->token(static::T_STRING, $c); |
||
298 | --$this->p; |
||
299 | |||
300 | if ($elTypes[$i] === null) { |
||
301 | $elements[$i] = $string; |
||
302 | $elTypes[$i] = static::T_STRING; |
||
303 | } |
||
304 | } elseif ($c === '"') { |
||
305 | if ($elTypes[$i] !== null) { |
||
306 | $this->raiseError('Unexpected T_STRING_DOUBLE.'); |
||
307 | } |
||
308 | |||
309 | $string = $this->token(static::T_STRING_DOUBLE, $c); |
||
310 | --$this->p; |
||
311 | |||
312 | if ($elTypes[$i] === null) { |
||
313 | $elements[$i] = $string; |
||
314 | $elTypes[$i] = static::T_STRING_DOUBLE; |
||
315 | } |
||
316 | } elseif ($c === '}') { |
||
317 | $this->raiseError('Unexpected \'}\' instead of \';\' or \'{\''); |
||
318 | } elseif ($c === ';') { |
||
319 | if ($newLineDetected) { |
||
320 | $this->raiseError('Unexpected new-line instead of \';\'', 'notice', $newLineDetected[0], $newLineDetected[1]); |
||
321 | } |
||
322 | $tokenType = static::T_VAR; |
||
323 | break; |
||
324 | } elseif ($c === '{') { |
||
325 | $tokenType = static::T_BLOCK; |
||
326 | break; |
||
327 | } else { |
||
328 | if ($elTypes[$i] === static::T_STRING) { |
||
329 | $this->raiseError('Unexpected T_CVALUE.'); |
||
330 | } else { |
||
331 | if (!isset($elements[$i])) { |
||
332 | $elements[$i] = ''; |
||
333 | } |
||
334 | |||
335 | $elements[$i] .= $c; |
||
336 | $elTypes[$i] = static::T_CVALUE; |
||
337 | } |
||
338 | } |
||
339 | } |
||
340 | foreach ($elTypes as $k => $v) { |
||
341 | if (static::T_CVALUE === $v) { |
||
342 | if (ctype_digit($elements[$k])) { |
||
343 | $elements[$k] = (int)$elements[$k]; |
||
344 | } elseif (is_numeric($elements[$k])) { |
||
345 | $elements[$k] = (float)$elements[$k]; |
||
346 | } else { |
||
347 | $l = strtolower($elements[$k]); |
||
348 | |||
349 | if (($l === 'true') || ($l === 'on')) { |
||
350 | $elements[$k] = true; |
||
351 | } elseif (($l === 'false') || ($l === 'off')) { |
||
352 | $elements[$k] = false; |
||
353 | } elseif ($l === 'null') { |
||
354 | $elements[$k] = null; |
||
355 | } |
||
356 | } |
||
357 | } |
||
358 | } |
||
359 | if ($tokenType === 0) { |
||
360 | $this->raiseError('Expected \';\' or \'{\''); |
||
361 | } elseif ($tokenType === static::T_VAR) { |
||
362 | $name = str_replace('-', '', strtolower($elements[0])); |
||
363 | if (sizeof($elements) > 2) { |
||
364 | $value = array_slice($elements, 1); |
||
365 | } else { |
||
366 | $value = isset($elements[1]) ? $elements[1] : null; |
||
367 | } |
||
368 | $scope = $this->getCurrentScope(); |
||
369 | |||
370 | if ($name === 'include') { |
||
371 | if (!is_array($value)) { |
||
372 | $value = [$value]; |
||
373 | } |
||
374 | foreach ($value as $path) { |
||
375 | if (substr($path, 0, 1) !== '/') { |
||
376 | $path = 'conf/' . $path; |
||
377 | } |
||
378 | $files = glob($path); |
||
379 | if ($files) { |
||
380 | foreach ($files as $fn) { |
||
381 | try { |
||
382 | static::parse($fn, $scope, true); |
||
383 | } catch (InfiniteRecursion $e) { |
||
384 | $this->raiseError('Cannot include \'' . $fn . '\' as a part of itself, it may cause an infinite recursion.'); |
||
385 | } |
||
386 | } |
||
387 | } |
||
388 | } |
||
389 | } else { |
||
390 | if (sizeof($elements) === 1) { |
||
391 | $value = true; |
||
392 | $elements[1] = true; |
||
393 | $elTypes[1] = static::T_CVALUE; |
||
394 | } elseif ($value === null) { |
||
395 | $value = null; |
||
396 | $elements[1] = null; |
||
397 | $elTypes[1] = static::T_CVALUE; |
||
398 | } |
||
399 | |||
400 | if (isset($scope->{$name})) { |
||
401 | if ($scope->{$name}->source !== 'cmdline') { |
||
402 | if (($elTypes[1] === static::T_CVALUE) && is_string($value)) { |
||
403 | $scope->{$name}->pushHumanValue($value); |
||
404 | } else { |
||
405 | $scope->{$name}->pushValue($value); |
||
406 | } |
||
407 | $scope->{$name}->source = 'config'; |
||
408 | $scope->{$name}->revision = $this->revision; |
||
409 | } |
||
410 | } elseif ($scope instanceof Section) { |
||
411 | $scope->{$name} = new Generic(); |
||
412 | $scope->{$name}->source = 'config'; |
||
413 | $scope->{$name}->revision = $this->revision; |
||
414 | $scope->{$name}->pushValue($value); |
||
415 | $scope->{$name}->setValueType($value); |
||
416 | } else { |
||
417 | $this->raiseError('Unrecognized parameter \'' . $name . '\''); |
||
418 | } |
||
419 | } |
||
420 | } elseif ($tokenType === static::T_BLOCK) { |
||
421 | $scope = $this->getCurrentScope(); |
||
422 | $sectionName = implode('-', $elements); |
||
423 | $sectionName = strtr($sectionName, '-. ', ':::'); |
||
424 | if (!isset($scope->{$sectionName})) { |
||
425 | $scope->{$sectionName} = new Section; |
||
426 | } |
||
427 | $scope->{$sectionName}->source = 'config'; |
||
428 | $scope->{$sectionName}->revision = $this->revision; |
||
429 | $this->state[] = [ |
||
430 | static::T_ALL, |
||
431 | $scope->{$sectionName}, |
||
432 | ]; |
||
433 | } |
||
434 | } else { |
||
435 | $this->raiseError('Unexpected char \'' . Debug::exportBytes($c) . '\''); |
||
436 | } |
||
437 | } |
||
438 | ]; |
||
439 | |||
440 | for (; $this->p < $this->length; ++$this->p) { |
||
441 | $c = $this->getCurrentChar(); |
||
442 | $e = end($this->state); |
||
443 | $this->token($e[0], $c); |
||
444 | } |
||
445 | if (!$included) { |
||
446 | $this->purgeScope($this->target); |
||
447 | } |
||
448 | |||
449 | if (Daemon::$config->verbosetty->value) { |
||
450 | Daemon::log('Loaded config file: '. escapeshellarg($file)); |
||
451 | } |
||
452 | } |
||
453 | |||
559 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.