Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Cron often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Cron, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class Cron |
||
18 | { |
||
19 | const ERROR_MINUTE = 1; |
||
20 | const ERROR_HOUR = 2; |
||
21 | const ERROR_DAY_OF_MONTH = 3; |
||
22 | const ERROR_MONTH = 4; |
||
23 | const ERROR_DAY_OF_WEEK = 5; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $minute = '*'; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $hour = '*'; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $dayOfMonth = '*'; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $month = '*'; |
||
44 | |||
45 | /** |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $dayOfWeek = '*'; |
||
49 | |||
50 | /** |
||
51 | * @var string |
||
52 | */ |
||
53 | protected $command; |
||
54 | |||
55 | /** |
||
56 | * @var string |
||
57 | */ |
||
58 | protected $logFile = null; |
||
59 | |||
60 | /** |
||
61 | * The size of the log file |
||
62 | * |
||
63 | * @var string |
||
64 | */ |
||
65 | protected $logSize = null; |
||
66 | |||
67 | /** |
||
68 | * @var string |
||
69 | */ |
||
70 | protected $errorFile = null; |
||
71 | |||
72 | /** |
||
73 | * The size of the error file |
||
74 | * |
||
75 | * @var string |
||
76 | */ |
||
77 | protected $errorSize = null; |
||
78 | |||
79 | /** |
||
80 | * The last run time based on when log files have been written |
||
81 | * |
||
82 | * @var int |
||
83 | */ |
||
84 | protected $lastRunTime = null; |
||
85 | |||
86 | /** |
||
87 | * The status of the cron, based on the log files |
||
88 | * |
||
89 | * @var string |
||
90 | */ |
||
91 | protected $status; |
||
92 | |||
93 | /** |
||
94 | * @var string |
||
95 | */ |
||
96 | protected $comment; |
||
97 | |||
98 | /** |
||
99 | * isSuspended |
||
100 | * |
||
101 | * @var boolean |
||
102 | * @access protected |
||
103 | */ |
||
104 | protected $isSuspended = false; |
||
105 | |||
106 | /** |
||
107 | * Parses a cron line into a Cron instance |
||
108 | * |
||
109 | * @static |
||
110 | * |
||
111 | * @param $cron string The cron line |
||
112 | * |
||
113 | * @return Cron |
||
114 | */ |
||
115 | public static function parse($cron) |
||
193 | |||
194 | /** |
||
195 | * @param string $command |
||
196 | * |
||
197 | * @return $this |
||
198 | */ |
||
199 | public function setCommand($command) |
||
205 | |||
206 | /** |
||
207 | * @return string |
||
208 | */ |
||
209 | public function getCommand() |
||
213 | |||
214 | /** |
||
215 | * @param string $dayOfMonth |
||
216 | * |
||
217 | * @return $this |
||
218 | * @throws InvalidArgumentException |
||
219 | */ |
||
220 | public function setDayOfMonth($dayOfMonth) |
||
233 | |||
234 | /** |
||
235 | * @return string |
||
236 | */ |
||
237 | public function getDayOfMonth() |
||
241 | |||
242 | /** |
||
243 | * @param string $dayOfWeek |
||
244 | * |
||
245 | * @return $this |
||
246 | * @throws InvalidArgumentException |
||
247 | */ |
||
248 | public function setDayOfWeek($dayOfWeek) |
||
261 | |||
262 | /** |
||
263 | * @return string |
||
264 | */ |
||
265 | public function getDayOfWeek() |
||
269 | |||
270 | /** |
||
271 | * @param string $hour |
||
272 | * |
||
273 | * @return $this |
||
274 | * @throws InvalidArgumentException |
||
275 | */ |
||
276 | View Code Duplication | public function setHour($hour) |
|
289 | |||
290 | /** |
||
291 | * @return string |
||
292 | */ |
||
293 | public function getHour() |
||
297 | |||
298 | /** |
||
299 | * @param string $minute |
||
300 | * |
||
301 | * @return $this |
||
302 | * @throws InvalidArgumentException |
||
303 | */ |
||
304 | View Code Duplication | public function setMinute($minute) |
|
317 | |||
318 | /** |
||
319 | * @return string |
||
320 | */ |
||
321 | public function getMinute() |
||
325 | |||
326 | /** |
||
327 | * @param string $month |
||
328 | * |
||
329 | * @return $this |
||
330 | * @throws InvalidArgumentException |
||
331 | */ |
||
332 | View Code Duplication | public function setMonth($month) |
|
345 | |||
346 | /** |
||
347 | * @return string |
||
348 | */ |
||
349 | public function getMonth() |
||
353 | |||
354 | /** |
||
355 | * @param string $comment |
||
356 | * |
||
357 | * @return $this |
||
358 | */ |
||
359 | public function setComment($comment) |
||
365 | |||
366 | /** |
||
367 | * @return string |
||
368 | */ |
||
369 | public function getComment() |
||
373 | |||
374 | /** |
||
375 | * @param string $logFile |
||
376 | * |
||
377 | * @return $this |
||
378 | */ |
||
379 | public function setLogFile($logFile) |
||
385 | |||
386 | /** |
||
387 | * @return string |
||
388 | */ |
||
389 | public function getLogFile() |
||
393 | |||
394 | /** |
||
395 | * @param string $errorFile |
||
396 | * |
||
397 | * @return $this |
||
398 | */ |
||
399 | public function setErrorFile($errorFile) |
||
405 | |||
406 | /** |
||
407 | * @return string |
||
408 | */ |
||
409 | public function getErrorFile() |
||
413 | |||
414 | /** |
||
415 | * @param int $lastRunTime |
||
416 | * |
||
417 | * @return $this |
||
418 | */ |
||
419 | public function setLastRunTime($lastRunTime) |
||
425 | |||
426 | /** |
||
427 | * @return int |
||
428 | */ |
||
429 | public function getLastRunTime() |
||
433 | |||
434 | /** |
||
435 | * @param string $errorSize |
||
436 | * |
||
437 | * @return $this |
||
438 | */ |
||
439 | public function setErrorSize($errorSize) |
||
445 | |||
446 | /** |
||
447 | * @return string |
||
448 | */ |
||
449 | public function getErrorSize() |
||
453 | |||
454 | /** |
||
455 | * @param string $logSize |
||
456 | * |
||
457 | * @return $this |
||
458 | */ |
||
459 | public function setLogSize($logSize) |
||
465 | |||
466 | /** |
||
467 | * @return string |
||
468 | */ |
||
469 | public function getLogSize() |
||
473 | |||
474 | /** |
||
475 | * @param string $status |
||
476 | * |
||
477 | * @return $this |
||
478 | */ |
||
479 | public function setStatus($status) |
||
485 | |||
486 | /** |
||
487 | * @return string |
||
488 | */ |
||
489 | public function getStatus() |
||
493 | |||
494 | /** |
||
495 | * Concatenate time data to get the time expression |
||
496 | * |
||
497 | * @return string |
||
498 | */ |
||
499 | public function getExpression() |
||
503 | |||
504 | /** |
||
505 | * Gets the value of isSuspended |
||
506 | * |
||
507 | * @return boolean |
||
508 | */ |
||
509 | public function isSuspended() |
||
513 | |||
514 | /** |
||
515 | * Sets the value of isSuspended |
||
516 | * |
||
517 | * @param boolean $isSuspended status |
||
518 | * |
||
519 | * @return Cron |
||
520 | */ |
||
521 | public function setSuspended($isSuspended = true) |
||
529 | |||
530 | /** |
||
531 | * Transforms the cron instance into a cron line |
||
532 | * |
||
533 | * @return string |
||
534 | */ |
||
535 | public function __toString() |
||
555 | } |
||
556 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: