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 |
||
48 | class Queue implements QueueInterface |
||
49 | { |
||
50 | |||
51 | const PRIORITY_HIGH = 'high'; |
||
52 | const PRIORITY_LOW = 'low'; |
||
53 | |||
54 | const NS_QUEUE = 'queue'; |
||
55 | const NS_QUEUE_STOP = 'queue2stop'; |
||
56 | const NS_MESSAGE = 'message'; |
||
57 | const NS_MESSAGE_TO_QUEUE = 'message2queue'; |
||
58 | const NS_MESSAGE_TO_STATE = 'message2state'; |
||
59 | |||
60 | // State |
||
61 | const STATE_READY = 'ready'; |
||
62 | const STATE_DELAYED = 'delayed'; |
||
63 | const STATE_RESERVED = 'reserved'; |
||
64 | const STATE_BURIED = 'buried'; |
||
65 | |||
66 | // Stats |
||
67 | const STATS_QUEUES_LIST = 'queues-list'; |
||
68 | const STATS_QUEUES = 'queues'; |
||
69 | const STATS_MESSAGE_TOTAL = 'total'; |
||
70 | const STATS_MESSAGE_READY = 'ready'; |
||
71 | const STATS_MESSAGE_RESERVED = 'reserved'; |
||
72 | const STATS_MESSAGE_DELAYED = 'delayed'; |
||
73 | const STATS_MESSAGE_BURIED = 'buried'; |
||
74 | const STATS_QUEUE_STOP = 'stop'; |
||
75 | |||
76 | /** |
||
77 | * @var \Predis\Client |
||
78 | */ |
||
79 | protected $redis; |
||
80 | |||
81 | /** |
||
82 | * @var string |
||
83 | */ |
||
84 | protected $namespace; |
||
85 | |||
86 | /** |
||
87 | * Queue constructor. |
||
88 | * |
||
89 | * @param mixed $redis |
||
90 | * @param string $namespace |
||
91 | */ |
||
92 | 33 | public function __construct($redis, $namespace = 'Quedis') |
|
97 | |||
98 | /** |
||
99 | * @return \Predis\Client |
||
100 | */ |
||
101 | 33 | public function getRedis() |
|
105 | |||
106 | /** |
||
107 | * @return string |
||
108 | */ |
||
109 | 1 | public function getNamespace() |
|
113 | |||
114 | /** |
||
115 | * @param string $queue |
||
116 | * @param mixed $data |
||
117 | * @param int $delay |
||
118 | * @param string $priority |
||
119 | * |
||
120 | * @return MessageInterface |
||
121 | * @throws \Exception |
||
122 | */ |
||
123 | 26 | public function put($queue, $data, $delay = 0, $priority = self::PRIORITY_LOW) |
|
152 | |||
153 | /** |
||
154 | * @param string $queue |
||
155 | * @param int $timeout |
||
156 | * |
||
157 | * @return mixed|null|Queue |
||
158 | */ |
||
159 | 9 | public function pop($queue, $timeout = 0) |
|
169 | |||
170 | /** |
||
171 | * @param string $queue |
||
172 | * @param int $timeout |
||
173 | * |
||
174 | * @return MessageInterface|null |
||
175 | */ |
||
176 | 20 | public function reserve($queue, $timeout = 0) |
|
199 | |||
200 | /** |
||
201 | * @param string $queue |
||
202 | * @param int $timeout |
||
203 | * |
||
204 | * @return null|string |
||
205 | */ |
||
206 | 19 | protected function reserveToken($queue, $timeout = 0) |
|
221 | |||
222 | /** |
||
223 | * @param string|null $token |
||
224 | * |
||
225 | * @return null|MessageInterface |
||
226 | */ |
||
227 | 19 | protected function restoreMessage($token) |
|
238 | |||
239 | /** |
||
240 | * @param string|MessageInterface $mixed |
||
241 | * |
||
242 | * @return $this |
||
243 | * @throws QueueException |
||
244 | */ |
||
245 | 18 | public function delete($mixed) |
|
262 | |||
263 | /** |
||
264 | * @param MessageInterface|string $mixed |
||
265 | * @param int $delay |
||
266 | * |
||
267 | * @return $this |
||
268 | * @throws FlowException |
||
269 | * @throws QueueException |
||
270 | */ |
||
271 | 3 | public function release($mixed, $delay = 0) |
|
294 | |||
295 | /** |
||
296 | * @param MessageInterface|string $mixed |
||
297 | * |
||
298 | * @return $this |
||
299 | * @throws QueueException |
||
300 | */ |
||
301 | 3 | public function kick($mixed) |
|
305 | |||
306 | /** |
||
307 | * @param string|MessageInterface $mixed |
||
308 | * |
||
309 | * @return $this |
||
310 | * @throws QueueException |
||
311 | */ |
||
312 | 3 | public function bury($mixed) |
|
316 | |||
317 | /** |
||
318 | * @param MessageInterface|string $mixed |
||
319 | * @param string $moveTo |
||
320 | * |
||
321 | * @return $this |
||
322 | * @throws QueueException |
||
323 | */ |
||
324 | 5 | protected function moveMessage($mixed, $moveTo) |
|
347 | |||
348 | /** |
||
349 | * @return array |
||
350 | */ |
||
351 | 4 | public function getQueueList() |
|
360 | |||
361 | /** |
||
362 | * @param null|string $queue |
||
363 | * |
||
364 | * @return $this |
||
365 | */ |
||
366 | 20 | public function migrate($queue = null) |
|
402 | |||
403 | |||
404 | /** |
||
405 | * @param string $queue |
||
406 | * @param string $strategy |
||
407 | * @param int $timeout |
||
408 | * |
||
409 | * @return Iterator |
||
410 | */ |
||
411 | 1 | public function iterator($queue, $strategy = 'pop', $timeout = 0) |
|
415 | |||
416 | /** |
||
417 | * @param string $queue |
||
418 | * |
||
419 | * @return $this |
||
420 | */ |
||
421 | 1 | public function start($queue) |
|
427 | |||
428 | /** |
||
429 | * @param string $queue |
||
430 | * |
||
431 | * @return $this |
||
432 | */ |
||
433 | 2 | public function stop($queue) |
|
439 | |||
440 | /** |
||
441 | * @param string $queue |
||
442 | * |
||
443 | * @return bool |
||
444 | */ |
||
445 | 23 | public function isStop($queue) |
|
449 | |||
450 | /** |
||
451 | * @param string $queue |
||
452 | * |
||
453 | * @return array |
||
454 | */ |
||
455 | 4 | protected function queueStats($queue) |
|
469 | |||
470 | /** |
||
471 | * @param null|string $queue |
||
472 | * |
||
473 | * @return array |
||
474 | */ |
||
475 | 6 | public function stats($queue = null) |
|
510 | |||
511 | /** |
||
512 | * @param string $queue |
||
513 | * @param string $state |
||
514 | * |
||
515 | * @return int|string |
||
516 | * @throws QueueException |
||
517 | */ |
||
518 | 5 | public function size($queue, $state = self::STATE_READY) |
|
530 | |||
531 | |||
532 | /** |
||
533 | * @param null|string $queue |
||
534 | * |
||
535 | * @return $this |
||
536 | */ |
||
537 | 33 | public function clean($queue = null) |
|
553 | |||
554 | /** |
||
555 | * @param mixed $mixed |
||
556 | * |
||
557 | * @return MessageInterface |
||
558 | */ |
||
559 | 26 | protected function createMessage($mixed) |
|
567 | |||
568 | /** |
||
569 | * @param string $type |
||
570 | * |
||
571 | * @return string |
||
572 | */ |
||
573 | 27 | protected function ns($type) |
|
577 | |||
578 | |||
579 | /** |
||
580 | * @param string $queue |
||
581 | * @param string $state |
||
582 | * |
||
583 | * @return string |
||
584 | */ |
||
585 | 26 | protected function getKey($queue, $state) |
|
589 | |||
590 | /** |
||
591 | * @param int|\DateTime $mixed |
||
592 | * |
||
593 | * @return int |
||
594 | */ |
||
595 | 26 | protected function parseDelay($mixed) |
|
605 | |||
606 | /** |
||
607 | * @param string $currentState |
||
608 | * @param string $action |
||
609 | * |
||
610 | * @return $this |
||
611 | * @throws FlowException |
||
612 | */ |
||
613 | 23 | protected function checkMessageFlow($currentState, $action) |
|
628 | |||
629 | /** |
||
630 | * @param mixed $result |
||
631 | * @param string $action |
||
632 | * |
||
633 | * @return $this |
||
634 | * @throws QueueException |
||
635 | */ |
||
636 | 27 | protected function checkTransactionResult($result, $action) |
|
644 | |||
645 | /** |
||
646 | * @param string|MessageInterface $mixed |
||
647 | * |
||
648 | * @return Payload |
||
649 | * @throws QueueException |
||
650 | */ |
||
651 | 23 | protected function payload($mixed) |
|
660 | |||
661 | } |
||
662 |