| Conditions | 37 |
| Paths | > 20000 |
| Total Lines | 180 |
| Code Lines | 106 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 393 | protected function calculateStatistics() |
||
| 394 | { |
||
| 395 | $classStack = $functionStack = []; |
||
| 396 | |||
| 397 | if ($this->cacheTokens) { |
||
| 398 | $tokens = \PHP_Token_Stream_CachingFactory::get($this->getPath()); |
||
| 399 | } else { |
||
| 400 | $tokens = new \PHP_Token_Stream($this->getPath()); |
||
| 401 | } |
||
| 402 | |||
| 403 | $this->processClasses($tokens); |
||
| 404 | $this->processTraits($tokens); |
||
| 405 | $this->processFunctions($tokens); |
||
| 406 | $this->linesOfCode = $tokens->getLinesOfCode(); |
||
| 407 | unset($tokens); |
||
| 408 | |||
| 409 | for ($lineNumber = 1; $lineNumber <= $this->linesOfCode['loc']; $lineNumber++) { |
||
| 410 | if (isset($this->startLines[$lineNumber])) { |
||
| 411 | // Start line of a class. |
||
| 412 | if (isset($this->startLines[$lineNumber]['className'])) { |
||
| 413 | if (isset($currentClass)) { |
||
| 414 | $classStack[] = &$currentClass; |
||
| 415 | } |
||
| 416 | |||
| 417 | $currentClass = &$this->startLines[$lineNumber]; |
||
| 418 | } // Start line of a trait. |
||
| 419 | elseif (isset($this->startLines[$lineNumber]['traitName'])) { |
||
| 420 | $currentTrait = &$this->startLines[$lineNumber]; |
||
| 421 | } // Start line of a method. |
||
| 422 | elseif (isset($this->startLines[$lineNumber]['methodName'])) { |
||
| 423 | $currentMethod = &$this->startLines[$lineNumber]; |
||
| 424 | } // Start line of a function. |
||
| 425 | elseif (isset($this->startLines[$lineNumber]['functionName'])) { |
||
| 426 | if (isset($currentFunction)) { |
||
| 427 | $functionStack[] = &$currentFunction; |
||
| 428 | } |
||
| 429 | |||
| 430 | $currentFunction = &$this->startLines[$lineNumber]; |
||
| 431 | } |
||
| 432 | } |
||
| 433 | |||
| 434 | if (isset($this->coverageData[$lineNumber])) { |
||
| 435 | if (isset($currentClass)) { |
||
| 436 | $currentClass['executableLines']++; |
||
| 437 | } |
||
| 438 | |||
| 439 | if (isset($currentTrait)) { |
||
| 440 | $currentTrait['executableLines']++; |
||
| 441 | } |
||
| 442 | |||
| 443 | if (isset($currentMethod)) { |
||
| 444 | $currentMethod['executableLines']++; |
||
| 445 | } |
||
| 446 | |||
| 447 | if (isset($currentFunction)) { |
||
| 448 | $currentFunction['executableLines']++; |
||
| 449 | } |
||
| 450 | |||
| 451 | $this->numExecutableLines++; |
||
| 452 | |||
| 453 | if (count($this->coverageData[$lineNumber]) > 0) { |
||
| 454 | if (isset($currentClass)) { |
||
| 455 | $currentClass['executedLines']++; |
||
| 456 | } |
||
| 457 | |||
| 458 | if (isset($currentTrait)) { |
||
| 459 | $currentTrait['executedLines']++; |
||
| 460 | } |
||
| 461 | |||
| 462 | if (isset($currentMethod)) { |
||
| 463 | $currentMethod['executedLines']++; |
||
| 464 | } |
||
| 465 | |||
| 466 | if (isset($currentFunction)) { |
||
| 467 | $currentFunction['executedLines']++; |
||
| 468 | } |
||
| 469 | |||
| 470 | $this->numExecutedLines++; |
||
| 471 | } |
||
| 472 | } |
||
| 473 | |||
| 474 | if (isset($this->endLines[$lineNumber])) { |
||
| 475 | // End line of a class. |
||
| 476 | if (isset($this->endLines[$lineNumber]['className'])) { |
||
| 477 | unset($currentClass); |
||
| 478 | |||
| 479 | if ($classStack) { |
||
|
|
|||
| 480 | end($classStack); |
||
| 481 | $key = key($classStack); |
||
| 482 | $currentClass = &$classStack[$key]; |
||
| 483 | unset($classStack[$key]); |
||
| 484 | } |
||
| 485 | } // End line of a trait. |
||
| 486 | elseif (isset($this->endLines[$lineNumber]['traitName'])) { |
||
| 487 | unset($currentTrait); |
||
| 488 | } // End line of a method. |
||
| 489 | elseif (isset($this->endLines[$lineNumber]['methodName'])) { |
||
| 490 | unset($currentMethod); |
||
| 491 | } // End line of a function. |
||
| 492 | elseif (isset($this->endLines[$lineNumber]['functionName'])) { |
||
| 493 | unset($currentFunction); |
||
| 494 | |||
| 495 | if ($functionStack) { |
||
| 496 | end($functionStack); |
||
| 497 | $key = key($functionStack); |
||
| 498 | $currentFunction = &$functionStack[$key]; |
||
| 499 | unset($functionStack[$key]); |
||
| 500 | } |
||
| 501 | } |
||
| 502 | } |
||
| 503 | } |
||
| 504 | |||
| 505 | foreach ($this->traits as &$trait) { |
||
| 506 | foreach ($trait['methods'] as &$method) { |
||
| 507 | if ($method['executableLines'] > 0) { |
||
| 508 | $method['coverage'] = ($method['executedLines'] / |
||
| 509 | $method['executableLines']) * 100; |
||
| 510 | } else { |
||
| 511 | $method['coverage'] = 100; |
||
| 512 | } |
||
| 513 | |||
| 514 | $method['crap'] = $this->crap( |
||
| 515 | $method['ccn'], |
||
| 516 | $method['coverage'] |
||
| 517 | ); |
||
| 518 | |||
| 519 | $trait['ccn'] += $method['ccn']; |
||
| 520 | } |
||
| 521 | |||
| 522 | if ($trait['executableLines'] > 0) { |
||
| 523 | $trait['coverage'] = ($trait['executedLines'] / |
||
| 524 | $trait['executableLines']) * 100; |
||
| 525 | |||
| 526 | if ($trait['coverage'] == 100) { |
||
| 527 | $this->numTestedClasses++; |
||
| 528 | } |
||
| 529 | } else { |
||
| 530 | $trait['coverage'] = 100; |
||
| 531 | } |
||
| 532 | |||
| 533 | $trait['crap'] = $this->crap( |
||
| 534 | $trait['ccn'], |
||
| 535 | $trait['coverage'] |
||
| 536 | ); |
||
| 537 | } |
||
| 538 | |||
| 539 | foreach ($this->classes as &$class) { |
||
| 540 | foreach ($class['methods'] as &$method) { |
||
| 541 | if ($method['executableLines'] > 0) { |
||
| 542 | $method['coverage'] = ($method['executedLines'] / |
||
| 543 | $method['executableLines']) * 100; |
||
| 544 | } else { |
||
| 545 | $method['coverage'] = 100; |
||
| 546 | } |
||
| 547 | |||
| 548 | $method['crap'] = $this->crap( |
||
| 549 | $method['ccn'], |
||
| 550 | $method['coverage'] |
||
| 551 | ); |
||
| 552 | |||
| 553 | $class['ccn'] += $method['ccn']; |
||
| 554 | } |
||
| 555 | |||
| 556 | if ($class['executableLines'] > 0) { |
||
| 557 | $class['coverage'] = ($class['executedLines'] / |
||
| 558 | $class['executableLines']) * 100; |
||
| 559 | |||
| 560 | if ($class['coverage'] == 100) { |
||
| 561 | $this->numTestedClasses++; |
||
| 562 | } |
||
| 563 | } else { |
||
| 564 | $class['coverage'] = 100; |
||
| 565 | } |
||
| 566 | |||
| 567 | $class['crap'] = $this->crap( |
||
| 568 | $class['ccn'], |
||
| 569 | $class['coverage'] |
||
| 570 | ); |
||
| 571 | } |
||
| 572 | } |
||
| 573 | |||
| 723 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.