Conditions | 13 |
Paths | 46 |
Total Lines | 78 |
Code Lines | 51 |
Lines | 33 |
Ratio | 42.31 % |
Tests | 34 |
CRAP Score | 13.0901 |
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 |
||
222 | 70 | public function parseVotes (string $input, bool $allowFile = true) |
|
223 | { |
||
224 | 70 | $input = CondorcetUtil::prepareParse($input, $allowFile); |
|
225 | 70 | if ($input === false) : |
|
226 | return $input; |
||
227 | endif; |
||
228 | |||
229 | // Check each lines |
||
230 | 70 | $adding = []; |
|
231 | 70 | foreach ($input as $line) : |
|
232 | // Empty Line |
||
233 | 70 | if (empty($line)) : |
|
234 | 63 | continue; |
|
235 | endif; |
||
236 | |||
237 | // Multiples |
||
238 | 70 | $is_multiple = mb_strpos($line, '*'); |
|
239 | 70 | View Code Duplication | if ($is_multiple !== false) : |
240 | 63 | $multiple = trim( substr($line, $is_multiple + 1) ); |
|
241 | |||
242 | // Errors |
||
243 | 63 | if ( !is_numeric($multiple) ) : |
|
244 | throw new CondorcetException(13, null); |
||
245 | endif; |
||
246 | |||
247 | 63 | $multiple = intval($multiple); |
|
248 | |||
249 | // Reformat line |
||
250 | 63 | $line = substr($line, 0, $is_multiple); |
|
251 | else : |
||
252 | 9 | $multiple = 1; |
|
253 | endif; |
||
254 | |||
255 | // Vote Weight |
||
256 | 70 | $is_voteWeight = mb_strpos($line, '^'); |
|
257 | 70 | View Code Duplication | if ($is_voteWeight !== false) : |
258 | 2 | $weight = trim( substr($line, $is_voteWeight + 1) ); |
|
259 | |||
260 | // Errors |
||
261 | 2 | if ( !is_numeric($weight) ) : |
|
262 | throw new CondorcetException(13, null); |
||
263 | endif; |
||
264 | |||
265 | 2 | $weight = intval($weight); |
|
266 | |||
267 | // Reformat line |
||
268 | 2 | $line = substr($line, 0, $is_voteWeight); |
|
269 | else : |
||
270 | 69 | $weight = 1; |
|
271 | endif; |
||
272 | |||
273 | // Tags + vote |
||
274 | 70 | if (mb_strpos($line, '||') !== false) : |
|
275 | 4 | $data = explode('||', $line); |
|
276 | |||
277 | 4 | $vote = $data[1]; |
|
278 | 4 | $tags = $data[0]; |
|
279 | // Vote without tags |
||
280 | else : |
||
281 | 69 | $vote = $line; |
|
282 | 69 | $tags = null; |
|
283 | endif; |
||
284 | |||
285 | // addVote |
||
286 | 70 | for ($i = 0; $i < $multiple; $i++) : |
|
287 | 70 | View Code Duplication | if (self::$_maxParseIteration !== null && count($adding) >= self::$_maxParseIteration) : |
288 | 1 | throw new CondorcetException(12, self::$_maxParseIteration); |
|
289 | endif; |
||
290 | |||
291 | try { |
||
292 | 70 | $adding[] = ($newVote = $this->addVote($vote, $tags)); |
|
293 | 70 | $newVote->setWeight($weight); |
|
294 | 1 | } catch (CondorcetException $e) {} |
|
1 ignored issue
–
show
|
|||
295 | endfor; |
||
296 | endforeach; |
||
297 | |||
298 | 70 | return $adding; |
|
299 | } |
||
300 | |||
302 |