Complex classes like Job 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 Job, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class Job |
||
6 | { |
||
7 | const STATUS_SUCCESS = 'success'; |
||
8 | const STATUS_ERROR = 'error'; |
||
9 | const STATUS_NEW = 'new'; |
||
10 | const STATUS_RUNNING = 'running'; |
||
11 | |||
12 | protected $id; |
||
13 | protected $workerName; |
||
14 | protected $className; |
||
15 | protected $method; |
||
16 | protected $args; |
||
17 | protected $batch; |
||
18 | protected $status; |
||
19 | protected $message; |
||
20 | protected $priority; |
||
21 | protected $crcHash; |
||
22 | protected $locked; |
||
23 | protected $lockedAt; |
||
24 | protected $whenAt; |
||
25 | protected $expiresAt; |
||
26 | protected $createdAt; |
||
27 | protected $updatedAt; |
||
28 | protected $delay; |
||
29 | protected $startedAt; |
||
30 | protected $finishedAt; |
||
31 | protected $maxDuration; |
||
32 | protected $elapsed; |
||
33 | protected $runId; |
||
34 | protected $worker; |
||
35 | |||
36 | public function __construct(Worker $worker = null, $batch = false, $priority = 10, \DateTime $whenAt = null) |
||
50 | |||
51 | public function __call($method, $args) |
||
67 | |||
68 | /** |
||
69 | * @var JobManagerInterface |
||
70 | */ |
||
71 | protected $jobManager; |
||
72 | |||
73 | /** |
||
74 | * @return string|null |
||
75 | */ |
||
76 | public function getMessage() |
||
80 | |||
81 | /** |
||
82 | * @param string $message |
||
83 | */ |
||
84 | public function setMessage($message) |
||
88 | |||
89 | /** |
||
90 | * @return string The status of the job |
||
91 | */ |
||
92 | public function getStatus() |
||
96 | |||
97 | /** |
||
98 | * @return bool|null |
||
99 | */ |
||
100 | public function getLocked() |
||
104 | |||
105 | /** |
||
106 | * @return \DateTime|null |
||
107 | */ |
||
108 | public function getLockedAt() |
||
112 | |||
113 | /** |
||
114 | * @return \DateTime|null |
||
115 | */ |
||
116 | public function getExpiresAt() |
||
120 | |||
121 | /** |
||
122 | * @param string $status The status of the job |
||
123 | */ |
||
124 | public function setStatus($status) |
||
128 | |||
129 | /** |
||
130 | * @param bool|null $locked |
||
131 | */ |
||
132 | public function setLocked($locked) |
||
136 | |||
137 | /** |
||
138 | * @param \DateTime|null $lockedAt |
||
139 | */ |
||
140 | public function setLockedAt($lockedAt) |
||
144 | |||
145 | /** |
||
146 | * @param \DateTime $expiresAt |
||
147 | */ |
||
148 | public function setExpiresAt(\DateTime $expiresAt) |
||
152 | |||
153 | /** |
||
154 | * @return int |
||
155 | */ |
||
156 | public function getId() |
||
160 | |||
161 | /** |
||
162 | * @return string |
||
163 | */ |
||
164 | public function getWorkerName() |
||
168 | |||
169 | /** |
||
170 | * @return string |
||
171 | */ |
||
172 | public function getClassName() |
||
176 | |||
177 | /** |
||
178 | * @return string |
||
179 | */ |
||
180 | public function getMethod() |
||
184 | |||
185 | /** |
||
186 | * @return mixed |
||
187 | */ |
||
188 | public function getArgs() |
||
192 | |||
193 | /** |
||
194 | * @return bool |
||
195 | */ |
||
196 | public function getBatch() |
||
200 | |||
201 | /** |
||
202 | * @return int |
||
203 | */ |
||
204 | public function getPriority() |
||
208 | |||
209 | /** |
||
210 | * @return string |
||
211 | */ |
||
212 | public function getCrcHash() |
||
216 | |||
217 | /** |
||
218 | * @return \DateTime|null |
||
219 | */ |
||
220 | public function getWhenAt() |
||
224 | |||
225 | /** |
||
226 | * @return \DateTime |
||
227 | */ |
||
228 | public function getCreatedAt() |
||
232 | |||
233 | /** |
||
234 | * @return \DateTime |
||
235 | */ |
||
236 | public function getUpdatedAt() |
||
240 | |||
241 | /** |
||
242 | * @return JobManagerInterface |
||
243 | */ |
||
244 | public function getJobManager() |
||
248 | |||
249 | /** |
||
250 | * @param mixed $id |
||
251 | */ |
||
252 | public function setId($id) |
||
256 | |||
257 | /** |
||
258 | * @param string $workerName |
||
259 | */ |
||
260 | public function setWorkerName($workerName) |
||
264 | |||
265 | /** |
||
266 | * @param string $className |
||
267 | */ |
||
268 | public function setClassName($className) |
||
272 | |||
273 | /** |
||
274 | * @param string $method |
||
275 | */ |
||
276 | public function setMethod($method) |
||
280 | |||
281 | /** |
||
282 | * @return \DateTime|null |
||
283 | */ |
||
284 | public function getStartedAt() |
||
288 | |||
289 | /** |
||
290 | * @param \DateTime $startedAt |
||
291 | */ |
||
292 | public function setStartedAt(\DateTime $startedAt) |
||
296 | |||
297 | /** |
||
298 | * @return \DateTime|null |
||
299 | */ |
||
300 | public function getFinishedAt() |
||
304 | |||
305 | /** |
||
306 | * @param \DateTime|null $finishedAt |
||
307 | */ |
||
308 | public function setFinishedAt($finishedAt) |
||
312 | |||
313 | /** |
||
314 | * @return int|null |
||
315 | */ |
||
316 | public function getMaxDuration() |
||
320 | |||
321 | /** |
||
322 | * @param int|null $maxDuration |
||
323 | */ |
||
324 | public function setMaxDuration($maxDuration) |
||
328 | |||
329 | /** |
||
330 | * @param $args |
||
331 | */ |
||
332 | public function setArgs($args) |
||
340 | |||
341 | protected function validateArgs($args) |
||
355 | |||
356 | /** |
||
357 | * @param bool $batch |
||
358 | */ |
||
359 | public function setBatch($batch) |
||
363 | |||
364 | /** |
||
365 | * @param int $priority |
||
366 | */ |
||
367 | public function setPriority($priority) |
||
371 | |||
372 | /** |
||
373 | * @param string $crcHash |
||
374 | */ |
||
375 | public function setCrcHash($crcHash) |
||
379 | |||
380 | /** |
||
381 | * @param \DateTime $whenAt |
||
382 | */ |
||
383 | public function setWhenAt(\DateTime $whenAt) |
||
387 | |||
388 | /** |
||
389 | * @param \DateTime $createdAt |
||
390 | */ |
||
391 | public function setCreatedAt(\DateTime $createdAt) |
||
395 | |||
396 | /** |
||
397 | * @param \DateTime $updatedAt |
||
398 | */ |
||
399 | public function setUpdatedAt(\DateTime $updatedAt) |
||
403 | |||
404 | /** |
||
405 | * @param JobManagerInterface $jobManager |
||
406 | */ |
||
407 | public function setJobManager(JobManagerInterface $jobManager) |
||
411 | |||
412 | /** |
||
413 | * @return int |
||
414 | */ |
||
415 | public function getDelay() |
||
419 | |||
420 | /** |
||
421 | * @return Worker |
||
422 | */ |
||
423 | public function getWorker() |
||
427 | |||
428 | /** |
||
429 | * @param int $delay Delay in seconds |
||
430 | */ |
||
431 | public function setDelay($delay) |
||
435 | |||
436 | /** |
||
437 | * @param Worker $worker |
||
438 | */ |
||
439 | public function setWorker($worker) |
||
443 | |||
444 | /** |
||
445 | * @return int |
||
446 | */ |
||
447 | public function getElapsed() |
||
451 | |||
452 | /** |
||
453 | * @param int $elapsed |
||
454 | */ |
||
455 | public function setElapsed($elapsed) |
||
459 | |||
460 | /** |
||
461 | * @return mixed |
||
462 | */ |
||
463 | public function getRunId() |
||
467 | |||
468 | /** |
||
469 | * @param mixed $runId |
||
470 | */ |
||
471 | public function setRunId($runId) |
||
475 | |||
476 | protected function toMessageArray() |
||
485 | |||
486 | /** |
||
487 | * @return string A json_encoded version of a queueable version of the object |
||
488 | */ |
||
489 | public function toMessage() |
||
493 | |||
494 | /** |
||
495 | * @param string $message a json_encoded version of the object |
||
496 | */ |
||
497 | public function fromMessage($message) |
||
504 | |||
505 | protected function fromMessageArray(array $arr) |
||
526 | } |
||
527 |