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 | */ |
||
| 217 | public function setDayOfMonth($dayOfMonth) |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @return string |
||
| 226 | */ |
||
| 227 | public function getDayOfMonth() |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @param string $dayOfWeek |
||
| 234 | * @param $dayOfWeek |
||
| 235 | * |
||
| 236 | * @return $this |
||
| 237 | */ |
||
| 238 | public function setDayOfWeek($dayOfWeek) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @return string |
||
| 247 | */ |
||
| 248 | public function getDayOfWeek() |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param string $hour |
||
| 255 | * @param $hour |
||
| 256 | * |
||
| 257 | * @return $this |
||
| 258 | */ |
||
| 259 | View Code Duplication | public function setHour($hour) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * @return string |
||
| 274 | */ |
||
| 275 | public function getHour() |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @param string $minute |
||
| 282 | * |
||
| 283 | * @return $this |
||
| 284 | */ |
||
| 285 | View Code Duplication | public function setMinute($minute) |
|
| 297 | |||
| 298 | /** |
||
| 299 | * @return string |
||
| 300 | */ |
||
| 301 | public function getMinute() |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @param string $month |
||
| 308 | * |
||
| 309 | * @return $this |
||
| 310 | */ |
||
| 311 | View Code Duplication | public function setMonth($month) |
|
| 323 | |||
| 324 | /** |
||
| 325 | * @return string |
||
| 326 | */ |
||
| 327 | public function getMonth() |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @param string $comment |
||
| 334 | * |
||
| 335 | * @return $this |
||
| 336 | */ |
||
| 337 | public function setComment($comment) |
||
| 343 | |||
| 344 | /** |
||
| 345 | * @return string |
||
| 346 | */ |
||
| 347 | public function getComment() |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @param string $logFile |
||
| 354 | * |
||
| 355 | * @return $this |
||
| 356 | */ |
||
| 357 | public function setLogFile($logFile) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @return string |
||
| 366 | */ |
||
| 367 | public function getLogFile() |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @param string $errorFile |
||
| 374 | * |
||
| 375 | * @return $this |
||
| 376 | */ |
||
| 377 | public function setErrorFile($errorFile) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @return string |
||
| 386 | */ |
||
| 387 | public function getErrorFile() |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @param int $lastRunTime |
||
| 394 | * |
||
| 395 | * @return $this |
||
| 396 | */ |
||
| 397 | public function setLastRunTime($lastRunTime) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * @return int |
||
| 406 | */ |
||
| 407 | public function getLastRunTime() |
||
| 411 | |||
| 412 | /** |
||
| 413 | * @param string $errorSize |
||
| 414 | * |
||
| 415 | * @return $this |
||
| 416 | */ |
||
| 417 | public function setErrorSize($errorSize) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * @return string |
||
| 426 | */ |
||
| 427 | public function getErrorSize() |
||
| 431 | |||
| 432 | /** |
||
| 433 | * @param string $logSize |
||
| 434 | * |
||
| 435 | * @return $this |
||
| 436 | */ |
||
| 437 | public function setLogSize($logSize) |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @return string |
||
| 446 | */ |
||
| 447 | public function getLogSize() |
||
| 451 | |||
| 452 | /** |
||
| 453 | * @param string $status |
||
| 454 | * |
||
| 455 | * @return $this |
||
| 456 | */ |
||
| 457 | public function setStatus($status) |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @return string |
||
| 466 | */ |
||
| 467 | public function getStatus() |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Concatenate time data to get the time expression |
||
| 474 | * |
||
| 475 | * @return string |
||
| 476 | */ |
||
| 477 | public function getExpression() |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Gets the value of isSuspended |
||
| 484 | * |
||
| 485 | * @return boolean |
||
| 486 | */ |
||
| 487 | public function isSuspended() |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Sets the value of isSuspended |
||
| 494 | * |
||
| 495 | * @param boolean $isSuspended status |
||
| 496 | * |
||
| 497 | * @return Cron |
||
| 498 | */ |
||
| 499 | public function setSuspended($isSuspended = true) |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Transforms the cron instance into a cron line |
||
| 510 | * |
||
| 511 | * @return string |
||
| 512 | */ |
||
| 513 | public function __toString() |
||
| 533 | } |
||
| 534 |
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: