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