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 |
||
| 15 | class Cron |
||
| 16 | { |
||
| 17 | const ERROR_MINUTE = 1; |
||
| 18 | const ERROR_HOUR = 2; |
||
| 19 | const ERROR_DAY_OF_MONTH = 3; |
||
| 20 | const ERROR_MONTH = 4; |
||
| 21 | const ERROR_DAY_OF_WEEK = 5; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | protected $minute = '*'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected $hour = '*'; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | protected $dayOfMonth = '*'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | protected $month = '*'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | protected $dayOfWeek = '*'; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | protected $command; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | protected $logFile = null; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * The size of the log file |
||
| 60 | * |
||
| 61 | * @var string |
||
| 62 | */ |
||
| 63 | protected $logSize = null; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | protected $errorFile = null; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * The size of the error file |
||
| 72 | * |
||
| 73 | * @var string |
||
| 74 | */ |
||
| 75 | protected $errorSize = null; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * The last run time based on when log files have been written |
||
| 79 | * |
||
| 80 | * @var int |
||
| 81 | */ |
||
| 82 | protected $lastRunTime = null; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * The status of the cron, based on the log files |
||
| 86 | * |
||
| 87 | * @var string |
||
| 88 | */ |
||
| 89 | protected $status; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var string |
||
| 93 | */ |
||
| 94 | protected $comment; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * isSuspended |
||
| 98 | * |
||
| 99 | * @var boolean |
||
| 100 | * @access protected |
||
| 101 | */ |
||
| 102 | protected $isSuspended = false; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Parses a cron line into a Cron instance |
||
| 106 | * |
||
| 107 | * @static |
||
| 108 | * |
||
| 109 | * @param $cron string The cron line |
||
| 110 | * |
||
| 111 | * @return Cron |
||
| 112 | */ |
||
| 113 | public static function parse($cron) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @param string $command |
||
| 194 | * |
||
| 195 | * @return $this |
||
| 196 | */ |
||
| 197 | public function setCommand($command) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @return string |
||
| 206 | */ |
||
| 207 | public function getCommand() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param string $dayOfMonth |
||
| 214 | * |
||
| 215 | * @return $this |
||
| 216 | * @throws InvalidArgumentException |
||
| 217 | */ |
||
| 218 | public function setDayOfMonth($dayOfMonth) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * @return string |
||
| 233 | */ |
||
| 234 | public function getDayOfMonth() |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @param string $dayOfWeek |
||
| 241 | * |
||
| 242 | * @return $this |
||
| 243 | * @throws InvalidArgumentException |
||
| 244 | */ |
||
| 245 | public function setDayOfWeek($dayOfWeek) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @return string |
||
| 260 | */ |
||
| 261 | public function getDayOfWeek() |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @param string $hour |
||
| 268 | * |
||
| 269 | * @return $this |
||
| 270 | * @throws InvalidArgumentException |
||
| 271 | */ |
||
| 272 | View Code Duplication | public function setHour($hour) |
|
| 284 | |||
| 285 | /** |
||
| 286 | * @return string |
||
| 287 | */ |
||
| 288 | public function getHour() |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @param string $minute |
||
| 295 | * |
||
| 296 | * @return $this |
||
| 297 | * @throws InvalidArgumentException |
||
| 298 | */ |
||
| 299 | View Code Duplication | public function setMinute($minute) |
|
| 311 | |||
| 312 | /** |
||
| 313 | * @return string |
||
| 314 | */ |
||
| 315 | public function getMinute() |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @param string $month |
||
| 322 | * |
||
| 323 | * @return $this |
||
| 324 | * @throws InvalidArgumentException |
||
| 325 | */ |
||
| 326 | View Code Duplication | public function setMonth($month) |
|
| 338 | |||
| 339 | /** |
||
| 340 | * @return string |
||
| 341 | */ |
||
| 342 | public function getMonth() |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @param string $comment |
||
| 349 | * |
||
| 350 | * @return $this |
||
| 351 | */ |
||
| 352 | public function setComment($comment) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @return string |
||
| 361 | */ |
||
| 362 | public function getComment() |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @param string $logFile |
||
| 369 | * |
||
| 370 | * @return $this |
||
| 371 | */ |
||
| 372 | public function setLogFile($logFile) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * @return string |
||
| 381 | */ |
||
| 382 | public function getLogFile() |
||
| 386 | |||
| 387 | /** |
||
| 388 | * @param string $errorFile |
||
| 389 | * |
||
| 390 | * @return $this |
||
| 391 | */ |
||
| 392 | public function setErrorFile($errorFile) |
||
| 398 | |||
| 399 | /** |
||
| 400 | * @return string |
||
| 401 | */ |
||
| 402 | public function getErrorFile() |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @param int $lastRunTime |
||
| 409 | * |
||
| 410 | * @return $this |
||
| 411 | */ |
||
| 412 | public function setLastRunTime($lastRunTime) |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @return int |
||
| 421 | */ |
||
| 422 | public function getLastRunTime() |
||
| 426 | |||
| 427 | /** |
||
| 428 | * @param string $errorSize |
||
| 429 | * |
||
| 430 | * @return $this |
||
| 431 | */ |
||
| 432 | public function setErrorSize($errorSize) |
||
| 438 | |||
| 439 | /** |
||
| 440 | * @return string |
||
| 441 | */ |
||
| 442 | public function getErrorSize() |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @param string $logSize |
||
| 449 | * |
||
| 450 | * @return $this |
||
| 451 | */ |
||
| 452 | public function setLogSize($logSize) |
||
| 458 | |||
| 459 | /** |
||
| 460 | * @return string |
||
| 461 | */ |
||
| 462 | public function getLogSize() |
||
| 466 | |||
| 467 | /** |
||
| 468 | * @param string $status |
||
| 469 | * |
||
| 470 | * @return $this |
||
| 471 | */ |
||
| 472 | public function setStatus($status) |
||
| 478 | |||
| 479 | /** |
||
| 480 | * @return string |
||
| 481 | */ |
||
| 482 | public function getStatus() |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Concatenate time data to get the time expression |
||
| 489 | * |
||
| 490 | * @return string |
||
| 491 | */ |
||
| 492 | public function getExpression() |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Gets the value of isSuspended |
||
| 499 | * |
||
| 500 | * @return boolean |
||
| 501 | */ |
||
| 502 | public function isSuspended() |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Sets the value of isSuspended |
||
| 509 | * |
||
| 510 | * @param boolean $isSuspended status |
||
| 511 | * |
||
| 512 | * @return Cron |
||
| 513 | */ |
||
| 514 | public function setSuspended($isSuspended = true) |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Transforms the cron instance into a cron line |
||
| 525 | * |
||
| 526 | * @return string |
||
| 527 | */ |
||
| 528 | public function __toString() |
||
| 548 | } |
||
| 549 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: