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 | * @var string|null |
||
14 | */ |
||
15 | protected $name; |
||
16 | |||
17 | /** |
||
18 | * The cron expression representing the event's frequency. |
||
19 | * @var string |
||
20 | */ |
||
21 | protected $expression = '* * * * * *'; |
||
22 | |||
23 | /** |
||
24 | * The timezone the date should be evaluated on. |
||
25 | * @var \DateTimeZone|string |
||
26 | */ |
||
27 | protected $timezone; |
||
28 | |||
29 | /** |
||
30 | * The array of filter callbacks. These must return true |
||
31 | * @var callable[] |
||
32 | */ |
||
33 | protected $filters = []; |
||
34 | |||
35 | /** |
||
36 | * The array of callbacks to be run before the event is started. |
||
37 | * @var callable[] |
||
38 | */ |
||
39 | protected $beforeCallbacks = []; |
||
40 | |||
41 | /** |
||
42 | * The array of callbacks to be run after the event is finished. |
||
43 | * @var callable[] |
||
44 | */ |
||
45 | protected $afterCallbacks = []; |
||
46 | |||
47 | /** |
||
48 | * The array of reject callbacks. |
||
49 | * @var callable[] |
||
50 | */ |
||
51 | protected $rejects = []; |
||
52 | |||
53 | /** |
||
54 | * @var string |
||
55 | */ |
||
56 | protected $description; |
||
57 | |||
58 | /** |
||
59 | * If you want to run a single event, please give it a name |
||
60 | * |
||
61 | * @return null|string |
||
62 | */ |
||
63 | public function getName() |
||
67 | |||
68 | /** |
||
69 | * @param string $name |
||
70 | * @return $this |
||
71 | */ |
||
72 | public function name($name) |
||
77 | |||
78 | /** |
||
79 | * @param string $description |
||
80 | * @return $this |
||
81 | */ |
||
82 | public function describe($description) |
||
87 | |||
88 | /** |
||
89 | * Schedule the event to run between start and end time. |
||
90 | * |
||
91 | * @param string $startTime |
||
92 | * @param string $endTime |
||
93 | * @return $this |
||
94 | */ |
||
95 | public function between($startTime, $endTime) |
||
99 | |||
100 | /** |
||
101 | * Schedule the event to not run between start and end time. |
||
102 | * |
||
103 | * @param string $startTime |
||
104 | * @param string $endTime |
||
105 | * @return $this |
||
106 | */ |
||
107 | public function notBetween($startTime, $endTime) |
||
111 | |||
112 | /** |
||
113 | * Schedule the event to run between start and end time. |
||
114 | * |
||
115 | * @param string $startTime |
||
116 | * @param string $endTime |
||
117 | * @return \Closure |
||
118 | */ |
||
119 | private function inTimeInterval($startTime, $endTime) |
||
126 | |||
127 | /** |
||
128 | * Set the timezone the date should be evaluated on. |
||
129 | * |
||
130 | * @return $this |
||
131 | */ |
||
132 | public function timezone(\DateTimeZone $timezone) |
||
138 | |||
139 | /** |
||
140 | * Determine if the given event should run based on the Cron expression. |
||
141 | * |
||
142 | * @return bool |
||
143 | */ |
||
144 | 2 | public function isDue() |
|
148 | |||
149 | /** |
||
150 | * Determine if the Cron expression passes. |
||
151 | * |
||
152 | * @return boolean |
||
153 | */ |
||
154 | 2 | protected function expressionPasses() |
|
164 | |||
165 | /** |
||
166 | * Determine if the filters pass for the event. |
||
167 | * |
||
168 | * @return boolean |
||
169 | */ |
||
170 | 2 | protected function filtersPass() |
|
186 | |||
187 | /** |
||
188 | * The Cron expression representing the event's frequency. |
||
189 | * |
||
190 | * @param string $expression |
||
191 | * @return $this |
||
192 | */ |
||
193 | 14 | public function cron(/* string */ $expression) |
|
201 | |||
202 | /** |
||
203 | * @return CronExpression |
||
204 | */ |
||
205 | 14 | public function getCronExpression() |
|
209 | |||
210 | /** |
||
211 | * Change the minute when the job should run (0-59, *, *\/2 etc) |
||
212 | * |
||
213 | * @param string|int $minute |
||
214 | * @return $this |
||
215 | */ |
||
216 | 4 | public function minute($minute) |
|
220 | |||
221 | /** |
||
222 | * Schedule the event to run every minute. |
||
223 | * |
||
224 | * @return $this |
||
225 | */ |
||
226 | 3 | public function everyMinute() |
|
230 | |||
231 | /** |
||
232 | * Schedule this event to run every 5 minutes |
||
233 | * |
||
234 | * @return $this |
||
235 | */ |
||
236 | public function everyFiveMinutes() |
||
240 | |||
241 | /** |
||
242 | * Schedule the event to run every N minutes |
||
243 | * |
||
244 | * @param int $n |
||
245 | * @return $this |
||
246 | */ |
||
247 | 1 | public function everyNMinutes(/* int */ $n) |
|
253 | |||
254 | /** |
||
255 | * Set the hour when the job should run (0-23, *, *\/2, etc) |
||
256 | * |
||
257 | * @param string|int $hour |
||
258 | * @return $this |
||
259 | */ |
||
260 | 1 | public function hour($hour) |
|
264 | |||
265 | /** |
||
266 | * Schedule the event to run hourly. |
||
267 | * |
||
268 | * @return $this |
||
269 | */ |
||
270 | 1 | public function hourly() |
|
276 | |||
277 | /** |
||
278 | * Schedule the event to run daily. |
||
279 | * |
||
280 | * @return $this |
||
281 | */ |
||
282 | 1 | public function daily() |
|
288 | |||
289 | /** |
||
290 | * Schedule the event to run daily at a given time (10:00, 19:30, etc). |
||
291 | * |
||
292 | * @param string $time |
||
293 | * @return $this |
||
294 | */ |
||
295 | 1 | public function dailyAt(/* string */ $time) |
|
304 | |||
305 | /** |
||
306 | * Set the days of the week the command should run on. |
||
307 | * |
||
308 | * @param array|mixed $days |
||
309 | * @return $this |
||
310 | */ |
||
311 | 1 | public function days($days) |
|
317 | |||
318 | /** |
||
319 | * Schedule the event to run only on weekdays. |
||
320 | * |
||
321 | * @return $this |
||
322 | */ |
||
323 | 1 | public function weekdays() |
|
327 | |||
328 | /** |
||
329 | * Schedule the event to run only on Mondays. |
||
330 | * |
||
331 | * @return $this |
||
332 | */ |
||
333 | public function mondays() |
||
337 | |||
338 | /** |
||
339 | * Schedule the event to run only on Tuesdays. |
||
340 | * |
||
341 | * @return $this |
||
342 | */ |
||
343 | public function tuesdays() |
||
347 | |||
348 | /** |
||
349 | * Schedule the event to run only on Wednesdays. |
||
350 | * |
||
351 | * @return $this |
||
352 | */ |
||
353 | public function wednesdays() |
||
357 | |||
358 | /** |
||
359 | * Schedule the event to run only on Thursdays. |
||
360 | * |
||
361 | * @return $this |
||
362 | */ |
||
363 | public function thursdays() |
||
367 | |||
368 | /** |
||
369 | * Schedule the event to run only on Fridays. |
||
370 | * |
||
371 | * @return $this |
||
372 | */ |
||
373 | public function fridays() |
||
377 | |||
378 | /** |
||
379 | * Schedule the event to run only on Saturdays. |
||
380 | * |
||
381 | * @return $this |
||
382 | */ |
||
383 | public function saturdays() |
||
387 | |||
388 | /** |
||
389 | * Schedule the event to run only on Sundays. |
||
390 | * |
||
391 | * @return $this |
||
392 | */ |
||
393 | public function sundays() |
||
397 | |||
398 | /** |
||
399 | * Schedule the event to run weekly. |
||
400 | * |
||
401 | * @return $this |
||
402 | */ |
||
403 | 1 | public function weekly() |
|
409 | |||
410 | /** |
||
411 | * Schedule the event to run weekly on a given day and time. |
||
412 | * |
||
413 | * @param int $day |
||
414 | * @param string $time |
||
415 | * @return $this |
||
416 | */ |
||
417 | public function weeklyOn($day, $time = '0:0') |
||
422 | |||
423 | /** |
||
424 | * Schedule the event to run monthly. |
||
425 | * |
||
426 | * @return $this |
||
427 | */ |
||
428 | 1 | public function monthly() |
|
434 | |||
435 | /** |
||
436 | * Schedule the event to run monthly on a given day and time. |
||
437 | * |
||
438 | * @param int $day |
||
439 | * @param string $time |
||
440 | * @return $this |
||
441 | */ |
||
442 | public function monthlyOn($day = 1, $time = '0:0') |
||
447 | |||
448 | /** |
||
449 | * Schedule the event to run quarterly. |
||
450 | * |
||
451 | * @return $this |
||
452 | */ |
||
453 | 1 | public function quarterly() |
|
460 | |||
461 | /** |
||
462 | * Schedule the event to run yearly. |
||
463 | * |
||
464 | * @return $this |
||
465 | */ |
||
466 | 1 | public function yearly() |
|
473 | |||
474 | /** |
||
475 | * Splice the given value into the given position of the expression. |
||
476 | * |
||
477 | * @param int $position |
||
478 | * @param string $value |
||
479 | * @return $this |
||
480 | */ |
||
481 | 14 | protected function spliceIntoPosition($position, $value) |
|
487 | |||
488 | |||
489 | /** |
||
490 | * Register a callback to further filter the schedule. |
||
491 | * |
||
492 | * @param callable $callback |
||
493 | * @return $this |
||
494 | */ |
||
495 | 1 | public function when(callable $callback) |
|
501 | |||
502 | /** |
||
503 | * Register a callback to further filter the schedule. |
||
504 | * |
||
505 | * @param callable $callback |
||
506 | * @return $this |
||
507 | */ |
||
508 | 1 | public function skip(callable $callback) |
|
514 | |||
515 | /** |
||
516 | * Register a callback to be called before the operation. |
||
517 | * |
||
518 | * @param callable $callback |
||
519 | * @return $this |
||
520 | */ |
||
521 | public function before(callable $callback) |
||
527 | |||
528 | /** |
||
529 | * Register a callback to be called after the operation. |
||
530 | * |
||
531 | * @param callable $callback |
||
532 | * @return $this |
||
533 | */ |
||
534 | public function after(callable $callback) |
||
540 | } |
||
541 |
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.