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