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 Queue 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 Queue, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Queue |
||
16 | { |
||
17 | |||
18 | // region MAIN ************************************************************* |
||
19 | |||
20 | // Namespaces |
||
21 | const REDIS_NS_JOB = 'cronario@queue-job'; |
||
22 | const REDIS_NS_QUEUE = 'cronario@queue'; |
||
23 | const REDIS_NS_QUEUE_STOP = 'cronario@queueStop'; |
||
24 | |||
25 | // State |
||
26 | const STATE_READY = 'ready'; |
||
27 | const STATE_DELAYED = 'delayed'; |
||
28 | const STATE_RESERVED = 'reserved'; |
||
29 | const STATE_BURIED = 'buried'; |
||
30 | |||
31 | // Priority |
||
32 | const PRIORITY_HIGH = 'high'; |
||
33 | const PRIORITY_LOW = 'low'; |
||
34 | |||
35 | // Stats |
||
36 | const STATS_QUEUES_LIST = 'queues-list'; |
||
37 | const STATS_QUEUES = 'queues'; |
||
38 | const STATS_JOBS_TOTAL = 'jobs-total'; |
||
39 | const STATS_JOBS_READY = 'jobs-ready'; |
||
40 | const STATS_JOBS_RESERVED = 'jobs-reserved'; |
||
41 | const STATS_JOBS_DELAYED = 'jobs-delayed'; |
||
42 | const STATS_JOBS_BURIED = 'jobs-buried'; |
||
43 | const STATS_QUEUE_STOP = 'stop'; |
||
44 | |||
45 | // Job payload |
||
46 | const JOB_PAYLOAD_QUEUE = 'queue'; |
||
47 | const JOB_PAYLOAD_STATE = 'state'; |
||
48 | |||
49 | // endregion *************************************************************** |
||
50 | |||
51 | |||
52 | // region STATIC ******************************************************** |
||
53 | |||
54 | /** @var Producer */ |
||
55 | protected $producer; |
||
56 | |||
57 | /** |
||
58 | * @param Producer $producer |
||
59 | * |
||
60 | * @return $this |
||
61 | * @throws QueueException |
||
62 | */ |
||
63 | 57 | public function setProducer(Producer $producer) |
|
73 | |||
74 | /** |
||
75 | * @return Producer |
||
76 | * @throws QueueException |
||
77 | */ |
||
78 | 1 | public function getProducer() |
|
86 | |||
87 | /** |
||
88 | * @return \Predis\Client |
||
89 | */ |
||
90 | 8 | public function getRedis() |
|
94 | |||
95 | /** |
||
96 | * @return string |
||
97 | */ |
||
98 | 8 | public function getAppId() |
|
102 | |||
103 | /** |
||
104 | * @return string |
||
105 | */ |
||
106 | 6 | protected function getRedisQueueNamespace() |
|
110 | |||
111 | /** |
||
112 | * @return string |
||
113 | */ |
||
114 | 6 | protected function getRedisJobNamespace() |
|
118 | |||
119 | /** |
||
120 | * @return string |
||
121 | */ |
||
122 | 5 | protected function getRedisQueueStopNamespace() |
|
126 | |||
127 | // endregion *************************************************************** |
||
128 | |||
129 | // region QUEUE Stopping ************************************************************ |
||
130 | |||
131 | /** |
||
132 | * @param $queueName |
||
133 | * |
||
134 | * @return bool |
||
135 | */ |
||
136 | 5 | public function isStop($queueName) |
|
140 | |||
141 | /** |
||
142 | * @param $queueName |
||
143 | * |
||
144 | * @return bool |
||
145 | */ |
||
146 | 1 | public function stop($queueName) |
|
150 | |||
151 | /** |
||
152 | * @param $queueName |
||
153 | * |
||
154 | * @return bool |
||
155 | */ |
||
156 | 1 | public function start($queueName) |
|
160 | |||
161 | |||
162 | // endregion *************************************************************** |
||
163 | |||
164 | // region STATS ************************************************************ |
||
165 | |||
166 | /** |
||
167 | * @return array |
||
168 | */ |
||
169 | 2 | public function getQueueNames() |
|
187 | |||
188 | /** |
||
189 | * @return array |
||
190 | */ |
||
191 | 1 | public function getJobReserved() |
|
209 | |||
210 | /** |
||
211 | * @param $queue |
||
212 | * @param $state |
||
213 | * |
||
214 | * @return int|string |
||
215 | */ |
||
216 | 4 | public function getJobCount($queue, $state) |
|
224 | |||
225 | /** |
||
226 | * Return statistical information about the server, across all clients. |
||
227 | * Keys that can be expected to be returned are the following: |
||
228 | * |
||
229 | * @return array |
||
230 | */ |
||
231 | 1 | public function getStats() |
|
261 | |||
262 | |||
263 | /** |
||
264 | * @param $queueName |
||
265 | * |
||
266 | * @return array |
||
267 | */ |
||
268 | 4 | public function getQueueInfo($queueName) |
|
282 | |||
283 | |||
284 | // endregion *************************************************************** |
||
285 | |||
286 | // region JobPayload ************************************************************ |
||
287 | |||
288 | /** |
||
289 | * @param $id |
||
290 | * |
||
291 | * @return mixed |
||
292 | */ |
||
293 | 4 | protected function getPayload($id) |
|
299 | |||
300 | /** |
||
301 | * @param $queueName string |
||
302 | * @param $jobState string |
||
303 | * |
||
304 | * @return string |
||
305 | */ |
||
306 | 5 | protected static function buildPayload($queueName, $jobState) |
|
313 | |||
314 | /** |
||
315 | * @param $rawPayload |
||
316 | * |
||
317 | * @return mixed |
||
318 | */ |
||
319 | 4 | protected static function parsePayload($rawPayload) |
|
323 | |||
324 | // endregion *************************************************************** |
||
325 | |||
326 | // region HELPER ************************************************************ |
||
327 | |||
328 | |||
329 | /** |
||
330 | * @return int |
||
331 | */ |
||
332 | 4 | protected function getTime() |
|
336 | |||
337 | /** |
||
338 | * @param string $key |
||
339 | * |
||
340 | * @return string |
||
341 | */ |
||
342 | protected function getQueueNameFromKey($key) |
||
346 | |||
347 | /** |
||
348 | * @param string $queue |
||
349 | * @param string $state |
||
350 | * |
||
351 | * @return string |
||
352 | */ |
||
353 | 6 | protected function getKey($queue, $state = self::STATE_READY) |
|
357 | |||
358 | /** |
||
359 | * @param $queue |
||
360 | * |
||
361 | * @return string |
||
362 | */ |
||
363 | 5 | protected function getKeyReady($queue) |
|
367 | |||
368 | /** |
||
369 | * @param $queue |
||
370 | * |
||
371 | * @return string |
||
372 | */ |
||
373 | 5 | protected function getKeyDelayed($queue) |
|
377 | |||
378 | /** |
||
379 | * @param $queue |
||
380 | * |
||
381 | * @return string |
||
382 | */ |
||
383 | 5 | protected function getKeyReserved($queue) |
|
387 | |||
388 | /** |
||
389 | * @param $queue |
||
390 | * |
||
391 | * @return string |
||
392 | */ |
||
393 | 4 | protected function getKeyBuried($queue) |
|
397 | |||
398 | |||
399 | // endregion *************************************************************** |
||
400 | |||
401 | // region JOB ************************************************************ |
||
402 | |||
403 | /** |
||
404 | * @param $id |
||
405 | * |
||
406 | * @return bool |
||
407 | */ |
||
408 | 1 | public function existsJob($id) |
|
412 | |||
413 | /** |
||
414 | * @param $queue |
||
415 | * @param $id |
||
416 | * @param int $delay |
||
417 | * @param string $priority |
||
418 | * |
||
419 | * @return bool |
||
420 | */ |
||
421 | 5 | public function putJob($queue, $id, $delay = 0, $priority = self::PRIORITY_LOW) |
|
448 | |||
449 | /** |
||
450 | * @param $queue |
||
451 | * @param null $timeout |
||
452 | * |
||
453 | * @return null|string |
||
454 | */ |
||
455 | 4 | public function reserveJob($queue, $timeout = null) |
|
488 | |||
489 | /** |
||
490 | * @param $id |
||
491 | * |
||
492 | * @return bool |
||
493 | */ |
||
494 | 4 | public function deleteJob($id) |
|
517 | |||
518 | /** |
||
519 | * @param $id |
||
520 | * |
||
521 | * @return bool |
||
522 | * @throws QueueException |
||
523 | */ |
||
524 | 1 | View Code Duplication | public function buryJob($id) |
548 | |||
549 | /** |
||
550 | * @param $id |
||
551 | * |
||
552 | * @return bool |
||
553 | * @throws QueueException |
||
554 | */ |
||
555 | 1 | View Code Duplication | public function kickJob($id) |
579 | |||
580 | /** |
||
581 | * @param $id |
||
582 | * @param int $delay |
||
583 | * |
||
584 | * @return bool |
||
585 | * @throws QueueException |
||
586 | */ |
||
587 | 1 | public function releaseJob($id, $delay = 0) |
|
621 | |||
622 | // endregion *************************************************************** |
||
623 | |||
624 | // region MIGRATIONS ************************************************************* |
||
625 | |||
626 | /** |
||
627 | * @param null $queue |
||
628 | * |
||
629 | * @return bool |
||
630 | */ |
||
631 | 4 | public function migrate($queue = null) |
|
669 | |||
670 | // endregion *************************************************************** |
||
671 | |||
672 | /** |
||
673 | * @return $this |
||
674 | */ |
||
675 | public function clean() |
||
683 | |||
684 | |||
685 | } |
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.