| 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 |
||
| 220 | 70 | public function parseVotes (string $input, bool $allowFile = true) |
|
| 221 | { |
||
| 222 | 70 | $input = CondorcetUtil::prepareParse($input, $allowFile); |
|
| 223 | 70 | if ($input === false) : |
|
| 224 | return $input; |
||
| 225 | endif; |
||
| 226 | |||
| 227 | // Check each lines |
||
| 228 | 70 | $adding = []; |
|
| 229 | 70 | foreach ($input as $line) : |
|
| 230 | // Empty Line |
||
| 231 | 70 | if (empty($line)) : |
|
| 232 | 63 | continue; |
|
| 233 | endif; |
||
| 234 | |||
| 235 | // Multiples |
||
| 236 | 70 | $is_multiple = mb_strpos($line, '*'); |
|
| 237 | 70 | View Code Duplication | if ($is_multiple !== false) : |
|
1 ignored issue
–
show
|
|||
| 238 | 63 | $multiple = trim( substr($line, $is_multiple + 1) ); |
|
| 239 | |||
| 240 | // Errors |
||
| 241 | 63 | if ( !is_numeric($multiple) ) : |
|
| 242 | throw new CondorcetException(13, null); |
||
| 243 | endif; |
||
| 244 | |||
| 245 | 63 | $multiple = intval($multiple); |
|
| 246 | |||
| 247 | // Reformat line |
||
| 248 | 63 | $line = substr($line, 0, $is_multiple); |
|
| 249 | else : |
||
| 250 | 9 | $multiple = 1; |
|
| 251 | endif; |
||
| 252 | |||
| 253 | // Vote Weight |
||
| 254 | 70 | $is_voteWeight = mb_strpos($line, '^'); |
|
| 255 | 70 | View Code Duplication | if ($is_voteWeight !== false) : |
|
1 ignored issue
–
show
|
|||
| 256 | 2 | $weight = trim( substr($line, $is_voteWeight + 1) ); |
|
| 257 | |||
| 258 | // Errors |
||
| 259 | 2 | if ( !is_numeric($weight) ) : |
|
| 260 | throw new CondorcetException(13, null); |
||
| 261 | endif; |
||
| 262 | |||
| 263 | 2 | $weight = intval($weight); |
|
| 264 | |||
| 265 | // Reformat line |
||
| 266 | 2 | $line = substr($line, 0, $is_voteWeight); |
|
| 267 | else : |
||
| 268 | 69 | $weight = 1; |
|
| 269 | endif; |
||
| 270 | |||
| 271 | // Tags + vote |
||
| 272 | 70 | if (mb_strpos($line, '||') !== false) : |
|
| 273 | 4 | $data = explode('||', $line); |
|
| 274 | |||
| 275 | 4 | $vote = $data[1]; |
|
| 276 | 4 | $tags = $data[0]; |
|
| 277 | // Vote without tags |
||
| 278 | else : |
||
| 279 | 69 | $vote = $line; |
|
| 280 | 69 | $tags = null; |
|
| 281 | endif; |
||
| 282 | |||
| 283 | // addVote |
||
| 284 | 70 | for ($i = 0; $i < $multiple; $i++) : |
|
| 285 | 70 | View Code Duplication | if (self::$_maxParseIteration !== null && count($adding) >= self::$_maxParseIteration) : |
|
1 ignored issue
–
show
|
|||
| 286 | 1 | throw new CondorcetException(12, self::$_maxParseIteration); |
|
| 287 | endif; |
||
| 288 | |||
| 289 | try { |
||
| 290 | 70 | $adding[] = ($newVote = $this->addVote($vote, $tags)); |
|
| 291 | 70 | $newVote->setWeight($weight); |
|
| 292 | 1 | } catch (CondorcetException $e) {} |
|
|
1 ignored issue
–
show
|
|||
| 293 | endfor; |
||
| 294 | endforeach; |
||
| 295 | |||
| 296 | 70 | return $adding; |
|
| 297 | } |
||
| 298 | |||
| 300 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.