| Conditions | 11 |
| Paths | 21 |
| Total Lines | 60 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 132 |
| 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 |
||
| 13 | public function canonicalize(string $string): string |
||
| 14 | { |
||
| 15 | $length = strlen($string); |
||
| 16 | $canon = ''; |
||
| 17 | $lastChar = null; |
||
| 18 | $space = false; |
||
| 19 | $line = ''; |
||
| 20 | $emptyCounter = 0; |
||
| 21 | |||
| 22 | for ($i = 0; $i < $length; ++$i) { |
||
| 23 | switch ($string[$i]) { |
||
| 24 | case "\r": |
||
| 25 | $lastChar = "\r"; |
||
| 26 | break; |
||
| 27 | |||
| 28 | case "\n": |
||
| 29 | if ($lastChar === "\r") { |
||
| 30 | $space = false; |
||
| 31 | |||
| 32 | if ($line === '') { |
||
| 33 | ++$emptyCounter; |
||
| 34 | } else { |
||
| 35 | $line = ''; |
||
| 36 | $canon .= "\r\n"; |
||
| 37 | } |
||
| 38 | } else { |
||
| 39 | throw new \RuntimeException('This should never happen'); |
||
| 40 | } |
||
| 41 | |||
| 42 | break; |
||
| 43 | |||
| 44 | case ' ': |
||
| 45 | case "\t": |
||
| 46 | $space = true; |
||
| 47 | break; |
||
| 48 | |||
| 49 | default: |
||
| 50 | if ($emptyCounter > 0) { |
||
| 51 | $canon .= str_repeat("\r\n", $emptyCounter); |
||
| 52 | $emptyCounter = 0; |
||
| 53 | } |
||
| 54 | |||
| 55 | if ($space) { |
||
| 56 | $line .= ' '; |
||
| 57 | $canon .= ' '; |
||
| 58 | $space = false; |
||
| 59 | } |
||
| 60 | |||
| 61 | $line .= $string[$i]; |
||
| 62 | $canon .= $string[$i]; |
||
| 63 | break; |
||
| 64 | } |
||
| 65 | } |
||
| 66 | |||
| 67 | if ($line === '') { |
||
| 68 | $canon .= "\r\n"; |
||
| 69 | } |
||
| 70 | |||
| 71 | return $canon; |
||
| 72 | } |
||
| 73 | } |