Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 | |||
35 | /** |
||
36 | * @var JobManagerInterface |
||
37 | */ |
||
38 | protected $jobManager; |
||
39 | |||
40 | /** |
||
41 | * @return string|null |
||
42 | */ |
||
43 | public function getMessage() |
||
47 | |||
48 | /** |
||
49 | * @param string $message |
||
50 | */ |
||
51 | public function setMessage($message) |
||
55 | |||
56 | /** |
||
57 | * @return string The status of the job |
||
58 | */ |
||
59 | public function getStatus() |
||
63 | |||
64 | /** |
||
65 | * @return bool|null |
||
66 | */ |
||
67 | public function getLocked() |
||
71 | |||
72 | /** |
||
73 | * @return \DateTime|null |
||
74 | */ |
||
75 | public function getLockedAt() |
||
79 | |||
80 | /** |
||
81 | * @return \DateTime|null |
||
82 | */ |
||
83 | public function getExpiresAt() |
||
87 | |||
88 | /** |
||
89 | * @param string $status The status of the job |
||
90 | */ |
||
91 | public function setStatus($status) |
||
95 | |||
96 | /** |
||
97 | * @param bool|null $locked |
||
98 | */ |
||
99 | public function setLocked($locked) |
||
103 | |||
104 | /** |
||
105 | * @param \DateTime|null $lockedAt |
||
106 | */ |
||
107 | public function setLockedAt($lockedAt) |
||
111 | |||
112 | /** |
||
113 | * @param \DateTime $expiresAt |
||
114 | */ |
||
115 | public function setExpiresAt(\DateTime $expiresAt) |
||
119 | |||
120 | /** |
||
121 | * @return int |
||
122 | */ |
||
123 | public function getId() |
||
127 | |||
128 | /** |
||
129 | * @return string |
||
130 | */ |
||
131 | public function getWorkerName() |
||
135 | |||
136 | /** |
||
137 | * @return string |
||
138 | */ |
||
139 | public function getClassName() |
||
143 | |||
144 | /** |
||
145 | * @return string |
||
146 | */ |
||
147 | public function getMethod() |
||
151 | |||
152 | /** |
||
153 | * @return |
||
154 | */ |
||
155 | public function getArgs() |
||
159 | |||
160 | /** |
||
161 | * @return bool |
||
162 | */ |
||
163 | public function getBatch() |
||
167 | |||
168 | /** |
||
169 | * @return int |
||
170 | */ |
||
171 | public function getPriority() |
||
175 | |||
176 | /** |
||
177 | * @return string |
||
178 | */ |
||
179 | public function getCrcHash() |
||
183 | |||
184 | /** |
||
185 | * @return \DateTime|null |
||
186 | */ |
||
187 | public function getWhenAt() |
||
191 | |||
192 | /** |
||
193 | * @return \DateTime |
||
194 | */ |
||
195 | public function getCreatedAt() |
||
199 | |||
200 | /** |
||
201 | * @return \DateTime |
||
202 | */ |
||
203 | public function getUpdatedAt() |
||
207 | |||
208 | /** |
||
209 | * @return JobManagerInterface |
||
210 | */ |
||
211 | public function getJobManager() |
||
215 | |||
216 | /** |
||
217 | * @param mixed $id |
||
218 | */ |
||
219 | public function setId($id) |
||
223 | |||
224 | /** |
||
225 | * @param string $workerName |
||
226 | */ |
||
227 | public function setWorkerName($workerName) |
||
231 | |||
232 | /** |
||
233 | * @param string $className |
||
234 | */ |
||
235 | public function setClassName($className) |
||
239 | |||
240 | /** |
||
241 | * @param string $method |
||
242 | */ |
||
243 | public function setMethod($method) |
||
247 | |||
248 | /** |
||
249 | * @return \DateTime|null |
||
250 | */ |
||
251 | public function getStartedAt() |
||
255 | |||
256 | /** |
||
257 | * @param \DateTime $startedAt |
||
258 | */ |
||
259 | public function setStartedAt(\DateTime $startedAt) |
||
263 | |||
264 | /** |
||
265 | * @return \DateTime|null |
||
266 | */ |
||
267 | public function getFinishedAt() |
||
271 | |||
272 | /** |
||
273 | * @param \DateTime $finishedAt |
||
274 | */ |
||
275 | public function setFinishedAt($finishedAt) |
||
279 | |||
280 | /** |
||
281 | * @return int|null |
||
282 | */ |
||
283 | public function getMaxDuration() |
||
287 | |||
288 | /** |
||
289 | * @param int|null $maxDuration |
||
290 | */ |
||
291 | public function setMaxDuration($maxDuration) |
||
295 | |||
296 | /** |
||
297 | * @param $args |
||
298 | */ |
||
299 | public function setArgs($args) |
||
307 | |||
308 | protected function validateArgs($args) |
||
322 | |||
323 | /** |
||
324 | * @param bool $batch |
||
325 | */ |
||
326 | public function setBatch($batch) |
||
330 | |||
331 | /** |
||
332 | * @param int $priority |
||
333 | */ |
||
334 | public function setPriority($priority) |
||
338 | |||
339 | /** |
||
340 | * @param string $crcHash |
||
341 | */ |
||
342 | public function setCrcHash($crcHash) |
||
346 | |||
347 | /** |
||
348 | * @param \DateTime $whenAt |
||
349 | */ |
||
350 | public function setWhenAt(\DateTime $whenAt) |
||
354 | |||
355 | /** |
||
356 | * @param \DateTime $createdAt |
||
357 | */ |
||
358 | public function setCreatedAt(\DateTime $createdAt) |
||
362 | |||
363 | /** |
||
364 | * @param \DateTime $updatedAt |
||
365 | */ |
||
366 | public function setUpdatedAt(\DateTime $updatedAt) |
||
370 | |||
371 | /** |
||
372 | * @param JobManagerInterface $jobManager |
||
373 | */ |
||
374 | public function setJobManager(JobManagerInterface $jobManager) |
||
378 | |||
379 | protected $worker; |
||
380 | |||
381 | public function __construct(Worker $worker = null, $batch = false, $priority = 10, \DateTime $whenAt = null) |
||
395 | |||
396 | public function __call($method, $args) |
||
412 | |||
413 | /** |
||
414 | * @return int |
||
415 | */ |
||
416 | public function getDelay() |
||
420 | |||
421 | /** |
||
422 | * @return Worker |
||
423 | */ |
||
424 | public function getWorker() |
||
428 | |||
429 | /** |
||
430 | * @param int $delay Delay in seconds |
||
431 | */ |
||
432 | public function setDelay($delay) |
||
436 | |||
437 | /** |
||
438 | * @param Worker $worker |
||
439 | */ |
||
440 | public function setWorker($worker) |
||
444 | |||
445 | /** |
||
446 | * @return int |
||
447 | */ |
||
448 | public function getElapsed() |
||
452 | |||
453 | /** |
||
454 | * @param int $elapsed |
||
455 | */ |
||
456 | public function setElapsed($elapsed) |
||
460 | |||
461 | /** |
||
462 | * @return mixed |
||
463 | */ |
||
464 | public function getRunId() |
||
468 | |||
469 | /** |
||
470 | * @param mixed $runId |
||
471 | */ |
||
472 | public function setRunId($runId) |
||
476 | |||
477 | /** |
||
478 | * @return string A json_encoded version of a queueable version of the object |
||
479 | */ |
||
480 | View Code Duplication | public function toMessage() |
|
491 | |||
492 | /** |
||
493 | * @param string $message a json_encoded version of the object |
||
494 | */ |
||
495 | View Code Duplication | public function fromMessage($message) |
|
509 | } |
||
510 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.