Conditions | 12 |
Paths | 26 |
Total Lines | 66 |
Code Lines | 29 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 156 |
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 |
||
29 | public function toString(array $config, $header = null) |
||
30 | { |
||
31 | $ini = !empty($header) ? $header . PHP_EOL : ''; |
||
32 | |||
33 | uasort($config, function ($first, $second) { |
||
34 | if (is_array($first)) { |
||
35 | return 1; |
||
36 | } |
||
37 | |||
38 | if (is_array($second)) |
||
39 | { |
||
40 | return -1; |
||
41 | } |
||
42 | |||
43 | return 0; |
||
44 | }); |
||
45 | |||
46 | $names = array_keys($config); |
||
47 | |||
48 | foreach ($names as $name) |
||
49 | { |
||
50 | $section = $config[$name]; |
||
51 | |||
52 | if (!is_array($section)) |
||
53 | { |
||
54 | $ini .= $name . ' = ' . $this->encodeValue($section) . PHP_EOL; |
||
55 | continue; |
||
56 | } |
||
57 | |||
58 | if (empty($section)) |
||
59 | { |
||
60 | continue; |
||
61 | } |
||
62 | |||
63 | if (!empty($ini)) |
||
64 | { |
||
65 | $ini .= PHP_EOL; |
||
66 | } |
||
67 | |||
68 | $ini .= "[$name]" . PHP_EOL; |
||
69 | |||
70 | foreach ($section as $option => $value) |
||
71 | { |
||
72 | if (is_numeric($option)) |
||
73 | { |
||
74 | $option = $name; |
||
75 | $value = (array)$value; |
||
76 | } |
||
77 | |||
78 | if (is_array($value)) |
||
79 | { |
||
80 | foreach ($value as $key => $currentValue) |
||
81 | { |
||
82 | $ini .= $option . '[' . $key . '] = ' . $this->encodeValue($currentValue) . PHP_EOL; |
||
83 | } |
||
84 | } |
||
85 | else |
||
86 | { |
||
87 | $ini .= $option . ' = ' . $this->encodeValue($value) . PHP_EOL; |
||
88 | } |
||
89 | } |
||
90 | |||
91 | $ini .= "\n"; |
||
92 | } |
||
93 | |||
94 | return $ini; |
||
95 | } |
||
118 |