Conditions | 23 |
Paths | 44 |
Total Lines | 67 |
Code Lines | 50 |
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 |
||
62 | public function writeFile($filePath = null, array $options = []) |
||
63 | { |
||
64 | $filePath = empty($filePath) |
||
65 | ? $this->filePath |
||
66 | : $filePath; |
||
67 | |||
68 | $sections = (empty($options) |
||
69 | ? true |
||
70 | : $options[ 'sections' ]); |
||
71 | |||
72 | $content = null; |
||
73 | |||
74 | if ($sections) { |
||
75 | foreach ($this->getArrayCopy() as $section => $data) { |
||
76 | if (is_array($data)) { |
||
77 | $content .= '[' . $section . ']' . PHP_EOL; |
||
78 | |||
79 | foreach ($data as $key => $value) { |
||
80 | if (is_array($value)) { |
||
81 | foreach ($value as $valueChild) { |
||
82 | $content .= $key . '[] = ' . (is_numeric($valueChild) |
||
83 | ? $valueChild |
||
84 | : '"' . $valueChild . '"') . PHP_EOL; |
||
85 | } |
||
86 | } elseif (strpos($key, ';') !== false) { |
||
87 | $content .= '; ' . trim(ucfirst($key), ';') . ' ' . $value . PHP_EOL; |
||
88 | } elseif (empty($value)) { |
||
89 | $content .= $key . ' = ' . PHP_EOL; |
||
90 | } else { |
||
91 | $content .= $key . ' = ' . (is_numeric($value) |
||
92 | ? $value |
||
93 | : '"' . $value . '"') . PHP_EOL; |
||
94 | } |
||
95 | } |
||
96 | |||
97 | $content .= PHP_EOL; |
||
98 | } elseif (strpos($section, ';') !== false) { |
||
99 | $content .= '; ' . trim(ucfirst($section), ';') . ' ' . $data . PHP_EOL; |
||
100 | } elseif (empty($data)) { |
||
101 | $content .= $section . ' = ' . PHP_EOL; |
||
102 | } else { |
||
103 | $content .= $section . ' = ' . (is_numeric($data) |
||
104 | ? $data |
||
105 | : '"' . $data . '"') . PHP_EOL; |
||
106 | } |
||
107 | } |
||
108 | } else { |
||
109 | foreach ($this->getArrayCopy() as $key => $value) { |
||
110 | if (is_array($value)) { |
||
111 | foreach ($value as $valueChild) { |
||
112 | $content .= $key . '[] = ' . (is_numeric($valueChild) |
||
113 | ? $valueChild |
||
114 | : '"' . $valueChild . '"') . PHP_EOL; |
||
115 | } |
||
116 | } elseif (strpos($key, ';') !== false) { |
||
117 | $content .= '; ' . trim(ucfirst($key), ';') . ' ' . $value . PHP_EOL; |
||
118 | } elseif (empty($value)) { |
||
119 | $content .= $key . ' = ' . PHP_EOL; |
||
120 | } else { |
||
121 | $content .= $key . ' = ' . (is_numeric($value) |
||
122 | ? $value |
||
123 | : '"' . $value . '"') . PHP_EOL; |
||
124 | } |
||
125 | } |
||
126 | } |
||
127 | |||
128 | return (new File())->write($filePath, $content); |
||
129 | } |
||
130 | } |