Conditions | 9 |
Paths | 24 |
Total Lines | 51 |
Code Lines | 25 |
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 |
||
156 | public function setStringOptions( |
||
157 | $optionString = '', |
||
158 | $optionSeparator = null, |
||
159 | $kvSeparator = null |
||
160 | ) { |
||
161 | if (is_null($optionSeparator)) { |
||
162 | $optionSeparator = static::OPTION_SEPARATOR; |
||
163 | } |
||
164 | |||
165 | if (is_null($kvSeparator)) { |
||
166 | $kvSeparator = static::KV_SEPARATOR; |
||
167 | } |
||
168 | |||
169 | $sections = explode($optionSeparator, $optionString); |
||
170 | foreach ($sections as $section) { |
||
171 | $kvPair = explode($kvSeparator, $section); |
||
172 | |||
173 | if (1 == count($kvPair)) { |
||
174 | // Option name only |
||
175 | $key = trim(current($kvPair)); |
||
176 | |||
177 | // Continues repeat separator will cause empty value like this |
||
178 | if ('' === $key) { |
||
179 | continue; |
||
180 | } |
||
181 | |||
182 | // No value part |
||
183 | $this->set($key, static::NAME_ONLY_VALUE); |
||
184 | |||
185 | } elseif (2 == count($kvPair)) { |
||
186 | // Normal key-value pair |
||
187 | $key = trim(array_shift($kvPair)); |
||
188 | $value = trim(array_shift($kvPair)); |
||
189 | |||
190 | $lowerValue = strtolower($value); |
||
191 | if ('true' === $lowerValue) { |
||
192 | $value = true; |
||
193 | } elseif ('false' === $lowerValue) { |
||
194 | $value = false; |
||
195 | } |
||
196 | |||
197 | $this->set($key, $value); |
||
198 | |||
199 | } else { |
||
200 | throw new InvalidFormatException( |
||
201 | "Format error: {$section}" |
||
202 | ); |
||
203 | } |
||
204 | } |
||
205 | |||
206 | return $this; |
||
207 | } |
||
209 |