| Conditions | 17 |
| Paths | 29 |
| Total Lines | 76 |
| Lines | 8 |
| Ratio | 10.53 % |
| 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 |
||
| 35 | public function leaveNode(Node $node) |
||
| 36 | { |
||
| 37 | if ($node instanceof Stmt\Class_ || $node instanceof Stmt\Function_ || $node instanceof Stmt\Trait_) { |
||
| 38 | View Code Duplication | if ($node instanceof Stmt\Class_ || $node instanceof Stmt\Trait_) { |
|
|
|
|||
| 39 | $name = (string)(isset($node->namespacedName) ? $node->namespacedName : 'anonymous@' . spl_object_hash($node)); |
||
| 40 | $classOrFunction = $this->metrics->get($name); |
||
| 41 | } else { |
||
| 42 | $name = (string)$node->name; |
||
| 43 | $classOrFunction = new FunctionMetric($name); |
||
| 44 | $this->metrics->attach($classOrFunction); |
||
| 45 | } |
||
| 46 | |||
| 47 | if ($classOrFunction === null) { |
||
| 48 | throw new MetricNullException($name, self::class); |
||
| 49 | } |
||
| 50 | |||
| 51 | $prettyPrinter = new PrettyPrinter\Standard(); |
||
| 52 | $code = $prettyPrinter->prettyPrintFile([$node]); |
||
| 53 | |||
| 54 | // count all lines |
||
| 55 | $allLines = preg_split('/\r\n|\r|\n/', $code); |
||
| 56 | if ($allLines === false) { |
||
| 57 | throw new ShouldNotHappenException('Count all lines return false'); |
||
| 58 | } |
||
| 59 | $loc = count($allLines) - 1; |
||
| 60 | |||
| 61 | // count and remove multi lines comments |
||
| 62 | $cloc = 0; |
||
| 63 | if (preg_match_all('!/\*.*?\*/!s', $code, $matches)) { |
||
| 64 | foreach ($matches[0] as $match) { |
||
| 65 | $matched = preg_split('/\r\n|\r|\n/', $match); |
||
| 66 | if ($matched === false) { |
||
| 67 | throw new ShouldNotHappenException('Count and remove multi lines comments return false'); |
||
| 68 | } |
||
| 69 | $cloc += max(1, count($matched)); |
||
| 70 | } |
||
| 71 | } |
||
| 72 | $code = preg_replace('!/\*.*?\*/!s', '', $code); |
||
| 73 | if ($code === null) { |
||
| 74 | throw new ShouldNotHappenException('Preg replace return null'); |
||
| 75 | } |
||
| 76 | |||
| 77 | // count and remove single line comments |
||
| 78 | $code = preg_replace_callback('!(\'[^\']*\'|"[^"]*")|((?:#|\/\/).*$)!m', function (array $matches) use (&$cloc) { |
||
| 79 | if (isset($matches[2])) { |
||
| 80 | $cloc += 1; |
||
| 81 | } |
||
| 82 | return $matches[1]; |
||
| 83 | }, $code, -1); |
||
| 84 | if ($code === null) { |
||
| 85 | throw new ShouldNotHappenException('Count and remove single line comments return null'); |
||
| 86 | } |
||
| 87 | |||
| 88 | // count and remove empty lines |
||
| 89 | $emptyLines = preg_replace('!(^\s*[\r\n])!sm', '', $code); |
||
| 90 | if ($emptyLines === null) { |
||
| 91 | throw new ShouldNotHappenException('Remove empty lines return null'); |
||
| 92 | } |
||
| 93 | $code = trim($emptyLines); |
||
| 94 | $countCode = preg_split('/\r\n|\r|\n/', $code); |
||
| 95 | if ($countCode === false) { |
||
| 96 | throw new ShouldNotHappenException('Count empty lines return false'); |
||
| 97 | } |
||
| 98 | |||
| 99 | $lloc = count($countCode); |
||
| 100 | |||
| 101 | // save result |
||
| 102 | $classOrFunction |
||
| 103 | ->set('cloc', $cloc) |
||
| 104 | ->set('loc', $loc) |
||
| 105 | ->set('lloc', $lloc); |
||
| 106 | $this->metrics->attach($classOrFunction); |
||
| 107 | } |
||
| 108 | |||
| 109 | return null; |
||
| 110 | } |
||
| 111 | } |
||
| 112 |
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.