Complex classes like TaskBag 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 TaskBag, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class TaskBag implements TaskBagInterface |
||
12 | { |
||
13 | /** @var string Task */ |
||
14 | protected $task; |
||
15 | |||
16 | /** @var array */ |
||
17 | protected $environments = [TaskPlanner::PRODUCTION_ENVIRONMENT, TaskPlanner::DEVELOPMENT_ENVIRONMENT]; |
||
18 | |||
19 | /** @var array|string Tasks that must follow this one */ |
||
20 | protected $followedBy = []; |
||
21 | |||
22 | /** @var CronExpression */ |
||
23 | protected $cronExpression; |
||
24 | |||
25 | /* Expressions */ |
||
26 | /** @var bool|string */ |
||
27 | protected $interval = false; // @daily, @yearly |
||
28 | |||
29 | /** @var null|string|int|array */ |
||
30 | protected $minute = null; |
||
31 | |||
32 | /** @var null|string|int|array */ |
||
33 | protected $hour = null; |
||
34 | |||
35 | /** @var null|string|int|array */ |
||
36 | protected $month = null; // 12 |
||
37 | |||
38 | /** @var null|string|int|array */ |
||
39 | protected $day = null; // 25 |
||
40 | |||
41 | /** @var null|string|int|array */ |
||
42 | protected $weekday = null; |
||
43 | |||
44 | /* Dependencies */ |
||
45 | /** @var ExpressionBuilderInterface */ |
||
46 | protected $expressionBuilder; |
||
47 | |||
48 | |||
49 | /** |
||
50 | * TaskBag constructor. |
||
51 | * @param string $task |
||
52 | */ |
||
53 | 40 | public function __construct($task = null) |
|
67 | |||
68 | /** |
||
69 | * @return string |
||
70 | */ |
||
71 | 4 | public function getTask() |
|
75 | |||
76 | /** |
||
77 | * @param string $task |
||
78 | * @return $this |
||
79 | */ |
||
80 | 29 | public function setTask($task) |
|
85 | |||
86 | /** |
||
87 | * @return bool|string |
||
88 | */ |
||
89 | 14 | public function getInterval() |
|
93 | |||
94 | /** |
||
95 | * @param string $interval |
||
96 | * @return $this |
||
97 | */ |
||
98 | 15 | public function setInterval($interval) |
|
103 | |||
104 | /** |
||
105 | * Parses a time from format 12:14 |
||
106 | * @param $time |
||
107 | * @return $this |
||
108 | */ |
||
109 | 5 | public function setTime($time) |
|
117 | |||
118 | /** |
||
119 | * @param string $time |
||
120 | * @return $this |
||
121 | */ |
||
122 | 12 | public function addTime($time) |
|
130 | |||
131 | /** |
||
132 | * Parses a time from formats 11/15 or 11-15 |
||
133 | * @param string $date |
||
134 | * @return $this |
||
135 | */ |
||
136 | 6 | public function setDate($date) |
|
144 | |||
145 | /** |
||
146 | * @param string $date |
||
147 | * @return $this |
||
148 | */ |
||
149 | 4 | public function addDate($date) |
|
153 | |||
154 | /** |
||
155 | * @param integer $month |
||
156 | * @return $this |
||
157 | */ |
||
158 | 8 | public function setMonth($month) |
|
163 | |||
164 | /** |
||
165 | * @return int|null|string |
||
166 | */ |
||
167 | 14 | public function getMonth() |
|
171 | |||
172 | /** |
||
173 | * @param string|int|array $month |
||
174 | * @return $this |
||
175 | */ |
||
176 | 5 | public function addMonth($month) |
|
181 | |||
182 | /** |
||
183 | * @param integer $day |
||
184 | * @return $this |
||
185 | */ |
||
186 | 10 | public function setDay($day) |
|
191 | |||
192 | /** |
||
193 | * @return int|null|string |
||
194 | */ |
||
195 | 14 | public function getDay() |
|
199 | |||
200 | /** |
||
201 | * @param string|int|array $day |
||
202 | * @return $this |
||
203 | */ |
||
204 | 7 | public function addDay($day) |
|
209 | |||
210 | /** |
||
211 | * @param string|int|array $minute |
||
212 | * @return $this |
||
213 | * @throws \Exception |
||
214 | */ |
||
215 | 8 | public function setMinute($minute) |
|
220 | |||
221 | /** |
||
222 | * @return int|null|string |
||
223 | */ |
||
224 | 14 | public function getMinute() |
|
228 | |||
229 | /** |
||
230 | * @param string|int|array $minute |
||
231 | * @return $this |
||
232 | */ |
||
233 | 13 | public function addMinute($minute) |
|
238 | |||
239 | /** |
||
240 | * @param string|int|array $hour |
||
241 | * @return $this |
||
242 | */ |
||
243 | 8 | public function setHour($hour) |
|
248 | |||
249 | /** |
||
250 | * @return int|null|string |
||
251 | */ |
||
252 | 14 | public function getHour() |
|
256 | |||
257 | /** |
||
258 | * @param string|int|array $hour |
||
259 | * @return $this |
||
260 | */ |
||
261 | 13 | public function addHour($hour) |
|
266 | |||
267 | /** |
||
268 | * @param string|int|array $weekday |
||
269 | * @return $this |
||
270 | */ |
||
271 | 2 | public function setWeekday($weekday) |
|
276 | |||
277 | /** |
||
278 | * @return int|null|string |
||
279 | */ |
||
280 | 12 | public function getWeekday() |
|
284 | |||
285 | /** |
||
286 | * @param string|int|array $weekday |
||
287 | * @return $this |
||
288 | */ |
||
289 | 3 | public function addWeekday($weekday) |
|
294 | |||
295 | 5 | public function setEnvironments($environments) |
|
304 | |||
305 | /** |
||
306 | * @param string $environment |
||
307 | * @return $this |
||
308 | */ |
||
309 | 2 | public function addEnvironment($environment) |
|
314 | |||
315 | /** |
||
316 | * @return array |
||
317 | */ |
||
318 | 4 | public function getEnvironments() |
|
322 | |||
323 | /** |
||
324 | * @param string $task |
||
325 | * @return $this |
||
326 | */ |
||
327 | 7 | public function addFollowedBy($task) |
|
332 | |||
333 | /** |
||
334 | * @return array |
||
335 | */ |
||
336 | 3 | public function getFollowedBy() |
|
340 | |||
341 | /** |
||
342 | * @param string $followedBy |
||
343 | * @return $this |
||
344 | */ |
||
345 | 3 | public function setFollowedBy($followedBy) |
|
350 | |||
351 | /** |
||
352 | * @param string|CronExpression $cronExpression |
||
353 | * @return $this |
||
354 | */ |
||
355 | 16 | public function setCronExpression($cronExpression) |
|
363 | |||
364 | /** |
||
365 | * @return CronExpression |
||
366 | */ |
||
367 | 12 | public function getCronExpression() |
|
371 | |||
372 | /** |
||
373 | * @return CronExpression |
||
374 | */ |
||
375 | 8 | protected function buildExpression() |
|
381 | |||
382 | /** |
||
383 | * @param ExpressionBuilderInterface $expressionBuilder |
||
384 | */ |
||
385 | 1 | public function setExpressionBuilder(ExpressionBuilderInterface $expressionBuilder) |
|
389 | |||
390 | /** |
||
391 | * @return ExpressionBuilder|ExpressionBuilderInterface |
||
392 | */ |
||
393 | 8 | protected function getExpressionBuilder() |
|
397 | |||
398 | |||
399 | /* Just passed through to CronExpression */ |
||
400 | /** |
||
401 | * @param string $currentTime |
||
402 | * @return bool |
||
403 | */ |
||
404 | 2 | public function isDue($currentTime = 'now') |
|
408 | |||
409 | /** |
||
410 | * @param string $currentTime |
||
411 | * @param int $nth |
||
412 | * @param bool $allowCurrentDate |
||
413 | */ |
||
414 | public function getNextRunDate($currentTime = 'now', $nth = 0, $allowCurrentDate = false) |
||
418 | |||
419 | /** |
||
420 | * @param string $currentTime |
||
421 | * @param int $nth |
||
422 | * @param bool $allowCurrentDate |
||
423 | */ |
||
424 | public function getPreviousRunDate($currentTime = 'now', $nth = 0, $allowCurrentDate = false) |
||
428 | |||
429 | /* Internals */ |
||
430 | /** |
||
431 | * @param $time |
||
432 | * @return array |
||
433 | */ |
||
434 | 14 | protected function parseTime($time) |
|
439 | |||
440 | /** |
||
441 | * @param string $date |
||
442 | * @return array |
||
443 | */ |
||
444 | 6 | protected function parseDate($date) |
|
454 | |||
455 | /** |
||
456 | * @param string $key |
||
457 | * @param $value |
||
458 | * @param string $deliminator |
||
459 | */ |
||
460 | 20 | protected function appendValue($key, $value, $deliminator = ',') |
|
472 | |||
473 | /** |
||
474 | * @param $value |
||
475 | * @return array |
||
476 | * @internal param $minute |
||
477 | */ |
||
478 | 20 | protected function forceToArray($value) |
|
486 | } |
||
487 |