| Conditions | 25 |
| Paths | 81 |
| Total Lines | 80 |
| Code Lines | 46 |
| Lines | 6 |
| Ratio | 7.5 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 281 | private function parseElement($index, $element, &$registers) |
||
| 282 | { |
||
| 283 | $stepping = 1; |
||
| 284 | $minv = [0, 0, 1, 1, 0]; |
||
| 285 | $maxv = [59, 23, 31, 12, 7]; |
||
| 286 | |||
| 287 | // parse stepping notation |
||
| 288 | if (strpos($element, '/') !== false) { |
||
| 289 | if (sizeof($stepsegments = explode('/', $element)) === 2) { |
||
| 290 | $element = $stepsegments[0]; |
||
| 291 | |||
| 292 | if (is_numeric($stepsegments[1])) { |
||
| 293 | if ($stepsegments[1] > 0 && $stepsegments[1] <= $maxv[$index]) { |
||
| 294 | $stepping = intval($stepsegments[1]); |
||
| 295 | } else { |
||
| 296 | throw new \Exception('stepping value out of allowed range'); |
||
| 297 | } |
||
| 298 | } else { |
||
| 299 | throw new \Exception('non-numeric stepping notation'); |
||
| 300 | } |
||
| 301 | } else { |
||
| 302 | throw new \Exception('invalid stepping notation'); |
||
| 303 | } |
||
| 304 | } |
||
| 305 | |||
| 306 | // single value |
||
| 307 | if (is_numeric($element)) { |
||
| 308 | if (intval($element) < $minv[$index] || intval($element) > $maxv[$index]) { |
||
| 309 | throw new \Exception('value out of allowed range'); |
||
| 310 | } |
||
| 311 | |||
| 312 | if ($stepping !== 1) { |
||
| 313 | throw new \Exception('invalid combination of value and stepping notation'); |
||
| 314 | } |
||
| 315 | |||
| 316 | $registers[$index][intval($element)] = true; |
||
| 317 | } else { |
||
| 318 | // asterisk indicates full range of values |
||
| 319 | if ($element === '*') { |
||
| 320 | $element = sprintf('%d-%d', $minv[$index], $maxv[$index]); |
||
| 321 | } |
||
| 322 | |||
| 323 | // range of values, e.g. "9-17" |
||
| 324 | if (strpos($element, '-') !== false) { |
||
| 325 | if (sizeof($ranges = explode('-', $element)) !== 2) { |
||
| 326 | throw new \Exception('invalid range notation'); |
||
| 327 | } |
||
| 328 | |||
| 329 | // validate range |
||
| 330 | foreach ($ranges as $range) { |
||
| 331 | if (is_numeric($range)) { |
||
| 332 | if (intval($range) < $minv[$index] || intval($range) > $maxv[$index]) { |
||
| 333 | throw new \Exception('invalid range start or end value'); |
||
| 334 | } |
||
| 335 | } else { |
||
| 336 | throw new \Exception('non-numeric range notation'); |
||
| 337 | } |
||
| 338 | } |
||
| 339 | |||
| 340 | // fill matching register |
||
| 341 | if ($ranges[0] === $ranges[1]) { |
||
| 342 | $registers[$index][$ranges[0]] = true; |
||
| 343 | } else { |
||
| 344 | for ($i = $minv[$index]; $i <= $maxv[$index]; $i++) { |
||
| 345 | if (($i - $ranges[0]) % $stepping === 0) { |
||
| 346 | if ($ranges[0] < $ranges[1]) { |
||
| 347 | View Code Duplication | if ($i >= $ranges[0] && $i <= $ranges[1]) { |
|
| 348 | $registers[$index][$i] = true; |
||
| 349 | } |
||
| 350 | View Code Duplication | } elseif ($i >= $ranges[0] || $i <= $ranges[1]) { |
|
| 351 | $registers[$index][$i] = true; |
||
| 352 | } |
||
| 353 | } |
||
| 354 | } |
||
| 355 | } |
||
| 356 | } else { |
||
| 357 | throw new \Exception('failed to parse list segment'); |
||
| 358 | } |
||
| 359 | } |
||
| 360 | } |
||
| 361 | } |
||
| 362 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.