Complex classes like BaseEvent 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 BaseEvent, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | abstract class BaseEvent implements Event |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * The cron expression representing the event's frequency. |
||
| 14 | * @var string |
||
| 15 | */ |
||
| 16 | protected $expression = '* * * * * *'; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * The timezone the date should be evaluated on. |
||
| 20 | * @var \DateTimeZone|string |
||
| 21 | */ |
||
| 22 | protected $timezone; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * The array of filter callbacks. These must return true |
||
| 26 | * @var callable[] |
||
| 27 | */ |
||
| 28 | protected $filters = []; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * The array of callbacks to be run before the event is started. |
||
| 32 | * @var callable[] |
||
| 33 | */ |
||
| 34 | protected $beforeCallbacks = []; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The array of callbacks to be run after the event is finished. |
||
| 38 | * @var callable[] |
||
| 39 | */ |
||
| 40 | protected $afterCallbacks = []; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * The array of reject callbacks. |
||
| 44 | * @var callable[] |
||
| 45 | */ |
||
| 46 | protected $rejects = []; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | protected $description; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @param string $description |
||
| 55 | * @return $this |
||
| 56 | */ |
||
| 57 | public function describe($description) |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Schedule the event to run between start and end time. |
||
| 65 | * |
||
| 66 | * @param string $startTime |
||
| 67 | * @param string $endTime |
||
| 68 | * @return $this |
||
| 69 | */ |
||
| 70 | public function between($startTime, $endTime) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Schedule the event to not run between start and end time. |
||
| 77 | * |
||
| 78 | * @param string $startTime |
||
| 79 | * @param string $endTime |
||
| 80 | * @return $this |
||
| 81 | */ |
||
| 82 | public function notBetween($startTime, $endTime) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Schedule the event to run between start and end time. |
||
| 89 | * |
||
| 90 | * @param string $startTime |
||
| 91 | * @param string $endTime |
||
| 92 | * @return \Closure |
||
| 93 | */ |
||
| 94 | private function inTimeInterval($startTime, $endTime) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Set the timezone the date should be evaluated on. |
||
| 104 | * |
||
| 105 | * @return $this |
||
| 106 | */ |
||
| 107 | public function timezone(\DateTimeZone $timezone) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Determine if the given event should run based on the Cron expression. |
||
| 116 | * |
||
| 117 | * @return bool |
||
| 118 | */ |
||
| 119 | 2 | public function isDue() |
|
| 123 | |||
| 124 | /** |
||
| 125 | * Determine if the Cron expression passes. |
||
| 126 | * |
||
| 127 | * @return boolean |
||
| 128 | */ |
||
| 129 | 2 | protected function expressionPasses() |
|
| 139 | |||
| 140 | /** |
||
| 141 | * Determine if the filters pass for the event. |
||
| 142 | * |
||
| 143 | * @return boolean |
||
| 144 | */ |
||
| 145 | 2 | protected function filtersPass() |
|
| 161 | |||
| 162 | /** |
||
| 163 | * The Cron expression representing the event's frequency. |
||
| 164 | * |
||
| 165 | * @param string $expression |
||
| 166 | * @return $this |
||
| 167 | */ |
||
| 168 | 14 | public function cron(/* string */ $expression) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * @return CronExpression |
||
| 179 | */ |
||
| 180 | 14 | public function getCronExpression() |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Change the minute when the job should run (0-59, *, *\/2 etc) |
||
| 187 | * |
||
| 188 | * @param string|int $minute |
||
| 189 | * @return $this |
||
| 190 | */ |
||
| 191 | 4 | public function minute($minute) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Schedule the event to run every minute. |
||
| 198 | * |
||
| 199 | * @return $this |
||
| 200 | */ |
||
| 201 | 3 | public function everyMinute() |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Schedule this event to run every 5 minutes |
||
| 208 | * |
||
| 209 | * @return $this |
||
| 210 | */ |
||
| 211 | public function everyFiveMinutes() |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Schedule the event to run every N minutes |
||
| 218 | * |
||
| 219 | * @param int $n |
||
| 220 | * @return $this |
||
| 221 | */ |
||
| 222 | 1 | public function everyNMinutes(/* int */ $n) |
|
| 228 | |||
| 229 | /** |
||
| 230 | * Set the hour when the job should run (0-23, *, *\/2, etc) |
||
| 231 | * |
||
| 232 | * @param string|int $hour |
||
| 233 | * @return $this |
||
| 234 | */ |
||
| 235 | 1 | public function hour($hour) |
|
| 239 | |||
| 240 | /** |
||
| 241 | * Schedule the event to run hourly. |
||
| 242 | * |
||
| 243 | * @return $this |
||
| 244 | */ |
||
| 245 | 1 | public function hourly() |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Schedule the event to run daily. |
||
| 254 | * |
||
| 255 | * @return $this |
||
| 256 | */ |
||
| 257 | 1 | public function daily() |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Schedule the event to run daily at a given time (10:00, 19:30, etc). |
||
| 266 | * |
||
| 267 | * @param string $time |
||
| 268 | * @return $this |
||
| 269 | */ |
||
| 270 | 1 | public function dailyAt(/* string */ $time) |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Set the days of the week the command should run on. |
||
| 282 | * |
||
| 283 | * @param array|mixed $days |
||
| 284 | * @return $this |
||
| 285 | */ |
||
| 286 | 1 | public function days($days) |
|
| 292 | |||
| 293 | /** |
||
| 294 | * Schedule the event to run only on weekdays. |
||
| 295 | * |
||
| 296 | * @return $this |
||
| 297 | */ |
||
| 298 | 1 | public function weekdays() |
|
| 302 | |||
| 303 | /** |
||
| 304 | * Schedule the event to run only on Mondays. |
||
| 305 | * |
||
| 306 | * @return $this |
||
| 307 | */ |
||
| 308 | public function mondays() |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Schedule the event to run only on Tuesdays. |
||
| 315 | * |
||
| 316 | * @return $this |
||
| 317 | */ |
||
| 318 | public function tuesdays() |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Schedule the event to run only on Wednesdays. |
||
| 325 | * |
||
| 326 | * @return $this |
||
| 327 | */ |
||
| 328 | public function wednesdays() |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Schedule the event to run only on Thursdays. |
||
| 335 | * |
||
| 336 | * @return $this |
||
| 337 | */ |
||
| 338 | public function thursdays() |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Schedule the event to run only on Fridays. |
||
| 345 | * |
||
| 346 | * @return $this |
||
| 347 | */ |
||
| 348 | public function fridays() |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Schedule the event to run only on Saturdays. |
||
| 355 | * |
||
| 356 | * @return $this |
||
| 357 | */ |
||
| 358 | public function saturdays() |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Schedule the event to run only on Sundays. |
||
| 365 | * |
||
| 366 | * @return $this |
||
| 367 | */ |
||
| 368 | public function sundays() |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Schedule the event to run weekly. |
||
| 375 | * |
||
| 376 | * @return $this |
||
| 377 | */ |
||
| 378 | 1 | public function weekly() |
|
| 384 | |||
| 385 | /** |
||
| 386 | * Schedule the event to run weekly on a given day and time. |
||
| 387 | * |
||
| 388 | * @param int $day |
||
| 389 | * @param string $time |
||
| 390 | * @return $this |
||
| 391 | */ |
||
| 392 | public function weeklyOn($day, $time = '0:0') |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Schedule the event to run monthly. |
||
| 400 | * |
||
| 401 | * @return $this |
||
| 402 | */ |
||
| 403 | 1 | public function monthly() |
|
| 409 | |||
| 410 | /** |
||
| 411 | * Schedule the event to run monthly on a given day and time. |
||
| 412 | * |
||
| 413 | * @param int $day |
||
| 414 | * @param string $time |
||
| 415 | * @return $this |
||
| 416 | */ |
||
| 417 | public function monthlyOn($day = 1, $time = '0:0') |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Schedule the event to run quarterly. |
||
| 425 | * |
||
| 426 | * @return $this |
||
| 427 | */ |
||
| 428 | 1 | public function quarterly() |
|
| 435 | |||
| 436 | /** |
||
| 437 | * Schedule the event to run yearly. |
||
| 438 | * |
||
| 439 | * @return $this |
||
| 440 | */ |
||
| 441 | 1 | public function yearly() |
|
| 448 | |||
| 449 | /** |
||
| 450 | * Splice the given value into the given position of the expression. |
||
| 451 | * |
||
| 452 | * @param int $position |
||
| 453 | * @param string $value |
||
| 454 | * @return $this |
||
| 455 | */ |
||
| 456 | 14 | protected function spliceIntoPosition($position, $value) |
|
| 462 | |||
| 463 | |||
| 464 | /** |
||
| 465 | * Register a callback to further filter the schedule. |
||
| 466 | * |
||
| 467 | * @param callable $callback |
||
| 468 | * @return $this |
||
| 469 | */ |
||
| 470 | 1 | public function when(callable $callback) |
|
| 476 | |||
| 477 | /** |
||
| 478 | * Register a callback to further filter the schedule. |
||
| 479 | * |
||
| 480 | * @param callable $callback |
||
| 481 | * @return $this |
||
| 482 | */ |
||
| 483 | 1 | public function skip(callable $callback) |
|
| 489 | |||
| 490 | /** |
||
| 491 | * Register a callback to be called before the operation. |
||
| 492 | * |
||
| 493 | * @param callable $callback |
||
| 494 | * @return $this |
||
| 495 | */ |
||
| 496 | public function before(callable $callback) |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Register a callback to be called after the operation. |
||
| 505 | * |
||
| 506 | * @param callable $callback |
||
| 507 | * @return $this |
||
| 508 | */ |
||
| 509 | public function after(callable $callback) |
||
| 515 | } |
||
| 516 |
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.