| Conditions | 10 |
| Paths | 19 |
| Total Lines | 55 |
| Code Lines | 35 |
| Lines | 6 |
| Ratio | 10.91 % |
| 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 |
||
| 48 | public function leaveNode(Node $node) |
||
| 49 | { |
||
| 50 | if ($node instanceof Stmt\Class_) { |
||
| 51 | |||
| 52 | View Code Duplication | if ($node instanceof Stmt\Class_) { |
|
|
|
|||
| 53 | $classOrFunction = $this->metrics->get($node->namespacedName->toString()); |
||
| 54 | } else { |
||
| 55 | $classOrFunction = new FunctionMetric($node->name); |
||
| 56 | $this->metrics->attach($classOrFunction); |
||
| 57 | } |
||
| 58 | if (null === $lloc = $classOrFunction->get('lloc')) { |
||
| 59 | throw new \LogicException('please enable length (lloc) visitor first'); |
||
| 60 | } |
||
| 61 | if (null === $cloc = $classOrFunction->get('cloc')) { |
||
| 62 | throw new \LogicException('please enable length (cloc) visitor first'); |
||
| 63 | } |
||
| 64 | if (null === $loc = $classOrFunction->get('loc')) { |
||
| 65 | throw new \LogicException('please enable length (loc) visitor first'); |
||
| 66 | } |
||
| 67 | if (null === $ccn = $classOrFunction->get('ccn')) { |
||
| 68 | throw new \LogicException('please enable McCabe visitor first'); |
||
| 69 | } |
||
| 70 | if (null === $volume = $classOrFunction->get('volume')) { |
||
| 71 | throw new \LogicException('please enable Halstead visitor first'); |
||
| 72 | } |
||
| 73 | |||
| 74 | // maintainability index without comment |
||
| 75 | $MIwoC = max( |
||
| 76 | (171 |
||
| 77 | - (5.2 * \log($volume)) |
||
| 78 | - (0.23 * $ccn) |
||
| 79 | - (16.2 * \log($lloc)) |
||
| 80 | ) * 100 / 171 |
||
| 81 | , 0); |
||
| 82 | if (is_infinite($MIwoC)) { |
||
| 83 | $MIwoC = 171; |
||
| 84 | } |
||
| 85 | |||
| 86 | // comment weight |
||
| 87 | if ($loc > 0) { |
||
| 88 | $CM = $cloc / $loc; |
||
| 89 | $commentWeight = 50 * sin(sqrt(2.4 * $CM)); |
||
| 90 | } |
||
| 91 | |||
| 92 | // maintainability index |
||
| 93 | $mi = $MIwoC + $commentWeight; |
||
| 94 | |||
| 95 | // save result |
||
| 96 | $classOrFunction |
||
| 97 | ->set('mi', round($mi, 2)) |
||
| 98 | ->set('mIwoC', round($MIwoC, 2)) |
||
| 99 | ->set('commentWeight', round($commentWeight, 2)); |
||
| 100 | $this->metrics->attach($classOrFunction); |
||
| 101 | } |
||
| 102 | } |
||
| 103 | } |
||
| 104 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.