Conditions | 12 |
Paths | 32 |
Total Lines | 42 |
Code Lines | 21 |
Lines | 0 |
Ratio | 0 % |
Tests | 22 |
CRAP Score | 12 |
Changes | 1 | ||
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 |
||
24 | 24 | public static function value($rawString) |
|
25 | { |
||
26 | // Expand a block string's raw value into independent lines. |
||
27 | 24 | $lines = preg_split("/\\r\\n|[\\n\\r]/", $rawString); |
|
28 | |||
29 | // Remove common indentation from all lines but first. |
||
30 | 24 | $commonIndent = null; |
|
31 | 24 | $linesLength = count($lines); |
|
|
|||
32 | |||
33 | 24 | for ($i = 1; $i < $linesLength; $i++) { |
|
34 | 21 | $line = $lines[$i]; |
|
35 | 21 | $indent = self::leadingWhitespace($line); |
|
36 | |||
37 | 21 | if ($indent >= mb_strlen($line) || |
|
38 | 21 | ($commonIndent !== null && $indent >= $commonIndent) |
|
39 | ) { |
||
40 | 20 | continue; |
|
41 | } |
||
42 | |||
43 | 18 | $commonIndent = $indent; |
|
44 | 18 | if ($commonIndent === 0) { |
|
45 | 4 | break; |
|
46 | } |
||
47 | } |
||
48 | |||
49 | 24 | if ($commonIndent) { |
|
50 | 17 | for ($i = 1; $i < $linesLength; $i++) { |
|
51 | 17 | $line = $lines[$i]; |
|
52 | 17 | $lines[$i] = mb_substr($line, $commonIndent); |
|
53 | } |
||
54 | } |
||
55 | |||
56 | // Remove leading and trailing blank lines. |
||
57 | 24 | while (count($lines) > 0 && trim($lines[0], " \t") === '') { |
|
58 | 18 | array_shift($lines); |
|
59 | } |
||
60 | 24 | while (count($lines) > 0 && trim($lines[count($lines) - 1], " \t") === '') { |
|
61 | 21 | array_pop($lines); |
|
62 | } |
||
63 | |||
64 | // Return a string of the lines joined with U+000A. |
||
65 | 24 | return implode("\n", $lines); |
|
66 | } |
||
78 |