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 | * Value boundaries  | 
            ||
| 47 | *  | 
            ||
| 48 | * @var array  | 
            ||
| 49 | */  | 
            ||
| 50 | protected static $boundaries = [  | 
            ||
| 51 | 0 => [  | 
            ||
| 52 | 'min' => 0,  | 
            ||
| 53 | 'max' => 59  | 
            ||
| 54 | ],  | 
            ||
| 55 | 1 => [  | 
            ||
| 56 | 'min' => 0,  | 
            ||
| 57 | 'max' => 23  | 
            ||
| 58 | ],  | 
            ||
| 59 | 2 => [  | 
            ||
| 60 | 'min' => 1,  | 
            ||
| 61 | 'max' => 31  | 
            ||
| 62 | ],  | 
            ||
| 63 | 3 => [  | 
            ||
| 64 | 'min' => 1,  | 
            ||
| 65 | 'max' => 12  | 
            ||
| 66 | ],  | 
            ||
| 67 | 4 => [  | 
            ||
| 68 | 'min' => 0,  | 
            ||
| 69 | 'max' => 7  | 
            ||
| 70 | ]  | 
            ||
| 71 | ];  | 
            ||
| 72 | |||
| 73 | /**  | 
            ||
| 74 | * Cron expression  | 
            ||
| 75 | *  | 
            ||
| 76 | * @var string  | 
            ||
| 77 | */  | 
            ||
| 78 | protected $expression;  | 
            ||
| 79 | |||
| 80 | /**  | 
            ||
| 81 | * Time zone  | 
            ||
| 82 | *  | 
            ||
| 83 | * @var \DateTimeZone  | 
            ||
| 84 | */  | 
            ||
| 85 | protected $timeZone;  | 
            ||
| 86 | |||
| 87 | /**  | 
            ||
| 88 | * Matching register  | 
            ||
| 89 | *  | 
            ||
| 90 | * @var array|null  | 
            ||
| 91 | */  | 
            ||
| 92 | protected $register;  | 
            ||
| 93 | |||
| 94 | /**  | 
            ||
| 95 | * Class constructor sets cron expression property  | 
            ||
| 96 | *  | 
            ||
| 97 | * @param string $expression cron expression  | 
            ||
| 98 | * @param \DateTimeZone $timeZone  | 
            ||
| 99 | */  | 
            ||
| 100 | public function __construct($expression = '* * * * *', \DateTimeZone $timeZone = null)  | 
            ||
| 105 | |||
| 106 | /**  | 
            ||
| 107 | * Set expression  | 
            ||
| 108 | *  | 
            ||
| 109 | * @param string $expression  | 
            ||
| 110 | * @return self  | 
            ||
| 111 | */  | 
            ||
| 112 | public function setExpression($expression)  | 
            ||
| 119 | |||
| 120 | /**  | 
            ||
| 121 | * Set time zone  | 
            ||
| 122 | *  | 
            ||
| 123 | * @param \DateTimeZone $timeZone  | 
            ||
| 124 | * @return self  | 
            ||
| 125 | */  | 
            ||
| 126 | public function setTimeZone(\DateTimeZone $timeZone = null)  | 
            ||
| 131 | |||
| 132 | /**  | 
            ||
| 133 | * Parse and validate cron expression  | 
            ||
| 134 | *  | 
            ||
| 135 | * @return bool true if expression is valid, or false on error  | 
            ||
| 136 | */  | 
            ||
| 137 | public function isValid()  | 
            ||
| 138 |     { | 
            ||
| 139 | $result = true;  | 
            ||
| 140 | |||
| 141 |         if ($this->register === null) { | 
            ||
| 142 |             try { | 
            ||
| 143 | $this->register = $this->parse();  | 
            ||
| 144 |             } catch (\Exception $e) { | 
            ||
| 145 | $result = false;  | 
            ||
| 146 | }  | 
            ||
| 147 | }  | 
            ||
| 148 | |||
| 149 | return $result;  | 
            ||
| 150 | }  | 
            ||
| 151 | |||
| 152 | /**  | 
            ||
| 153 | * Match current or given date/time against cron expression  | 
            ||
| 154 | *  | 
            ||
| 155 | * @param mixed $dtime \DateTime object, timestamp or null  | 
            ||
| 156 | * @return bool  | 
            ||
| 157 | */  | 
            ||
| 158 | public function isMatching($dtime = null)  | 
            ||
| 189 | |||
| 190 | /**  | 
            ||
| 191 | * Calculate next matching timestamp  | 
            ||
| 192 | *  | 
            ||
| 193 | * @param mixed $dtime \DateTime object, timestamp or null  | 
            ||
| 194 | * @return int|bool next matching timestamp, or false on error  | 
            ||
| 195 | */  | 
            ||
| 196 | public function getNext($dtime = null)  | 
            ||
| 257 | |||
| 258 | /**  | 
            ||
| 259 | * Parse whole cron expression  | 
            ||
| 260 | *  | 
            ||
| 261 | * @return array  | 
            ||
| 262 | * @throws \Exception  | 
            ||
| 263 | */  | 
            ||
| 264 | private function parse()  | 
            ||
| 280 | |||
| 281 | /**  | 
            ||
| 282 | * @param int $index  | 
            ||
| 283 | * @param string $segment  | 
            ||
| 284 | * @param array $register  | 
            ||
| 285 | * @throws \Exception  | 
            ||
| 286 | */  | 
            ||
| 287 | private function parseSegment($index, &$register, $segment)  | 
            ||
| 302 | |||
| 303 | /**  | 
            ||
| 304 | * @param int $index  | 
            ||
| 305 | * @param string $element  | 
            ||
| 306 | * @param array $register  | 
            ||
| 307 | * @throws \Exception  | 
            ||
| 308 | */  | 
            ||
| 309 | private function parseElement($index, array &$register, $element)  | 
            ||
| 337 | |||
| 338 | /**  | 
            ||
| 339 | * @param int $index  | 
            ||
| 340 | * @param array $register  | 
            ||
| 341 | * @param array $range  | 
            ||
| 342 | * @param int $stepping  | 
            ||
| 343 | * @throws \Exception  | 
            ||
| 344 | */  | 
            ||
| 345 | private function parseRange($index, array &$register, array $range, $stepping)  | 
            ||
| 366 | |||
| 367 | /**  | 
            ||
| 368 | * Validate range of values  | 
            ||
| 369 | *  | 
            ||
| 370 | * @param int $index  | 
            ||
| 371 | * @param array $range  | 
            ||
| 372 | * @throws \Exception  | 
            ||
| 373 | */  | 
            ||
| 374 | private function validateRange($index, array $range)  | 
            ||
| 391 | |||
| 392 | /**  | 
            ||
| 393 | * @param int $index  | 
            ||
| 394 | * @param string $element  | 
            ||
| 395 | * @return int  | 
            ||
| 396 | * @throws \Exception  | 
            ||
| 397 | */  | 
            ||
| 398 | private function parseStepping($index, &$element)  | 
            ||
| 423 | }  | 
            ||
| 424 | 
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.