| Conditions | 42 |
| Paths | 490 |
| Total Lines | 182 |
| Code Lines | 115 |
| Lines | 47 |
| Ratio | 25.82 % |
| Changes | 1 | ||
| Bugs | 0 | 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 |
||
| 360 | public static function getMethodParameters(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 361 | { |
||
| 362 | if (version_compare(self::getVersion(), '3.3.0', '>=') === true) { |
||
| 363 | return $phpcsFile->getMethodParameters($stackPtr); |
||
| 364 | } |
||
| 365 | |||
| 366 | $tokens = $phpcsFile->getTokens(); |
||
| 367 | |||
| 368 | // Check for the existence of the token. |
||
| 369 | if (isset($tokens[$stackPtr]) === false) { |
||
| 370 | return false; |
||
| 371 | } |
||
| 372 | |||
| 373 | View Code Duplication | if ($tokens[$stackPtr]['code'] !== T_FUNCTION |
|
| 374 | && $tokens[$stackPtr]['code'] !== T_CLOSURE |
||
| 375 | ) { |
||
| 376 | throw new \PHP_CodeSniffer_Exception('$stackPtr must be of type T_FUNCTION or T_CLOSURE'); |
||
| 377 | } |
||
| 378 | |||
| 379 | $opener = $tokens[$stackPtr]['parenthesis_opener']; |
||
| 380 | $closer = $tokens[$stackPtr]['parenthesis_closer']; |
||
| 381 | |||
| 382 | $vars = array(); |
||
| 383 | $currVar = null; |
||
| 384 | $paramStart = ($opener + 1); |
||
| 385 | $defaultStart = null; |
||
| 386 | $paramCount = 0; |
||
| 387 | $passByReference = false; |
||
| 388 | $variableLength = false; |
||
| 389 | $typeHint = ''; |
||
| 390 | $typeHintToken = false; |
||
| 391 | $nullableType = false; |
||
| 392 | |||
| 393 | for ($i = $paramStart; $i <= $closer; $i++) { |
||
| 394 | // Check to see if this token has a parenthesis or bracket opener. If it does |
||
| 395 | // it's likely to be an array which might have arguments in it. This |
||
| 396 | // could cause problems in our parsing below, so lets just skip to the |
||
| 397 | // end of it. |
||
| 398 | View Code Duplication | if (isset($tokens[$i]['parenthesis_opener']) === true) { |
|
| 399 | // Don't do this if it's the close parenthesis for the method. |
||
| 400 | if ($i !== $tokens[$i]['parenthesis_closer']) { |
||
| 401 | $i = ($tokens[$i]['parenthesis_closer'] + 1); |
||
| 402 | } |
||
| 403 | } |
||
| 404 | |||
| 405 | View Code Duplication | if (isset($tokens[$i]['bracket_opener']) === true) { |
|
| 406 | // Don't do this if it's the close parenthesis for the method. |
||
| 407 | if ($i !== $tokens[$i]['bracket_closer']) { |
||
| 408 | $i = ($tokens[$i]['bracket_closer'] + 1); |
||
| 409 | } |
||
| 410 | } |
||
| 411 | |||
| 412 | switch ($tokens[$i]['type']) { |
||
| 413 | case 'T_BITWISE_AND': |
||
| 414 | if ($defaultStart === null) { |
||
| 415 | $passByReference = true; |
||
| 416 | } |
||
| 417 | break; |
||
| 418 | case 'T_VARIABLE': |
||
| 419 | $currVar = $i; |
||
| 420 | break; |
||
| 421 | case 'T_ELLIPSIS': |
||
| 422 | $variableLength = true; |
||
| 423 | break; |
||
| 424 | case 'T_ARRAY_HINT': // Pre-PHPCS 3.3.0. |
||
| 425 | case 'T_CALLABLE': |
||
| 426 | if ($typeHintToken === false) { |
||
| 427 | $typeHintToken = $i; |
||
| 428 | } |
||
| 429 | |||
| 430 | $typeHint .= $tokens[$i]['content']; |
||
| 431 | break; |
||
| 432 | case 'T_SELF': |
||
| 433 | case 'T_PARENT': |
||
| 434 | View Code Duplication | case 'T_STATIC': |
|
| 435 | // Self and parent are valid, static invalid, but was probably intended as type hint. |
||
| 436 | if (isset($defaultStart) === false) { |
||
| 437 | if ($typeHintToken === false) { |
||
| 438 | $typeHintToken = $i; |
||
| 439 | } |
||
| 440 | |||
| 441 | $typeHint .= $tokens[$i]['content']; |
||
| 442 | } |
||
| 443 | break; |
||
| 444 | case 'T_STRING': |
||
| 445 | // This is a string, so it may be a type hint, but it could |
||
| 446 | // also be a constant used as a default value. |
||
| 447 | $prevComma = false; |
||
| 448 | View Code Duplication | for ($t = $i; $t >= $opener; $t--) { |
|
| 449 | if ($tokens[$t]['code'] === T_COMMA) { |
||
| 450 | $prevComma = $t; |
||
| 451 | break; |
||
| 452 | } |
||
| 453 | } |
||
| 454 | |||
| 455 | if ($prevComma !== false) { |
||
| 456 | $nextEquals = false; |
||
| 457 | View Code Duplication | for ($t = $prevComma; $t < $i; $t++) { |
|
| 458 | if ($tokens[$t]['code'] === T_EQUAL) { |
||
| 459 | $nextEquals = $t; |
||
| 460 | break; |
||
| 461 | } |
||
| 462 | } |
||
| 463 | |||
| 464 | if ($nextEquals !== false) { |
||
| 465 | break; |
||
| 466 | } |
||
| 467 | } |
||
| 468 | |||
| 469 | if ($defaultStart === null) { |
||
| 470 | if ($typeHintToken === false) { |
||
| 471 | $typeHintToken = $i; |
||
| 472 | } |
||
| 473 | |||
| 474 | $typeHint .= $tokens[$i]['content']; |
||
| 475 | } |
||
| 476 | break; |
||
| 477 | View Code Duplication | case 'T_NS_SEPARATOR': |
|
| 478 | // Part of a type hint or default value. |
||
| 479 | if ($defaultStart === null) { |
||
| 480 | if ($typeHintToken === false) { |
||
| 481 | $typeHintToken = $i; |
||
| 482 | } |
||
| 483 | |||
| 484 | $typeHint .= $tokens[$i]['content']; |
||
| 485 | } |
||
| 486 | break; |
||
| 487 | case 'T_NULLABLE': |
||
| 488 | case 'T_INLINE_THEN': // Pre-PHPCS 2.8.0. |
||
| 489 | if ($defaultStart === null) { |
||
| 490 | $nullableType = true; |
||
| 491 | $typeHint .= $tokens[$i]['content']; |
||
| 492 | } |
||
| 493 | break; |
||
| 494 | case 'T_CLOSE_PARENTHESIS': |
||
| 495 | case 'T_COMMA': |
||
| 496 | // If it's null, then there must be no parameters for this |
||
| 497 | // method. |
||
| 498 | if ($currVar === null) { |
||
| 499 | continue; |
||
| 500 | } |
||
| 501 | |||
| 502 | $vars[$paramCount] = array(); |
||
| 503 | $vars[$paramCount]['token'] = $currVar; |
||
| 504 | $vars[$paramCount]['name'] = $tokens[$currVar]['content']; |
||
| 505 | $vars[$paramCount]['content'] = trim($phpcsFile->getTokensAsString($paramStart, ($i - $paramStart))); |
||
| 506 | |||
| 507 | if ($defaultStart !== null) { |
||
| 508 | $vars[$paramCount]['default'] = trim( |
||
| 509 | $phpcsFile->getTokensAsString( |
||
| 510 | $defaultStart, |
||
| 511 | ($i - $defaultStart) |
||
| 512 | ) |
||
| 513 | ); |
||
| 514 | } |
||
| 515 | |||
| 516 | $vars[$paramCount]['pass_by_reference'] = $passByReference; |
||
| 517 | $vars[$paramCount]['variable_length'] = $variableLength; |
||
| 518 | $vars[$paramCount]['type_hint'] = $typeHint; |
||
| 519 | $vars[$paramCount]['type_hint_token'] = $typeHintToken; |
||
| 520 | $vars[$paramCount]['nullable_type'] = $nullableType; |
||
| 521 | |||
| 522 | // Reset the vars, as we are about to process the next parameter. |
||
| 523 | $defaultStart = null; |
||
| 524 | $paramStart = ($i + 1); |
||
| 525 | $passByReference = false; |
||
| 526 | $variableLength = false; |
||
| 527 | $typeHint = ''; |
||
| 528 | $typeHintToken = false; |
||
| 529 | $nullableType = false; |
||
| 530 | |||
| 531 | $paramCount++; |
||
| 532 | break; |
||
| 533 | case 'T_EQUAL': |
||
| 534 | $defaultStart = ($i + 1); |
||
| 535 | break; |
||
| 536 | }//end switch |
||
| 537 | }//end for |
||
| 538 | |||
| 539 | return $vars; |
||
| 540 | |||
| 541 | }//end getMethodParameters() |
||
| 542 | } |
||
| 543 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: