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 | 89 | public function __construct($expression = '* * * * *', \DateTimeZone $timeZone = null) |
|
105 | |||
106 | /** |
||
107 | * Set expression |
||
108 | * |
||
109 | * @param string $expression |
||
110 | * @return self |
||
111 | */ |
||
112 | 89 | public function setExpression($expression) |
|
119 | |||
120 | /** |
||
121 | * Set time zone |
||
122 | * |
||
123 | * @param \DateTimeZone $timeZone |
||
124 | * @return self |
||
125 | */ |
||
126 | 89 | public function setTimeZone(\DateTimeZone $timeZone = null) |
|
131 | |||
132 | /** |
||
133 | * Calculate next matching timestamp |
||
134 | * |
||
135 | * @param mixed $dtime \DateTime object, timestamp or null |
||
136 | * @return int|bool next matching timestamp, or false on error |
||
137 | */ |
||
138 | 7 | public function getNext($dtime = null) |
|
165 | |||
166 | /** |
||
167 | * @param \DateTime $dtime |
||
168 | * @param array $pointer |
||
169 | * @return array |
||
170 | */ |
||
171 | 7 | private function adjust(\DateTime $dtime, array &$pointer) |
|
172 | { |
||
173 | 7 | $current = sscanf($dtime->format('i G j n Y w'), '%d %d %d %d %d %d'); |
|
174 | |||
175 | switch (true) { |
||
176 | 7 | View Code Duplication | case ($pointer[1] !== $current[1]): |
177 | 4 | $pointer[1] = $current[1]; |
|
178 | 4 | $dtime->setTime($current[1], 0); |
|
179 | 4 | break; |
|
180 | |||
181 | 7 | View Code Duplication | case ($pointer[0] !== $current[0]): |
182 | 5 | $pointer[0] = $current[0]; |
|
183 | 5 | $dtime->setTime($current[1], $current[0]); |
|
184 | 5 | break; |
|
185 | |||
186 | 7 | View Code Duplication | case ($pointer[4] !== $current[4]): |
187 | 1 | $pointer[4] = $current[4]; |
|
188 | 1 | $dtime->setDate($current[4], 1, 1); |
|
189 | 1 | $dtime->setTime(0, 0); |
|
190 | 1 | break; |
|
191 | |||
192 | 7 | View Code Duplication | case ($pointer[3] !== $current[3]): |
193 | 2 | $pointer[3] = $current[3]; |
|
194 | 2 | $dtime->setDate($current[4], $current[3], 1); |
|
195 | 2 | $dtime->setTime(0, 0); |
|
196 | 2 | break; |
|
197 | |||
198 | 7 | View Code Duplication | case ($pointer[2] !== $current[2]): |
199 | 2 | $pointer[2] = $current[2]; |
|
200 | 2 | $dtime->setTime(0, 0); |
|
201 | 2 | break; |
|
202 | } |
||
203 | |||
204 | 7 | return $current; |
|
205 | } |
||
206 | |||
207 | /** |
||
208 | * @param \DateTime $dtime |
||
209 | * @param array $current |
||
210 | * @return bool |
||
211 | */ |
||
212 | 7 | private function forward(\DateTime $dtime, array $current) |
|
232 | |||
233 | /** |
||
234 | * Parse and validate cron expression |
||
235 | * |
||
236 | * @return bool true if expression is valid, or false on error |
||
237 | */ |
||
238 | 88 | public function isValid() |
|
252 | |||
253 | /** |
||
254 | * Match current or given date/time against cron expression |
||
255 | * |
||
256 | * @param mixed $dtime \DateTime object, timestamp or null |
||
257 | * @return bool |
||
258 | */ |
||
259 | 82 | public function isMatching($dtime = null) |
|
278 | |||
279 | /** |
||
280 | * @param array $segments |
||
281 | * @return bool |
||
282 | * @throws \Exception |
||
283 | */ |
||
284 | 82 | private function match(array $segments) |
|
297 | |||
298 | /** |
||
299 | * Parse whole cron expression |
||
300 | * |
||
301 | * @return array |
||
302 | * @throws \Exception |
||
303 | */ |
||
304 | 89 | private function parse() |
|
322 | |||
323 | /** |
||
324 | * Parse one segment of a cron expression |
||
325 | * |
||
326 | * @param int $index |
||
327 | * @param string $segment |
||
328 | * @param array $register |
||
329 | * @throws \Exception |
||
330 | */ |
||
331 | 85 | private function parseSegment($index, array &$register, $segment) |
|
346 | |||
347 | /** |
||
348 | * @param int $index |
||
349 | * @param array $register |
||
350 | * @param string $element |
||
351 | * @throws \Exception |
||
352 | */ |
||
353 | 85 | private function parseElement($index, array &$register, $element) |
|
373 | |||
374 | /** |
||
375 | * Parse range of values, e.g. "5-10" |
||
376 | * |
||
377 | * @param int $index |
||
378 | * @param array $register |
||
379 | * @param string $range |
||
380 | * @param int $stepping |
||
381 | * @throws \Exception |
||
382 | */ |
||
383 | 78 | private function parseRange($index, array &$register, $range, $stepping) |
|
395 | |||
396 | /** |
||
397 | * Parse stepping notation, e.g. "5-10/2" => 2 |
||
398 | * |
||
399 | * @param int $index |
||
400 | * @param string $element |
||
401 | * @param int $stepping |
||
402 | * @throws \Exception |
||
403 | */ |
||
404 | 41 | private function parseStepping($index, &$element, &$stepping) |
|
413 | |||
414 | /** |
||
415 | * Validate whether a given range of values exceeds allowed value boundaries |
||
416 | * |
||
417 | * @param int $index |
||
418 | * @param array $range |
||
419 | * @return array |
||
420 | * @throws \Exception |
||
421 | */ |
||
422 | 44 | private function validateRange($index, array $range) |
|
434 | /** |
||
435 | * @param int $index |
||
436 | * @param int $value |
||
437 | * @throws \Exception |
||
438 | */ |
||
439 | 65 | private function validateValue($index, $value) |
|
450 | |||
451 | /** |
||
452 | * @param int $index |
||
453 | * @param array $segments |
||
454 | * @throws \Exception |
||
455 | */ |
||
456 | 41 | private function validateStepping($index, array $segments) |
|
466 | |||
467 | /** |
||
468 | * @param int $index |
||
469 | * @param array $register |
||
470 | * @param array $range |
||
471 | * @param int $stepping |
||
472 | */ |
||
473 | 73 | private function fillRegister($index, array &$register, array $range, $stepping) |
|
485 | |||
486 | /** |
||
487 | * @param int $index |
||
488 | * @param array $register |
||
489 | * @param array $range |
||
490 | * @param int $value |
||
491 | */ |
||
492 | 29 | private function fillRegisterAcrossBoundaries($index, array &$register, $range, $value) |
|
498 | |||
499 | /** |
||
500 | * @param int $index |
||
501 | * @param array $register |
||
502 | * @param array $range |
||
503 | * @param int $value |
||
504 | */ |
||
505 | 72 | private function fillRegisterBetweenBoundaries($index, array &$register, $range, $value) |
|
511 | } |
||
512 |
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.