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 |
||
| 14 | class Queue implements RabbitMqObjectInterface |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @var Client |
||
| 18 | */ |
||
| 19 | private $client; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Queue name. |
||
| 23 | * |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | private $name; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Passive. |
||
| 30 | * |
||
| 31 | * @var bool |
||
| 32 | */ |
||
| 33 | private $passive = false; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Durable. |
||
| 37 | * |
||
| 38 | * @var bool |
||
| 39 | */ |
||
| 40 | private $durable = false; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Exclusive. |
||
| 44 | * |
||
| 45 | * @var bool |
||
| 46 | */ |
||
| 47 | private $exclusive = false; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Auto delete. |
||
| 51 | * |
||
| 52 | * @var bool |
||
| 53 | */ |
||
| 54 | private $autoDelete = false; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * No wait. |
||
| 58 | * |
||
| 59 | * @var bool |
||
| 60 | */ |
||
| 61 | private $noWait = false; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Ticket. |
||
| 65 | * |
||
| 66 | * @var int |
||
| 67 | */ |
||
| 68 | private $ticket = null; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * RabbitMq specific parameter : x-dead-letter-exchange. |
||
| 72 | * |
||
| 73 | * @var Exchange |
||
| 74 | */ |
||
| 75 | private $deadLetterExchanger = null; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * RabbitMq specific parameter : confirm. |
||
| 79 | * |
||
| 80 | * @var int |
||
| 81 | */ |
||
| 82 | private $confirm = null; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * RabbitMq specific parameter : consumer_cancel_notify. |
||
| 86 | * |
||
| 87 | * @var bool |
||
| 88 | */ |
||
| 89 | private $consumerCancelNotify = null; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * RabbitMq specific parameter : alternate-exchange. |
||
| 93 | * |
||
| 94 | * @var Queue |
||
| 95 | */ |
||
| 96 | private $alternateExchange = null; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * RabbitMq specific parameter : x-message-ttl. |
||
| 100 | * |
||
| 101 | * @var int |
||
| 102 | */ |
||
| 103 | private $ttl = null; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * RabbitMq specific parameter : x-max-length. |
||
| 107 | * |
||
| 108 | * @var int |
||
| 109 | */ |
||
| 110 | private $maxLength = null; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * RabbitMq specific parameter : x-max-priority. |
||
| 114 | * |
||
| 115 | * @var int |
||
| 116 | */ |
||
| 117 | private $maxPriority = null; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Parameter to initialize object only one time. |
||
| 121 | * |
||
| 122 | * @var bool |
||
| 123 | */ |
||
| 124 | private $init = false; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Consumer list implement ConsumerInterface. |
||
| 128 | * |
||
| 129 | * @var array|null |
||
| 130 | */ |
||
| 131 | private $consumers; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Set the source (Binding). |
||
| 135 | * |
||
| 136 | * @param Binding $source |
||
|
|
|||
| 137 | * @param string $name |
||
| 138 | * @param ConsumerInterface[] $consumers |
||
| 139 | */ |
||
| 140 | View Code Duplication | public function __construct(Client $client, $name, array $consumers = []) |
|
| 147 | |||
| 148 | /** |
||
| 149 | * Get queue name. |
||
| 150 | * |
||
| 151 | * @return string |
||
| 152 | */ |
||
| 153 | public function getName() |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Get passive. |
||
| 160 | * |
||
| 161 | * @return bool |
||
| 162 | */ |
||
| 163 | public function getPassive() |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @param bool $passive |
||
| 170 | * |
||
| 171 | * @return Queue |
||
| 172 | */ |
||
| 173 | public function setPassive($passive) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Get durable. |
||
| 182 | * |
||
| 183 | * @return bool |
||
| 184 | */ |
||
| 185 | public function getDurable() |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Set durable. |
||
| 192 | * |
||
| 193 | * @param bool $durable |
||
| 194 | * |
||
| 195 | * @return Queue |
||
| 196 | */ |
||
| 197 | public function setDurable($durable) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Get exclusive. |
||
| 206 | * |
||
| 207 | * @return bool |
||
| 208 | */ |
||
| 209 | public function getExclusive() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Set exclusive. |
||
| 216 | * |
||
| 217 | * @param bool $exclusive |
||
| 218 | * |
||
| 219 | * @return Queue |
||
| 220 | */ |
||
| 221 | public function setExclusive($exclusive) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Get autoDelete. |
||
| 230 | * |
||
| 231 | * @return bool |
||
| 232 | */ |
||
| 233 | public function getAutoDelete() |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Set autoDelete. |
||
| 240 | * |
||
| 241 | * @param bool $autoDelete |
||
| 242 | * |
||
| 243 | * @return Queue |
||
| 244 | */ |
||
| 245 | public function setAutoDelete($autoDelete) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Get noWait. |
||
| 254 | * |
||
| 255 | * @return bool |
||
| 256 | */ |
||
| 257 | public function getNoWait() |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Set noWait. |
||
| 264 | * |
||
| 265 | * @param bool $noWait |
||
| 266 | * |
||
| 267 | * @return Queue |
||
| 268 | */ |
||
| 269 | public function setNoWait($noWait) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Get arguments. |
||
| 278 | * |
||
| 279 | * @return array|null |
||
| 280 | */ |
||
| 281 | public function getArguments() |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Set arguments. |
||
| 288 | * |
||
| 289 | * @param array $arguments |
||
| 290 | * |
||
| 291 | * @return Queue |
||
| 292 | */ |
||
| 293 | public function setArguments($arguments) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Get ticket. |
||
| 302 | * |
||
| 303 | * @return int |
||
| 304 | */ |
||
| 305 | public function getTicket() |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Set ticket. |
||
| 312 | * |
||
| 313 | * @param int $ticket |
||
| 314 | * |
||
| 315 | * @return Queue |
||
| 316 | */ |
||
| 317 | public function setTicket($ticket) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Get RabbitMq specific parameter : dead letter queue. |
||
| 326 | * |
||
| 327 | * @return Queue |
||
| 328 | */ |
||
| 329 | public function getDeadLetterExchanger() |
||
| 330 | { |
||
| 331 | return $this->deadLetterExchanger; |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Set RabbitMq specific parameter : dead letter queue. |
||
| 336 | * |
||
| 337 | * @param Exchange $exchange |
||
| 338 | * |
||
| 339 | * @return Queue |
||
| 340 | */ |
||
| 341 | public function setDeadLetterExchange(Exchange $exchange) |
||
| 342 | { |
||
| 343 | $this->deadLetterExchanger = $exchange; |
||
| 344 | |||
| 345 | return $this; |
||
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Get RabbitMq specific parameter : confirm. |
||
| 350 | * |
||
| 351 | * @return int |
||
| 352 | */ |
||
| 353 | public function getConfirm() |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Set RabbitMq specific parameter : confirm. |
||
| 360 | * |
||
| 361 | * @param int $confirm |
||
| 362 | * |
||
| 363 | * @return Queue |
||
| 364 | */ |
||
| 365 | public function setConfirm($confirm) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Get RabbitMq specific parameter : consumer_cancel_notify. |
||
| 374 | * |
||
| 375 | * @return bool |
||
| 376 | */ |
||
| 377 | public function getConsumerCancelNotify() |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Set RabbitMq specific parameter : consumer_cancel_notify. |
||
| 384 | * |
||
| 385 | * @param Queue $consumerCancelNotify |
||
| 386 | * |
||
| 387 | * @return Queue |
||
| 388 | */ |
||
| 389 | public function setConsumerCancelNotify(Queue $consumerCancelNotify) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Get RabbitMq specific parameter : alternate_exchange. |
||
| 398 | * |
||
| 399 | * @return Queue |
||
| 400 | */ |
||
| 401 | public function getAlternateExchange() |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Set RabbitMq specific parameter : alternate_exchange. |
||
| 408 | * |
||
| 409 | * @param Queue $alternateExchange |
||
| 410 | * |
||
| 411 | * @return Queue |
||
| 412 | */ |
||
| 413 | public function setAlternateExchange(Queue $alternateExchange) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Get RabbitMq specific parameter : ttl. |
||
| 422 | * |
||
| 423 | * @return int |
||
| 424 | */ |
||
| 425 | public function getTtl() |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Set RabbitMq specific parameter : ttl. |
||
| 432 | * |
||
| 433 | * @param int $ttl |
||
| 434 | * |
||
| 435 | * @return Queue |
||
| 436 | */ |
||
| 437 | public function setTtl($ttl) |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Get RabbitMq specific parameter : max length. |
||
| 446 | * |
||
| 447 | * @return int |
||
| 448 | */ |
||
| 449 | public function getMaxLength() |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Set RabbitMq specific parameter : max length. |
||
| 456 | * |
||
| 457 | * @param int $ttl |
||
| 458 | * |
||
| 459 | * @return Queue |
||
| 460 | */ |
||
| 461 | public function setMaxLength($maxLength) |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Get RabbitMq specific parameter : max priority. |
||
| 470 | * |
||
| 471 | * @return int |
||
| 472 | */ |
||
| 473 | public function getMaxPriority() |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Set RabbitMq specific parameter : max priority. |
||
| 480 | * |
||
| 481 | * @param int $maxPriority |
||
| 482 | * |
||
| 483 | * @return Queue |
||
| 484 | */ |
||
| 485 | public function setMaxPriority($maxPriority) |
||
| 491 | |||
| 492 | public function init(AMQPChannel $amqpChannel) |
||
| 493 | { |
||
| 494 | if (!$this->init) { |
||
| 495 | if ($this->deadLetterExchanger) { |
||
| 496 | $this->deadLetterExchanger->init($amqpChannel); |
||
| 497 | } |
||
| 498 | |||
| 499 | $parameters = []; |
||
| 500 | if ($this->alternateExchange !== null) { |
||
| 501 | $parameters['alternate-exchange'] = ['S', $this->alternateExchange->getName()]; |
||
| 502 | } |
||
| 503 | if ($this->confirm !== null) { |
||
| 504 | $parameters['confirm'] = ['I', $this->confirm]; |
||
| 505 | } |
||
| 506 | if ($this->consumerCancelNotify !== null) { |
||
| 507 | $parameters['consumer_cancel_notify'] = ['I', $this->consumerCancelNotify]; |
||
| 508 | } |
||
| 509 | if ($this->deadLetterExchanger !== null) { |
||
| 510 | $parameters['x-dead-letter-exchange'] = ['S', $this->deadLetterExchanger->getName()]; |
||
| 511 | } |
||
| 512 | if ($this->maxLength) { |
||
| 513 | $parameters['x-max-length'] = ['I', $this->maxLength]; |
||
| 514 | } |
||
| 515 | if ($this->maxPriority) { |
||
| 516 | $parameters['x-max-priority'] = ['I', $this->maxPriority]; |
||
| 517 | } |
||
| 518 | if ($this->ttl) { |
||
| 519 | $parameters['x-message-ttl'] = ['I', $this->ttl]; |
||
| 520 | } |
||
| 521 | |||
| 522 | if (!$parameters) { |
||
| 523 | $parameters = null; |
||
| 524 | } |
||
| 525 | $amqpChannel->queue_declare($this->name, $this->passive, $this->durable, $this->exclusive, $this->autoDelete, $this->noWait, $parameters); |
||
| 526 | $this->init = true; |
||
| 527 | } |
||
| 528 | } |
||
| 529 | |||
| 530 | public function consume() |
||
| 549 | } |
||
| 550 |
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.