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 |
||
| 21 | class Cron extends EventProvider |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * how long ahead to schedule cron jobs |
||
| 25 | * |
||
| 26 | * @var int (minute) |
||
| 27 | */ |
||
| 28 | protected $scheduleAhead = 60; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * the Doctrine ORM Entity Manager |
||
| 32 | * |
||
| 33 | * @var EntityManager |
||
| 34 | */ |
||
| 35 | protected $em; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * List of cron jobs to be managed |
||
| 39 | */ |
||
| 40 | protected $cronjobs; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * how long before a scheduled job is considered missed |
||
| 44 | * |
||
| 45 | * @var int (minute) |
||
| 46 | */ |
||
| 47 | protected $scheduleLifetime = 60; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * maximum running time of each cron job |
||
| 51 | * |
||
| 52 | * @var int (minute) |
||
| 53 | */ |
||
| 54 | protected $maxRunningTime = 60; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * how long to keep successfully completed cron job logs |
||
| 58 | * |
||
| 59 | * @var int (minute) |
||
| 60 | */ |
||
| 61 | protected $successLogLifetime = 300; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * how long to keep failed (missed / error) cron job logs |
||
| 65 | * |
||
| 66 | * @var int (minute) |
||
| 67 | */ |
||
| 68 | protected $failureLogLifetime = 10080; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * set of pending cron jobs |
||
| 72 | * |
||
| 73 | * wrapped the Repo function here to implement a (crude) cache feature |
||
| 74 | * |
||
| 75 | * @var array of Entity\Cronjob |
||
| 76 | */ |
||
| 77 | protected $pending; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * |
||
| 81 | * @var ServiceManager |
||
| 82 | */ |
||
| 83 | protected $serviceLocator; |
||
| 84 | |||
| 85 | public function __construct(ServiceLocatorInterface $locator) |
||
| 89 | |||
| 90 | /** |
||
| 91 | * main entry function |
||
| 92 | * |
||
| 93 | * 1. schedule new cron jobs |
||
| 94 | * 2. process cron jobs |
||
| 95 | * 3. cleanup old logs |
||
| 96 | * |
||
| 97 | * @return self |
||
| 98 | */ |
||
| 99 | public function run() |
||
| 107 | |||
| 108 | /** |
||
| 109 | * trigger an event and fetch crons |
||
| 110 | * Return array |
||
| 111 | */ |
||
| 112 | public function getCronjobs() |
||
| 137 | |||
| 138 | /** |
||
| 139 | * |
||
| 140 | */ |
||
| 141 | public function setCronjobs($cronjobs) |
||
| 147 | |||
| 148 | public function getPending() |
||
| 158 | |||
| 159 | public function resetPending() |
||
| 165 | |||
| 166 | /** |
||
| 167 | * run cron jobs |
||
| 168 | * |
||
| 169 | * @return self |
||
| 170 | */ |
||
| 171 | public function process() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * schedule cron jobs |
||
| 243 | * |
||
| 244 | * @return self |
||
| 245 | */ |
||
| 246 | public function schedule() |
||
| 300 | |||
| 301 | /** |
||
| 302 | * perform various cleanup work |
||
| 303 | * |
||
| 304 | * @return self |
||
| 305 | */ |
||
| 306 | public function cleanup() |
||
| 314 | |||
| 315 | public function recoverRunning() |
||
| 338 | |||
| 339 | /** |
||
| 340 | * delete old cron job logs |
||
| 341 | * |
||
| 342 | * @return self |
||
| 343 | */ |
||
| 344 | public function cleanLog() |
||
| 372 | |||
| 373 | /** |
||
| 374 | * wrapper function |
||
| 375 | * @see Registry::register() |
||
| 376 | */ |
||
| 377 | public static function register( |
||
| 385 | |||
| 386 | /** |
||
| 387 | * try to acquire a lock on a cron job |
||
| 388 | * |
||
| 389 | * set a job to 'running' only if it is currently 'pending' |
||
| 390 | * |
||
| 391 | * @param Entity\Cronjob $job |
||
| 392 | * @return bool |
||
| 393 | */ |
||
| 394 | public function tryLockJob(Entity\Cronjob $job) |
||
| 409 | |||
| 410 | /** |
||
| 411 | * determine whether a given time falls within the given cron expr |
||
| 412 | * |
||
| 413 | * @param string|numeric $time |
||
| 414 | * timestamp or strtotime()-compatible string |
||
| 415 | * @param string $expr |
||
| 416 | * any valid cron expression, in addition supporting: |
||
| 417 | * range: '0-5' |
||
| 418 | * range + interval: '10-59/5' |
||
| 419 | * comma-separated combinations of these: '1,4,7,10-20' |
||
| 420 | * English months: 'january' |
||
| 421 | * English months (abbreviated to three letters): 'jan' |
||
| 422 | * English weekdays: 'monday' |
||
| 423 | * English weekdays (abbreviated to three letters): 'mon' |
||
| 424 | * These text counterparts can be used in all places where their |
||
| 425 | * numerical counterparts are allowed, e.g. 'jan-jun/2' |
||
| 426 | * A full example: |
||
| 427 | * '0-5,10-59/5 * 2-10,15-25 january-june/2 mon-fri' - |
||
| 428 | * every minute between minute 0-5 + every 5th min between 10-59 |
||
| 429 | * every hour |
||
| 430 | * every day between day 2-10 and day 15-25 |
||
| 431 | * every 2nd month between January-June |
||
| 432 | * Monday-Friday |
||
| 433 | * @throws Exception\InvalidArgumentException on invalid cron expression |
||
| 434 | * @return bool |
||
| 435 | */ |
||
| 436 | public static function matchTime($time, $expr) |
||
| 458 | |||
| 459 | /** |
||
| 460 | * match a cron expression component to a given corresponding date/time |
||
| 461 | * |
||
| 462 | * In the expression, * * * * *, each component |
||
| 463 | * *[1] *[2] *[3] *[4] *[5] |
||
| 464 | * will correspond to a getdate() component |
||
| 465 | * 1. $date['minutes'] |
||
| 466 | * 2. $date['hours'] |
||
| 467 | * 3. $date['mday'] |
||
| 468 | * 4. $date['mon'] |
||
| 469 | * 5. $date['wday'] |
||
| 470 | * |
||
| 471 | * @see self::exprToNumeric() for additional valid string values |
||
| 472 | * |
||
| 473 | * @param string $expr |
||
| 474 | * @param numeric $num |
||
| 475 | * @throws Exception\InvalidArgumentException on invalid expression |
||
| 476 | * @return bool |
||
| 477 | */ |
||
| 478 | public static function matchTimeComponent($expr, $num) |
||
| 561 | |||
| 562 | /** |
||
| 563 | * parse a string month / weekday expression to its numeric equivalent |
||
| 564 | * |
||
| 565 | * @param string|numeric $value |
||
| 566 | * accepts, case insensitive, |
||
| 567 | * - Jan - Dec |
||
| 568 | * - Sun - Sat |
||
| 569 | * - (or their long forms - only the first three letters important) |
||
| 570 | * @return int|false |
||
| 571 | */ |
||
| 572 | public static function exprToNumeric($value) |
||
| 614 | |||
| 615 | public function setScheduleAhead($scheduleAhead) |
||
| 621 | |||
| 622 | public function getScheduleAhead() |
||
| 626 | |||
| 627 | public function setScheduleLifetime($scheduleLifetime) |
||
| 633 | |||
| 634 | public function getScheduleLifeTime() |
||
| 638 | |||
| 639 | public function setMaxRunningTime($maxRunningTime) |
||
| 645 | |||
| 646 | public function getMaxRunningtime() |
||
| 650 | |||
| 651 | public function setSuccessLogLifetime($successLogLifetime) |
||
| 661 | |||
| 662 | public function setFailureLogLifetime($failureLogLifetime) |
||
| 668 | |||
| 669 | public function getFailureLogLifetime() |
||
| 673 | |||
| 674 | public function setEm(EntityManager $em) |
||
| 680 | |||
| 681 | public function getEm() |
||
| 689 | } |
||
| 690 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.