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 |
||
47 | class Queue implements QueueInterface |
||
48 | { |
||
49 | |||
50 | const PRIORITY_HIGH = 'high'; |
||
51 | const PRIORITY_LOW = 'low'; |
||
52 | |||
53 | const NS_QUEUE = 'queue'; |
||
54 | const NS_QUEUE_STOP = 'queue2stop'; |
||
55 | const NS_MESSAGE = 'message'; |
||
56 | const NS_MESSAGE_TO_QUEUE = 'message2queue'; |
||
57 | const NS_MESSAGE_TO_STATE = 'message2state'; |
||
58 | |||
59 | // State |
||
60 | const STATE_READY = 'ready'; |
||
61 | const STATE_DELAYED = 'delayed'; |
||
62 | const STATE_RESERVED = 'reserved'; |
||
63 | const STATE_BURIED = 'buried'; |
||
64 | |||
65 | // Stats |
||
66 | const STATS_QUEUES_LIST = 'queues'; |
||
67 | const STATS_QUEUES = 'queues'; |
||
68 | const STATS_MESSAGE_TOTAL = 'total'; |
||
69 | const STATS_MESSAGE_READY = 'ready'; |
||
70 | const STATS_MESSAGE_RESERVED = 'reserved'; |
||
71 | const STATS_MESSAGE_DELAYED = 'delayed'; |
||
72 | const STATS_MESSAGE_BURIED = 'buried'; |
||
73 | const STATS_QUEUE_STOP = 'stop'; |
||
74 | |||
75 | /** |
||
76 | * @var \Predis\Client |
||
77 | */ |
||
78 | protected $redis; |
||
79 | |||
80 | /** |
||
81 | * @var string |
||
82 | */ |
||
83 | protected $namespace; |
||
84 | |||
85 | /** |
||
86 | * Queue constructor. |
||
87 | * |
||
88 | * @param mixed $redis |
||
89 | * @param string $namespace |
||
90 | */ |
||
91 | 28 | public function __construct($redis, $namespace = 'Quedis') |
|
96 | |||
97 | /** |
||
98 | * @return \Predis\Client |
||
99 | */ |
||
100 | 28 | public function getRedis() |
|
104 | |||
105 | /** |
||
106 | * @return string |
||
107 | */ |
||
108 | 1 | public function getNamespace() |
|
112 | |||
113 | /** |
||
114 | * @param string $queue |
||
115 | * @param mixed $data |
||
116 | * @param int $delay |
||
117 | * @param string $priority |
||
118 | * |
||
119 | * @return Message |
||
120 | * @throws \Exception |
||
121 | */ |
||
122 | 23 | public function put($queue, $data, $delay = 0, $priority = self::PRIORITY_LOW) |
|
153 | |||
154 | /** |
||
155 | * @param string $queue |
||
156 | * @param int $timeout |
||
157 | * @param callable|null $callback |
||
158 | * |
||
159 | * @return mixed|null|Queue |
||
160 | */ |
||
161 | 7 | public function pop($queue, $timeout = 0, callable $callback = null) |
|
180 | |||
181 | /** |
||
182 | * @param string $queue |
||
183 | * @param int $timeout |
||
184 | * @param callable|null $callback |
||
185 | * |
||
186 | * @return mixed|null|static |
||
187 | */ |
||
188 | 17 | public function reserve($queue, $timeout = 0, callable $callback = null) |
|
211 | |||
212 | /** |
||
213 | * @param string $queue |
||
214 | * @param Message $message |
||
215 | * @param int $timeout |
||
216 | * @param null $callback |
||
217 | * |
||
218 | * @return mixed|null|Queue |
||
219 | */ |
||
220 | 16 | protected function reserveResult($queue, $message, $timeout = 0, $callback = null) |
|
229 | |||
230 | /** |
||
231 | * @param string $queue |
||
232 | * @param int $timeout |
||
233 | * |
||
234 | * @return null|string |
||
235 | */ |
||
236 | 16 | protected function reserveToken($queue, $timeout = 0) |
|
251 | |||
252 | /** |
||
253 | * @param string|null $token |
||
254 | * |
||
255 | * @return null|Message |
||
256 | */ |
||
257 | 16 | protected function restoreMessage($token) |
|
268 | |||
269 | /** |
||
270 | * @param Message|string $mixed |
||
271 | * |
||
272 | * @return $this |
||
273 | * @throws QueueException |
||
274 | */ |
||
275 | 3 | View Code Duplication | public function bury($mixed) |
293 | |||
294 | /** |
||
295 | * @param Message|string $mixed |
||
296 | * |
||
297 | * @return $this |
||
298 | * @throws QueueException |
||
299 | */ |
||
300 | 16 | public function delete($mixed) |
|
321 | |||
322 | /** |
||
323 | * @param Message|string $mixed |
||
324 | * |
||
325 | * @return $this |
||
326 | * @throws QueueException |
||
327 | */ |
||
328 | 3 | View Code Duplication | public function kick($mixed) |
346 | |||
347 | /** |
||
348 | * @param Message|string $mixed |
||
349 | * @param int $delay |
||
350 | * |
||
351 | * @return $this |
||
352 | * @throws FlowException |
||
353 | * @throws QueueException |
||
354 | */ |
||
355 | 2 | public function release($mixed, $delay = 0) |
|
380 | |||
381 | /** |
||
382 | * @return array |
||
383 | */ |
||
384 | 6 | public function getQueueList() |
|
393 | |||
394 | /** |
||
395 | * @param null|string $queue |
||
396 | * |
||
397 | * @return $this |
||
398 | */ |
||
399 | 17 | public function migrate($queue = null) |
|
435 | |||
436 | /** |
||
437 | * @param string $queue |
||
438 | * |
||
439 | * @return $this |
||
440 | */ |
||
441 | 1 | public function start($queue) |
|
447 | |||
448 | /** |
||
449 | * @param string $queue |
||
450 | * |
||
451 | * @return $this |
||
452 | */ |
||
453 | 2 | public function stop($queue) |
|
459 | |||
460 | /** |
||
461 | * @param string $queue |
||
462 | * |
||
463 | * @return bool |
||
464 | */ |
||
465 | 20 | public function isStop($queue) |
|
469 | |||
470 | /** |
||
471 | * @param string $queue |
||
472 | * |
||
473 | * @return array |
||
474 | */ |
||
475 | 3 | protected function queueStats($queue) |
|
489 | |||
490 | /** |
||
491 | * @param null|string $queue |
||
492 | * |
||
493 | * @return array |
||
494 | */ |
||
495 | 7 | public function stats($queue = null) |
|
530 | |||
531 | /** |
||
532 | * @param string $queue |
||
533 | * @param string $state |
||
534 | * |
||
535 | * @return int|string |
||
536 | * @throws QueueException |
||
537 | */ |
||
538 | 4 | public function size($queue, $state = self::STATE_READY) |
|
550 | |||
551 | |||
552 | /** |
||
553 | * @param null|string $queue |
||
554 | * |
||
555 | * @return $this |
||
556 | */ |
||
557 | 28 | public function clean($queue = null) |
|
573 | |||
574 | /** |
||
575 | * @param mixed $mixed |
||
576 | * |
||
577 | * @return Message |
||
578 | */ |
||
579 | 23 | protected function createMessage($mixed) |
|
587 | |||
588 | /** |
||
589 | * @param string $type |
||
590 | * |
||
591 | * @return string |
||
592 | */ |
||
593 | 24 | protected function ns($type) |
|
597 | |||
598 | |||
599 | /** |
||
600 | * @param string $queue |
||
601 | * @param string $state |
||
602 | * |
||
603 | * @return string |
||
604 | */ |
||
605 | 23 | protected function getKey($queue, $state) |
|
609 | |||
610 | /** |
||
611 | * @param int|\DateTime $mixed |
||
612 | * |
||
613 | * @return int |
||
614 | */ |
||
615 | 23 | protected function parseDelay($mixed) |
|
625 | |||
626 | /** |
||
627 | * @param string $currentState |
||
628 | * @param string $action |
||
629 | * |
||
630 | * @return $this |
||
631 | * @throws FlowException |
||
632 | */ |
||
633 | 20 | protected function checkMessageFlow($currentState, $action) |
|
648 | |||
649 | /** |
||
650 | * @param Message|string $mixed |
||
651 | * |
||
652 | * @return Payload |
||
653 | * @throws QueueException |
||
654 | */ |
||
655 | 20 | protected function payload($mixed) |
|
664 | |||
665 | } |
||
666 |
It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.
We recommend to add an additional type check (or disallow null for the parameter):