| Conditions | 13 |
| Paths | 13 |
| Total Lines | 67 |
| Code Lines | 51 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 20 | public function parse(string $source): void |
||
| 21 | { |
||
| 22 | $comment = ''; |
||
| 23 | $params = ''; |
||
| 24 | $signature = ''; |
||
| 25 | |||
| 26 | $source = str_replace("\r\n", "\n", $source); |
||
| 27 | |||
| 28 | $state = 'root'; |
||
| 29 | foreach (explode("\n", $source) as $lineNumber => $line) { |
||
| 30 | switch ($state) { |
||
| 31 | case 'root': |
||
| 32 | if (str_starts_with($line, '/**')) { |
||
| 33 | $state = 'comment'; |
||
| 34 | break; |
||
| 35 | } |
||
| 36 | if (str_starts_with($line, 'function')) { |
||
| 37 | $signature = preg_replace('/^function\s+/', '', $line); |
||
| 38 | $funcName = preg_replace('/\(.*$/', '', $signature); |
||
| 39 | $this->fns[] = [ |
||
| 40 | 'comment' => $comment, |
||
| 41 | 'params' => $params, |
||
| 42 | 'funcName' => $funcName, |
||
| 43 | 'signature' => $signature, |
||
| 44 | ]; |
||
| 45 | $comment = ''; |
||
| 46 | $params = ''; |
||
| 47 | |||
| 48 | if (str_ends_with($signature, '(')) { |
||
| 49 | $state = 'params'; |
||
| 50 | } else { |
||
| 51 | $signature = ''; |
||
| 52 | } |
||
| 53 | } |
||
| 54 | break; |
||
| 55 | |||
| 56 | case 'comment': |
||
| 57 | if (str_ends_with($line, '*/')) { |
||
| 58 | $state = 'root'; |
||
| 59 | break; |
||
| 60 | } |
||
| 61 | if (preg_match('/^\s\*\s@param\s(?<type>.+?)\$(?<name>.+?)\s(?<comment>.+)$/', $line, $matches)) { |
||
| 62 | if (empty($params)) { |
||
| 63 | $params = "| Argument | Type | Comment |\n|---|---|---|\n"; |
||
| 64 | } |
||
| 65 | $type = implode(' or ', array_map(function ($t) { |
||
| 66 | $t = trim($t, ' '); |
||
| 67 | return "`$t`"; |
||
| 68 | }, explode('|', $matches['type']))); |
||
| 69 | $params .= "| `\${$matches['name']}` | $type | {$matches['comment']} |\n"; |
||
| 70 | break; |
||
| 71 | } |
||
| 72 | if (str_starts_with($line, ' * @')) { |
||
| 73 | break; |
||
| 74 | } |
||
| 75 | $comment .= preg_replace('/^\s\*\s?/', '', $line) . "\n"; |
||
| 76 | break; |
||
| 77 | |||
| 78 | case 'params': |
||
| 79 | if (preg_match('/^\).+\{$/', $line, $matches)) { |
||
| 80 | $signature .= "\n" . preg_replace('/\{$/', '', $line); |
||
| 81 | $this->fns[count($this->fns) - 1]['signature'] = $signature; |
||
| 82 | $state = 'root'; |
||
| 83 | } else { |
||
| 84 | $signature .= "\n" . $line; |
||
| 85 | } |
||
| 86 | break; |
||
| 87 | } |
||
| 130 |