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