Conditions | 28 |
Paths | 83 |
Total Lines | 91 |
Code Lines | 52 |
Lines | 6 |
Ratio | 6.59 % |
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 |
||
261 | private function parseSegment($index, $segment, &$registers) |
||
262 | { |
||
263 | $minv = [0, 0, 1, 1, 0]; |
||
264 | $maxv = [59, 23, 31, 12, 7]; |
||
265 | $strv = [false, false, false, self::$months, self::$weekdays]; |
||
266 | |||
267 | // month names, weekdays |
||
268 | if ($strv[$index] !== false && isset($strv[$index][strtolower($segment)])) { |
||
269 | // cannot be used with lists or ranges, see crontab(5) man page |
||
270 | $registers[$index][$strv[$index][strtolower($segment)]] = true; |
||
271 | } else { |
||
272 | // split up list into segments (e.g. "1,3-5,9") |
||
273 | foreach (explode(',', $segment) as $listsegment) { |
||
274 | // parse stepping notation |
||
275 | if (strpos($listsegment, '/') !== false) { |
||
276 | if (sizeof($stepsegments = explode('/', $listsegment)) === 2) { |
||
277 | $listsegment = $stepsegments[0]; |
||
278 | |||
279 | if (is_numeric($stepsegments[1])) { |
||
280 | if ($stepsegments[1] > 0 && $stepsegments[1] <= $maxv[$index]) { |
||
281 | $steps = intval($stepsegments[1]); |
||
282 | } else { |
||
283 | throw new \Exception('stepping value out of allowed range'); |
||
284 | } |
||
285 | } else { |
||
286 | throw new \Exception('non-numeric stepping notation'); |
||
287 | } |
||
288 | } else { |
||
289 | throw new \Exception('invalid stepping notation'); |
||
290 | } |
||
291 | } else { |
||
292 | $steps = 1; |
||
293 | } |
||
294 | |||
295 | // single value |
||
296 | if (is_numeric($listsegment)) { |
||
297 | if (intval($listsegment) < $minv[$index] || intval($listsegment) > $maxv[$index]) { |
||
298 | throw new \Exception('value out of allowed range'); |
||
299 | } |
||
300 | |||
301 | if ($steps !== 1) { |
||
302 | throw new \Exception('invalid combination of value and stepping notation'); |
||
303 | } |
||
304 | |||
305 | $registers[$index][intval($listsegment)] = true; |
||
306 | } else { |
||
307 | // asterisk indicates full range of values |
||
308 | if ($listsegment === '*') { |
||
309 | $listsegment = sprintf('%d-%d', $minv[$index], $maxv[$index]); |
||
310 | } |
||
311 | |||
312 | // range of values, e.g. "9-17" |
||
313 | if (strpos($listsegment, '-') !== false) { |
||
314 | if (sizeof($ranges = explode('-', $listsegment)) !== 2) { |
||
315 | throw new \Exception('invalid range notation'); |
||
316 | } |
||
317 | |||
318 | // validate range |
||
319 | foreach ($ranges as $range) { |
||
320 | if (is_numeric($range)) { |
||
321 | if (intval($range) < $minv[$index] || intval($range) > $maxv[$index]) { |
||
322 | throw new \Exception('invalid range start or end value'); |
||
323 | } |
||
324 | } else { |
||
325 | throw new \Exception('non-numeric range notation'); |
||
326 | } |
||
327 | } |
||
328 | |||
329 | // fill matching register |
||
330 | if ($ranges[0] === $ranges[1]) { |
||
331 | $registers[$index][$ranges[0]] = true; |
||
332 | } else { |
||
333 | for ($i = $minv[$index]; $i <= $maxv[$index]; $i++) { |
||
334 | if (($i - $ranges[0]) % $steps === 0) { |
||
335 | if ($ranges[0] < $ranges[1]) { |
||
336 | View Code Duplication | if ($i >= $ranges[0] && $i <= $ranges[1]) { |
|
337 | $registers[$index][$i] = true; |
||
338 | } |
||
339 | View Code Duplication | } elseif ($i >= $ranges[0] || $i <= $ranges[1]) { |
|
340 | $registers[$index][$i] = true; |
||
341 | } |
||
342 | } |
||
343 | } |
||
344 | } |
||
345 | } else { |
||
346 | throw new \Exception('failed to parse list segment'); |
||
347 | } |
||
348 | } |
||
349 | } |
||
350 | } |
||
351 | } |
||
352 | } |
||
353 |
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.