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 |
||
| 8 | class Cron |
||
|
|
|||
| 9 | { |
||
| 10 | /** |
||
| 11 | * Weekday look-up table |
||
| 12 | * |
||
| 13 | * @var array |
||
| 14 | */ |
||
| 15 | protected static $weekdays = [ |
||
| 16 | 'sun' => 0, |
||
| 17 | 'mon' => 1, |
||
| 18 | 'tue' => 2, |
||
| 19 | 'wed' => 3, |
||
| 20 | 'thu' => 4, |
||
| 21 | 'fri' => 5, |
||
| 22 | 'sat' => 6 |
||
| 23 | ]; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Month name look-up table |
||
| 27 | * |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | protected static $months = [ |
||
| 31 | 'jan' => 1, |
||
| 32 | 'feb' => 2, |
||
| 33 | 'mar' => 3, |
||
| 34 | 'apr' => 4, |
||
| 35 | 'may' => 5, |
||
| 36 | 'jun' => 6, |
||
| 37 | 'jul' => 7, |
||
| 38 | 'aug' => 8, |
||
| 39 | 'sep' => 9, |
||
| 40 | 'oct' => 10, |
||
| 41 | 'nov' => 11, |
||
| 42 | 'dec' => 12 |
||
| 43 | ]; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Cron expression |
||
| 47 | * |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | protected $expression; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Time zone |
||
| 54 | * |
||
| 55 | * @var \DateTimeZone |
||
| 56 | */ |
||
| 57 | protected $timeZone; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Matching register |
||
| 61 | * |
||
| 62 | * @var array|null |
||
| 63 | */ |
||
| 64 | protected $registers; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Class constructor sets cron expression property |
||
| 68 | * |
||
| 69 | * @param string $expression cron expression |
||
| 70 | * @param \DateTimeZone $timeZone |
||
| 71 | */ |
||
| 72 | public function __construct($expression = '* * * * *', \DateTimeZone $timeZone = null) |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Set expression |
||
| 80 | * |
||
| 81 | * @param string $expression |
||
| 82 | * @return self |
||
| 83 | */ |
||
| 84 | public function setExpression($expression) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Set time zone |
||
| 94 | * |
||
| 95 | * @param \DateTimeZone $timeZone |
||
| 96 | * @return self |
||
| 97 | */ |
||
| 98 | public function setTimeZone(\DateTimeZone $timeZone = null) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Parse and validate cron expression |
||
| 106 | * |
||
| 107 | * @return bool true if expression is valid, or false on error |
||
| 108 | */ |
||
| 109 | public function isValid() |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Match current or given date/time against cron expression |
||
| 126 | * |
||
| 127 | * @param mixed $dtime \DateTime object, timestamp or null |
||
| 128 | * @return bool |
||
| 129 | */ |
||
| 130 | public function isMatching($dtime = null) |
||
| 131 | { |
||
| 132 | if ($dtime instanceof \DateTime) { |
||
| 133 | $dtime->setTimezone($this->timeZone); |
||
| 134 | } else { |
||
| 135 | $dt = new \DateTime('now', $this->timeZone); |
||
| 136 | |||
| 137 | if ((int)$dtime > 0) { |
||
| 138 | $dt->setTimestamp($dtime); |
||
| 139 | } |
||
| 140 | |||
| 141 | $dtime = $dt; |
||
| 142 | } |
||
| 143 | |||
| 144 | $segments = sscanf($dtime->format('i G j n w'), '%d %d %d %d %d'); |
||
| 145 | |||
| 146 | try { |
||
| 147 | $result = true; |
||
| 148 | |||
| 149 | foreach ($this->parse() as $i => $item) { |
||
| 150 | if (isset($item[(int)$segments[$i]]) === false) { |
||
| 151 | $result = false; |
||
| 152 | break; |
||
| 153 | } |
||
| 154 | } |
||
| 155 | } catch (\Exception $e) { |
||
| 156 | $result = false; |
||
| 157 | } |
||
| 158 | |||
| 159 | return $result; |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Calculate next matching timestamp |
||
| 164 | * |
||
| 165 | * @param mixed $dtime \DateTime object, timestamp or null |
||
| 166 | * @return int|bool next matching timestamp, or false on error |
||
| 167 | */ |
||
| 168 | public function getNext($dtime = null) |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Parse cron expression and return expression parsed into matchable registers |
||
| 232 | * |
||
| 233 | * @return array |
||
| 234 | * @throws \Exception |
||
| 235 | */ |
||
| 236 | private function parse() |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param int $index |
||
| 255 | * @param string $segment |
||
| 256 | * @param array $registers |
||
| 257 | * @throws \Exception |
||
| 258 | */ |
||
| 259 | private function parseSegment($index, $segment, &$registers) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @param int $index |
||
| 277 | * @param string $element |
||
| 278 | * @param array $registers |
||
| 279 | * @throws \Exception |
||
| 280 | */ |
||
| 281 | private function parseElement($index, $element, &$registers) |
||
| 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.