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 |
||
| 17 | class Cron |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var string |
||
| 21 | * @CronAsserts\CronMinuteFormat |
||
| 22 | */ |
||
| 23 | protected $minute = '*'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var string |
||
| 27 | * @CronAsserts\CronHourFormat |
||
| 28 | */ |
||
| 29 | protected $hour = '*'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | * @CronAsserts\CronDayOfMonthFormat |
||
| 34 | */ |
||
| 35 | protected $dayOfMonth = '*'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var string |
||
| 39 | * @CronAsserts\CronMonthFormat |
||
| 40 | */ |
||
| 41 | protected $month = '*'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string |
||
| 45 | * @CronAsserts\CronDayOfWeekFormat |
||
| 46 | */ |
||
| 47 | protected $dayOfWeek = '*'; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var string |
||
| 51 | * @CronAsserts\CliCommandPath |
||
| 52 | */ |
||
| 53 | protected $command; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var string |
||
| 57 | * @CronAsserts\LogFile() |
||
| 58 | */ |
||
| 59 | protected $logFile = null; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * The size of the log file |
||
| 63 | * |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | protected $logSize = null; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var string |
||
| 70 | * @CronAsserts\LogFile() |
||
| 71 | */ |
||
| 72 | protected $errorFile = null; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * The size of the error file |
||
| 76 | * |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | protected $errorSize = null; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * The last run time based on when log files have been written |
||
| 83 | * |
||
| 84 | * @var int |
||
| 85 | */ |
||
| 86 | protected $lastRunTime = null; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * The status of the cron, based on the log files |
||
| 90 | * |
||
| 91 | * @var string |
||
| 92 | */ |
||
| 93 | protected $status; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var string |
||
| 97 | */ |
||
| 98 | protected $comment; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @var boolean |
||
| 102 | */ |
||
| 103 | protected $isSuspended = false; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Parses a cron line into a Cron instance |
||
| 107 | * |
||
| 108 | * @static |
||
| 109 | * |
||
| 110 | * @param string $cron The cron line |
||
| 111 | * |
||
| 112 | * @return Cron |
||
| 113 | */ |
||
| 114 | public static function parse($cron) |
||
| 115 | { |
||
| 116 | if (substr($cron, 0, 12) == '#suspended: ') { |
||
| 117 | $cron = substr($cron, 12); |
||
| 118 | $isSuspended = true; |
||
| 119 | } |
||
| 120 | |||
| 121 | $parts = explode(' ', $cron); |
||
| 122 | |||
| 123 | $command = implode(' ', array_slice($parts, 5)); |
||
| 124 | |||
| 125 | // extract comment |
||
| 126 | if (strpos($command, '#')) { |
||
| 127 | list($command, $comment) = explode('#', $command); |
||
| 128 | $comment = trim($comment); |
||
| 129 | } |
||
| 130 | |||
| 131 | // extract error file |
||
| 132 | if (strpos($command, '2>')) { |
||
| 133 | list($command, $errorFile) = explode('2>', $command); |
||
| 134 | $errorFile = trim($errorFile); |
||
| 135 | } |
||
| 136 | |||
| 137 | // extract log file |
||
| 138 | if (strpos($command, '>')) { |
||
| 139 | list($command, $logFile) = explode('>', $command); |
||
| 140 | $logFile = trim($logFile); |
||
| 141 | } |
||
| 142 | |||
| 143 | // compute last run time, and file size |
||
| 144 | $lastRunTime = null; |
||
| 145 | $logSize = null; |
||
| 146 | $errorSize = null; |
||
| 147 | if (isset($logFile) && file_exists($logFile)) { |
||
| 148 | $lastRunTime = filemtime($logFile); |
||
| 149 | $logSize = filesize($logFile); |
||
| 150 | } |
||
| 151 | if (isset($errorFile) && file_exists($errorFile)) { |
||
| 152 | $lastRunTime = max($lastRunTime ?: 0, filemtime($errorFile)); |
||
| 153 | $errorSize = filesize($errorFile); |
||
| 154 | } |
||
| 155 | |||
| 156 | // compute status |
||
| 157 | $status = 'error'; |
||
| 158 | if (!$logSize && !$errorSize) { |
||
| 159 | $status = 'unknown'; |
||
| 160 | } elseif (!$errorSize || $errorSize == 0) { |
||
| 161 | $status = 'success'; |
||
| 162 | } |
||
| 163 | |||
| 164 | // create cron instance |
||
| 165 | $cron = new self(); |
||
| 166 | $cron->setMinute($parts[0]) |
||
| 167 | ->setHour($parts[1]) |
||
| 168 | ->setDayOfMonth($parts[2]) |
||
| 169 | ->setMonth($parts[3]) |
||
| 170 | ->setDayOfWeek($parts[4]) |
||
| 171 | ->setCommand(\trim($command)) |
||
| 172 | ->setLastRunTime($lastRunTime) |
||
| 173 | ->setLogSize($logSize) |
||
| 174 | ->setErrorSize($errorSize) |
||
| 175 | ->setStatus($status); |
||
| 176 | |||
| 177 | if (isset($isSuspended)) { |
||
| 178 | $cron->setSuspended($isSuspended); |
||
| 179 | } |
||
| 180 | if (isset($comment)) { |
||
| 181 | $cron->setComment($comment); |
||
| 182 | } |
||
| 183 | if (isset($logFile)) { |
||
| 184 | $cron->setLogFile($logFile); |
||
| 185 | } |
||
| 186 | if (isset($errorFile)) { |
||
| 187 | $cron->setErrorFile($errorFile); |
||
| 188 | } |
||
| 189 | |||
| 190 | return $cron; |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @param string $command |
||
| 195 | * |
||
| 196 | * @return $this |
||
| 197 | */ |
||
| 198 | public function setCommand($command) |
||
| 199 | { |
||
| 200 | $this->command = $command; |
||
| 201 | |||
| 202 | return $this; |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @return string |
||
| 207 | */ |
||
| 208 | public function getCommand() |
||
| 209 | { |
||
| 210 | return $this->command; |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param string $dayOfMonth |
||
| 215 | * |
||
| 216 | * @return $this |
||
| 217 | */ |
||
| 218 | public function setDayOfMonth($dayOfMonth) |
||
| 219 | { |
||
| 220 | $this->dayOfMonth = $dayOfMonth; |
||
| 221 | |||
| 222 | return $this; |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @return string |
||
| 227 | */ |
||
| 228 | public function getDayOfMonth() |
||
| 229 | { |
||
| 230 | return $this->dayOfMonth; |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @param string $dayOfWeek |
||
| 235 | * |
||
| 236 | * @return $this |
||
| 237 | */ |
||
| 238 | public function setDayOfWeek($dayOfWeek) |
||
| 239 | { |
||
| 240 | $this->dayOfWeek = $dayOfWeek; |
||
| 241 | |||
| 242 | return $this; |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @return string |
||
| 247 | */ |
||
| 248 | public function getDayOfWeek() |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param string $hour |
||
| 255 | * |
||
| 256 | * @return $this |
||
| 257 | */ |
||
| 258 | public function setHour($hour) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @return string |
||
| 267 | */ |
||
| 268 | public function getHour() |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @param string $minute |
||
| 275 | * |
||
| 276 | * @return $this |
||
| 277 | */ |
||
| 278 | public function setMinute($minute) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @return string |
||
| 287 | */ |
||
| 288 | public function getMinute() |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @param string $month |
||
| 295 | * |
||
| 296 | * @return $this |
||
| 297 | */ |
||
| 298 | public function setMonth($month) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @return string |
||
| 307 | */ |
||
| 308 | public function getMonth() |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @param string $comment |
||
| 315 | * |
||
| 316 | * @return $this |
||
| 317 | */ |
||
| 318 | public function setComment($comment) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @return string |
||
| 327 | */ |
||
| 328 | public function getComment() |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @param string $logFile |
||
| 335 | * |
||
| 336 | * @return $this |
||
| 337 | */ |
||
| 338 | public function setLogFile($logFile) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @return string |
||
| 347 | */ |
||
| 348 | public function getLogFile() |
||
| 352 | |||
| 353 | /** |
||
| 354 | * @param string $errorFile |
||
| 355 | * |
||
| 356 | * @return $this |
||
| 357 | */ |
||
| 358 | public function setErrorFile($errorFile) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * @return string |
||
| 367 | */ |
||
| 368 | public function getErrorFile() |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @param int $lastRunTime |
||
| 375 | * |
||
| 376 | * @return $this |
||
| 377 | */ |
||
| 378 | public function setLastRunTime($lastRunTime) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @return int |
||
| 387 | */ |
||
| 388 | public function getLastRunTime() |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @param string $errorSize |
||
| 395 | * |
||
| 396 | * @return $this |
||
| 397 | */ |
||
| 398 | public function setErrorSize($errorSize) |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @return string |
||
| 407 | */ |
||
| 408 | public function getErrorSize() |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @param string $logSize |
||
| 415 | * |
||
| 416 | * @return $this |
||
| 417 | */ |
||
| 418 | public function setLogSize($logSize) |
||
| 424 | |||
| 425 | /** |
||
| 426 | * @return string |
||
| 427 | */ |
||
| 428 | public function getLogSize() |
||
| 432 | |||
| 433 | /** |
||
| 434 | * @param string $status |
||
| 435 | * |
||
| 436 | * @return $this |
||
| 437 | */ |
||
| 438 | public function setStatus($status) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * @return string |
||
| 447 | */ |
||
| 448 | public function getStatus() |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Concatenate time data to get the time expression |
||
| 455 | * |
||
| 456 | * @return string |
||
| 457 | */ |
||
| 458 | public function getExpression() |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Gets the value of isSuspended |
||
| 465 | * |
||
| 466 | * @return boolean |
||
| 467 | */ |
||
| 468 | public function isSuspended() |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Sets the value of isSuspended |
||
| 475 | * |
||
| 476 | * @param boolean $isSuspended status |
||
| 477 | * |
||
| 478 | * @return Cron |
||
| 479 | */ |
||
| 480 | public function setSuspended($isSuspended = true) |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Transforms the cron instance into a cron line |
||
| 491 | * |
||
| 492 | * @return string |
||
| 493 | */ |
||
| 494 | public function __toString() |
||
| 514 | } |
||
| 515 |
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: