Conditions | 13 |
Paths | 105 |
Total Lines | 42 |
Code Lines | 27 |
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 |
||
18 | public static function format(string $string): string |
||
19 | { |
||
20 | $tabs = 0; |
||
21 | $startCounter = 0; |
||
22 | $result = ''; |
||
23 | $use = true; |
||
24 | foreach (preg_split("/((\r?\n)|(\r\n?))/", $string) as $line) { |
||
25 | $line = str_replace(' ', '', $line); |
||
26 | if (strpos($line, '}') !== false) { |
||
27 | $tabs--; |
||
28 | } |
||
29 | $indent = ''; |
||
30 | for ($i = 0; $i < $tabs; $i++) { |
||
31 | $indent .= ' '; |
||
32 | } |
||
33 | if ($line !== '') { |
||
34 | if (substr($line, 0, 3) === '/**') { |
||
35 | if ($startCounter >= 1) { |
||
36 | $result .= PHP_EOL; |
||
37 | } |
||
38 | $startCounter++; |
||
39 | } |
||
40 | |||
41 | if (substr($line, 0, 3) === 'use' && $use) { |
||
42 | $result .= PHP_EOL; |
||
43 | $use = false; |
||
44 | } |
||
45 | |||
46 | if (substr($line, 0, 6) === 'public' || |
||
47 | substr($line, 0, 9) === 'namespace' || |
||
48 | substr($line, 0, 5) === 'class' |
||
49 | ) { |
||
50 | $result .= PHP_EOL; |
||
51 | } |
||
52 | $result .= $indent . $line . PHP_EOL; |
||
53 | } |
||
54 | if (strpos($line, '{') !== false) { |
||
55 | $tabs++; |
||
56 | } |
||
57 | } |
||
58 | |||
59 | return $result; |
||
60 | } |
||
62 |