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 implements RabbitMqObjectInterface, QueueInterface |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var Client |
||
| 19 | */ |
||
| 20 | private $client; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Queue name. |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | private $name; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Passive. |
||
| 31 | * |
||
| 32 | * @var bool |
||
| 33 | */ |
||
| 34 | private $passive = false; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Durable. |
||
| 38 | * |
||
| 39 | * @var bool |
||
| 40 | */ |
||
| 41 | private $durable = false; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Exclusive. |
||
| 45 | * |
||
| 46 | * @var bool |
||
| 47 | */ |
||
| 48 | private $exclusive = false; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Auto delete. |
||
| 52 | * |
||
| 53 | * @var bool |
||
| 54 | */ |
||
| 55 | private $autoDelete = false; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * No wait. |
||
| 59 | * |
||
| 60 | * @var bool |
||
| 61 | */ |
||
| 62 | private $noWait = false; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Ticket. |
||
| 66 | * |
||
| 67 | * @var int |
||
| 68 | */ |
||
| 69 | private $ticket = null; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * RabbitMq specific parameter : x-dead-letter-exchange. |
||
| 73 | * |
||
| 74 | * @var Exchange |
||
| 75 | */ |
||
| 76 | private $deadLetterExchanger = null; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * RabbitMq specific parameter : confirm. |
||
| 80 | * |
||
| 81 | * @var int |
||
| 82 | */ |
||
| 83 | private $confirm = null; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * RabbitMq specific parameter : consumer_cancel_notify. |
||
| 87 | * |
||
| 88 | * @var bool |
||
| 89 | */ |
||
| 90 | private $consumerCancelNotify = null; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * RabbitMq specific parameter : alternate-exchange. |
||
| 94 | * |
||
| 95 | * @var Queue |
||
| 96 | */ |
||
| 97 | private $alternateExchange = null; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * RabbitMq specific parameter : x-message-ttl. |
||
| 101 | * |
||
| 102 | * @var int |
||
| 103 | */ |
||
| 104 | private $ttl = null; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * RabbitMq specific parameter : x-max-length. |
||
| 108 | * |
||
| 109 | * @var int |
||
| 110 | */ |
||
| 111 | private $maxLength = null; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * RabbitMq specific parameter : x-max-priority. |
||
| 115 | * |
||
| 116 | * @var int |
||
| 117 | */ |
||
| 118 | private $maxPriority = null; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Parameter to initialize object only one time. |
||
| 122 | * |
||
| 123 | * @var bool |
||
| 124 | */ |
||
| 125 | private $init = false; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Consumer list implement ConsumerInterface. |
||
| 129 | * |
||
| 130 | * @var array |
||
| 131 | */ |
||
| 132 | private $consumers; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Set the source (Binding). |
||
| 136 | * |
||
| 137 | * @param Binding $source |
||
|
|
|||
| 138 | * @param string $name |
||
| 139 | * @param ConsumerInterface[] $consumers |
||
| 140 | */ |
||
| 141 | public function __construct(Client $client, $name, array $consumers = []) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Get queue name. |
||
| 151 | * |
||
| 152 | * @return string |
||
| 153 | */ |
||
| 154 | public function getName() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Get passive. |
||
| 161 | * |
||
| 162 | * @return bool |
||
| 163 | */ |
||
| 164 | public function getPassive() |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @param bool $passive |
||
| 171 | * |
||
| 172 | * @return Queue |
||
| 173 | */ |
||
| 174 | public function setPassive($passive) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Get durable. |
||
| 183 | * |
||
| 184 | * @return bool |
||
| 185 | */ |
||
| 186 | public function getDurable() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Set durable. |
||
| 193 | * |
||
| 194 | * @param bool $durable |
||
| 195 | * |
||
| 196 | * @return Queue |
||
| 197 | */ |
||
| 198 | public function setDurable($durable) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Get exclusive. |
||
| 207 | * |
||
| 208 | * @return bool |
||
| 209 | */ |
||
| 210 | public function getExclusive() |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Set exclusive. |
||
| 217 | * |
||
| 218 | * @param bool $exclusive |
||
| 219 | * |
||
| 220 | * @return Queue |
||
| 221 | */ |
||
| 222 | public function setExclusive($exclusive) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Get autoDelete. |
||
| 231 | * |
||
| 232 | * @return bool |
||
| 233 | */ |
||
| 234 | public function getAutoDelete() |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Set autoDelete. |
||
| 241 | * |
||
| 242 | * @param bool $autoDelete |
||
| 243 | * |
||
| 244 | * @return Queue |
||
| 245 | */ |
||
| 246 | public function setAutoDelete($autoDelete) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Get noWait. |
||
| 255 | * |
||
| 256 | * @return bool |
||
| 257 | */ |
||
| 258 | public function getNoWait() |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Set noWait. |
||
| 265 | * |
||
| 266 | * @param bool $noWait |
||
| 267 | * |
||
| 268 | * @return Queue |
||
| 269 | */ |
||
| 270 | public function setNoWait($noWait) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Get arguments. |
||
| 279 | * |
||
| 280 | * @return array|null |
||
| 281 | */ |
||
| 282 | public function getArguments() |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Set arguments. |
||
| 289 | * |
||
| 290 | * @param array $arguments |
||
| 291 | * |
||
| 292 | * @return Queue |
||
| 293 | */ |
||
| 294 | public function setArguments($arguments) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Get ticket. |
||
| 303 | * |
||
| 304 | * @return int |
||
| 305 | */ |
||
| 306 | public function getTicket() |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Set ticket. |
||
| 313 | * |
||
| 314 | * @param int $ticket |
||
| 315 | * |
||
| 316 | * @return Queue |
||
| 317 | */ |
||
| 318 | public function setTicket($ticket) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Get RabbitMq specific parameter : dead letter queue. |
||
| 327 | * |
||
| 328 | * @return Queue |
||
| 329 | */ |
||
| 330 | public function getDeadLetterExchanger() |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Set RabbitMq specific parameter : dead letter queue. |
||
| 337 | * |
||
| 338 | * @param Exchange $exchange |
||
| 339 | * |
||
| 340 | * @return Queue |
||
| 341 | */ |
||
| 342 | public function setDeadLetterExchange(Exchange $exchange) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Get RabbitMq specific parameter : confirm. |
||
| 351 | * |
||
| 352 | * @return int |
||
| 353 | */ |
||
| 354 | public function getConfirm() |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Set RabbitMq specific parameter : confirm. |
||
| 361 | * |
||
| 362 | * @param int $confirm |
||
| 363 | * |
||
| 364 | * @return Queue |
||
| 365 | */ |
||
| 366 | public function setConfirm($confirm) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Get RabbitMq specific parameter : consumer_cancel_notify. |
||
| 375 | * |
||
| 376 | * @return bool |
||
| 377 | */ |
||
| 378 | public function getConsumerCancelNotify() |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Set RabbitMq specific parameter : consumer_cancel_notify. |
||
| 385 | * |
||
| 386 | * @param Queue $consumerCancelNotify |
||
| 387 | * |
||
| 388 | * @return Queue |
||
| 389 | */ |
||
| 390 | public function setConsumerCancelNotify(Queue $consumerCancelNotify) |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Get RabbitMq specific parameter : alternate_exchange. |
||
| 399 | * |
||
| 400 | * @return Queue |
||
| 401 | */ |
||
| 402 | public function getAlternateExchange() |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Set RabbitMq specific parameter : alternate_exchange. |
||
| 409 | * |
||
| 410 | * @param Queue $alternateExchange |
||
| 411 | * |
||
| 412 | * @return Queue |
||
| 413 | */ |
||
| 414 | public function setAlternateExchange(Queue $alternateExchange) |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Get RabbitMq specific parameter : ttl. |
||
| 423 | * |
||
| 424 | * @return int |
||
| 425 | */ |
||
| 426 | public function getTtl() |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Set RabbitMq specific parameter : ttl. |
||
| 433 | * |
||
| 434 | * @param int $ttl |
||
| 435 | * |
||
| 436 | * @return Queue |
||
| 437 | */ |
||
| 438 | public function setTtl($ttl) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Get RabbitMq specific parameter : max length. |
||
| 447 | * |
||
| 448 | * @return int |
||
| 449 | */ |
||
| 450 | public function getMaxLength() |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Set RabbitMq specific parameter : max length. |
||
| 457 | * |
||
| 458 | * @param int $maxLength |
||
| 459 | * |
||
| 460 | * @return Queue |
||
| 461 | */ |
||
| 462 | public function setMaxLength($maxLength) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Get RabbitMq specific parameter : max priority. |
||
| 471 | * |
||
| 472 | * @return int |
||
| 473 | */ |
||
| 474 | public function getMaxPriority() |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Set RabbitMq specific parameter : max priority. |
||
| 481 | * |
||
| 482 | * @param int $maxPriority |
||
| 483 | * |
||
| 484 | * @return Queue |
||
| 485 | */ |
||
| 486 | public function setMaxPriority($maxPriority) |
||
| 492 | |||
| 493 | public function init(AMQPChannel $amqpChannel) |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Sends to RabbitMQ the order to subscribe to the consumers. |
||
| 533 | */ |
||
| 534 | public function consume() |
||
| 553 | |||
| 554 | /** |
||
| 555 | * Unsubscribes consumers. |
||
| 556 | */ |
||
| 557 | public function cancelConsume() |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Sends a message directly to this queue (skipping any exchange). |
||
| 570 | * |
||
| 571 | * This is a RabbitMQ only feature. Behind the scene, it will dispatch the message to the default RabbitMQ exchange. |
||
| 572 | * |
||
| 573 | * @param Message $message |
||
| 574 | * @param bool $mandatory |
||
| 575 | * @param bool $immediate |
||
| 576 | * @param null $ticket |
||
| 577 | */ |
||
| 578 | View Code Duplication | public function publish(Message $message, $mandatory = false, |
|
| 586 | } |
||
| 587 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.