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