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 |
||
| 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'; |
||
| 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 | 35 | public function __construct($redis, $namespace = 'Quedis') |
|
| 97 | |||
| 98 | /** |
||
| 99 | * @return \Predis\Client |
||
| 100 | */ |
||
| 101 | 35 | 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 | 28 | 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 | 11 | public function pop($queue, $timeout = 0) |
|
| 169 | |||
| 170 | /** |
||
| 171 | * @param string $queue |
||
| 172 | * @param int $timeout |
||
| 173 | * |
||
| 174 | * @return MessageInterface|null |
||
| 175 | */ |
||
| 176 | 22 | public function reserve($queue, $timeout = 0) |
|
| 199 | |||
| 200 | /** |
||
| 201 | * @param string $queue |
||
| 202 | * @param int $timeout |
||
| 203 | * |
||
| 204 | * @return null|string |
||
| 205 | */ |
||
| 206 | 21 | protected function reserveToken($queue, $timeout = 0) |
|
| 221 | |||
| 222 | /** |
||
| 223 | * @param string|null $token |
||
| 224 | * |
||
| 225 | * @return null|MessageInterface |
||
| 226 | */ |
||
| 227 | 21 | protected function restoreMessage($token) |
|
| 238 | |||
| 239 | /** |
||
| 240 | * @param string|MessageInterface $mixed |
||
| 241 | * |
||
| 242 | * @return $this |
||
| 243 | * @throws QueueException |
||
| 244 | */ |
||
| 245 | 3 | View Code Duplication | public function bury($mixed) |
| 261 | |||
| 262 | /** |
||
| 263 | * @param string|MessageInterface $mixed |
||
| 264 | * |
||
| 265 | * @return $this |
||
| 266 | * @throws QueueException |
||
| 267 | */ |
||
| 268 | 20 | public function delete($mixed) |
|
| 285 | |||
| 286 | /** |
||
| 287 | * @param MessageInterface|string $mixed |
||
| 288 | * |
||
| 289 | * @return $this |
||
| 290 | * @throws QueueException |
||
| 291 | */ |
||
| 292 | 3 | View Code Duplication | public function kick($mixed) |
| 308 | |||
| 309 | /** |
||
| 310 | * @param MessageInterface|string $mixed |
||
| 311 | * @param int $delay |
||
| 312 | * |
||
| 313 | * @return $this |
||
| 314 | * @throws FlowException |
||
| 315 | * @throws QueueException |
||
| 316 | */ |
||
| 317 | 3 | public function release($mixed, $delay = 0) |
|
| 340 | |||
| 341 | /** |
||
| 342 | * @return array |
||
| 343 | */ |
||
| 344 | 4 | public function getQueueList() |
|
| 353 | |||
| 354 | /** |
||
| 355 | * @param null|string $queue |
||
| 356 | * |
||
| 357 | * @return $this |
||
| 358 | */ |
||
| 359 | 22 | public function migrate($queue = null) |
|
| 395 | |||
| 396 | |||
| 397 | /** |
||
| 398 | * @param string $queue |
||
| 399 | * @param array $options |
||
| 400 | * |
||
| 401 | * @return Iterator |
||
| 402 | */ |
||
| 403 | 1 | public function iterator($queue, array $options = []) |
|
| 409 | |||
| 410 | /** |
||
| 411 | * @param string $queue |
||
| 412 | * |
||
| 413 | * @return $this |
||
| 414 | */ |
||
| 415 | 1 | public function start($queue) |
|
| 421 | |||
| 422 | /** |
||
| 423 | * @param string $queue |
||
| 424 | * |
||
| 425 | * @return $this |
||
| 426 | */ |
||
| 427 | 2 | public function stop($queue) |
|
| 433 | |||
| 434 | /** |
||
| 435 | * @param string $queue |
||
| 436 | * |
||
| 437 | * @return bool |
||
| 438 | */ |
||
| 439 | 25 | public function isStop($queue) |
|
| 443 | |||
| 444 | /** |
||
| 445 | * @param string $queue |
||
| 446 | * |
||
| 447 | * @return array |
||
| 448 | */ |
||
| 449 | 4 | protected function queueStats($queue) |
|
| 463 | |||
| 464 | /** |
||
| 465 | * @param null|string $queue |
||
| 466 | * |
||
| 467 | * @return array |
||
| 468 | */ |
||
| 469 | 6 | public function stats($queue = null) |
|
| 504 | |||
| 505 | /** |
||
| 506 | * @param string $queue |
||
| 507 | * @param string $state |
||
| 508 | * |
||
| 509 | * @return int|string |
||
| 510 | * @throws QueueException |
||
| 511 | */ |
||
| 512 | 5 | public function size($queue, $state = self::STATE_READY) |
|
| 524 | |||
| 525 | |||
| 526 | /** |
||
| 527 | * @param null|string $queue |
||
| 528 | * |
||
| 529 | * @return $this |
||
| 530 | */ |
||
| 531 | 35 | public function clean($queue = null) |
|
| 547 | |||
| 548 | /** |
||
| 549 | * @param mixed $mixed |
||
| 550 | * |
||
| 551 | * @return MessageInterface |
||
| 552 | */ |
||
| 553 | 28 | protected function createMessage($mixed) |
|
| 561 | |||
| 562 | /** |
||
| 563 | * @param string $type |
||
| 564 | * |
||
| 565 | * @return string |
||
| 566 | */ |
||
| 567 | 29 | protected function ns($type) |
|
| 571 | |||
| 572 | |||
| 573 | /** |
||
| 574 | * @param string $queue |
||
| 575 | * @param string $state |
||
| 576 | * |
||
| 577 | * @return string |
||
| 578 | */ |
||
| 579 | 28 | protected function getKey($queue, $state) |
|
| 583 | |||
| 584 | /** |
||
| 585 | * @param int|\DateTime $mixed |
||
| 586 | * |
||
| 587 | * @return int |
||
| 588 | */ |
||
| 589 | 28 | protected function parseDelay($mixed) |
|
| 599 | |||
| 600 | /** |
||
| 601 | * @param string $currentState |
||
| 602 | * @param string $action |
||
| 603 | * |
||
| 604 | * @return $this |
||
| 605 | * @throws FlowException |
||
| 606 | */ |
||
| 607 | 25 | protected function checkMessageFlow($currentState, $action) |
|
| 622 | |||
| 623 | /** |
||
| 624 | * @param array $result |
||
| 625 | * @param string $action |
||
| 626 | * |
||
| 627 | * @return $this |
||
| 628 | * @throws QueueException |
||
| 629 | */ |
||
| 630 | 29 | protected function checkTransactionResult($result, $action) |
|
| 638 | |||
| 639 | /** |
||
| 640 | * @param string|MessageInterface $mixed |
||
| 641 | * |
||
| 642 | * @return Payload |
||
| 643 | * @throws QueueException |
||
| 644 | */ |
||
| 645 | 25 | protected function payload($mixed) |
|
| 654 | |||
| 655 | } |
||
| 656 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.