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 = []) |
||
142 | { |
||
143 | $this->client = $client; |
||
144 | $this->client->register($this); |
||
145 | $this->name = $name; |
||
146 | $this->consumers = $consumers; |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * Get queue name. |
||
151 | * |
||
152 | * @return string |
||
153 | */ |
||
154 | public function getName() |
||
155 | { |
||
156 | return $this->name; |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * Get passive. |
||
161 | * |
||
162 | * @return bool |
||
163 | */ |
||
164 | public function getPassive() |
||
165 | { |
||
166 | return $this->passive; |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * @param bool $passive |
||
171 | * |
||
172 | * @return Queue |
||
173 | */ |
||
174 | public function setPassive($passive) |
||
175 | { |
||
176 | $this->passive = $passive; |
||
177 | |||
178 | return $this; |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Get durable. |
||
183 | * |
||
184 | * @return bool |
||
185 | */ |
||
186 | public function getDurable() |
||
187 | { |
||
188 | return $this->durable; |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * Set durable. |
||
193 | * |
||
194 | * @param bool $durable |
||
195 | * |
||
196 | * @return Queue |
||
197 | */ |
||
198 | public function setDurable($durable) |
||
199 | { |
||
200 | $this->durable = $durable; |
||
201 | |||
202 | return $this; |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * Get exclusive. |
||
207 | * |
||
208 | * @return bool |
||
209 | */ |
||
210 | public function getExclusive() |
||
211 | { |
||
212 | return $this->exclusive; |
||
213 | } |
||
214 | |||
215 | /** |
||
216 | * Set exclusive. |
||
217 | * |
||
218 | * @param bool $exclusive |
||
219 | * |
||
220 | * @return Queue |
||
221 | */ |
||
222 | public function setExclusive($exclusive) |
||
223 | { |
||
224 | $this->exclusive = $exclusive; |
||
225 | |||
226 | return $this; |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * Get autoDelete. |
||
231 | * |
||
232 | * @return bool |
||
233 | */ |
||
234 | public function getAutoDelete() |
||
235 | { |
||
236 | return $this->autoDelete; |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * Set autoDelete. |
||
241 | * |
||
242 | * @param bool $autoDelete |
||
243 | * |
||
244 | * @return Queue |
||
245 | */ |
||
246 | public function setAutoDelete($autoDelete) |
||
247 | { |
||
248 | $this->autoDelete = $autoDelete; |
||
249 | |||
250 | return $this; |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * Get noWait. |
||
255 | * |
||
256 | * @return bool |
||
257 | */ |
||
258 | public function getNoWait() |
||
259 | { |
||
260 | return $this->noWait; |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * Set noWait. |
||
265 | * |
||
266 | * @param bool $noWait |
||
267 | * |
||
268 | * @return Queue |
||
269 | */ |
||
270 | public function setNoWait($noWait) |
||
271 | { |
||
272 | $this->noWait = $noWait; |
||
273 | |||
274 | return $this; |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * Get arguments. |
||
279 | * |
||
280 | * @return array|null |
||
281 | */ |
||
282 | public function getArguments() |
||
283 | { |
||
284 | return $this->arguments; |
||
285 | } |
||
286 | |||
287 | /** |
||
288 | * Set arguments. |
||
289 | * |
||
290 | * @param array $arguments |
||
291 | * |
||
292 | * @return Queue |
||
293 | */ |
||
294 | public function setArguments($arguments) |
||
295 | { |
||
296 | $this->arguments = $arguments; |
||
297 | |||
298 | return $this; |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * Get ticket. |
||
303 | * |
||
304 | * @return int |
||
305 | */ |
||
306 | public function getTicket() |
||
307 | { |
||
308 | return $this->ticket; |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * Set ticket. |
||
313 | * |
||
314 | * @param int $ticket |
||
315 | * |
||
316 | * @return Queue |
||
317 | */ |
||
318 | public function setTicket($ticket) |
||
319 | { |
||
320 | $this->ticket = $ticket; |
||
321 | |||
322 | return $this; |
||
323 | } |
||
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() |
||
355 | { |
||
356 | return $this->confirm; |
||
357 | } |
||
358 | |||
359 | /** |
||
360 | * Set RabbitMq specific parameter : confirm. |
||
361 | * |
||
362 | * @param int $confirm |
||
363 | * |
||
364 | * @return Queue |
||
365 | */ |
||
366 | public function setConfirm($confirm) |
||
367 | { |
||
368 | $this->confirm = $confirm; |
||
369 | |||
370 | return $this; |
||
371 | } |
||
372 | |||
373 | /** |
||
374 | * Get RabbitMq specific parameter : consumer_cancel_notify. |
||
375 | * |
||
376 | * @return bool |
||
377 | */ |
||
378 | public function getConsumerCancelNotify() |
||
379 | { |
||
380 | return $this->consumerCancelNotify; |
||
381 | } |
||
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) |
||
391 | { |
||
392 | $this->consumerCancelNotify = $consumerCancelNotify; |
||
393 | |||
394 | return $this; |
||
395 | } |
||
396 | |||
397 | /** |
||
398 | * Get RabbitMq specific parameter : alternate_exchange. |
||
399 | * |
||
400 | * @return Queue |
||
401 | */ |
||
402 | public function getAlternateExchange() |
||
403 | { |
||
404 | return $this->alternateExchange; |
||
405 | } |
||
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) |
||
415 | { |
||
416 | $this->alternateExchange = $alternateExchange; |
||
417 | |||
418 | return $this; |
||
419 | } |
||
420 | |||
421 | /** |
||
422 | * Get RabbitMq specific parameter : ttl. |
||
423 | * |
||
424 | * @return int |
||
425 | */ |
||
426 | public function getTtl() |
||
427 | { |
||
428 | return $this->ttl; |
||
429 | } |
||
430 | |||
431 | /** |
||
432 | * Set RabbitMq specific parameter : ttl. |
||
433 | * |
||
434 | * @param int $ttl |
||
435 | * |
||
436 | * @return Queue |
||
437 | */ |
||
438 | public function setTtl($ttl) |
||
439 | { |
||
440 | $this->ttl = $ttl; |
||
441 | |||
442 | return $this; |
||
443 | } |
||
444 | |||
445 | /** |
||
446 | * Get RabbitMq specific parameter : max length. |
||
447 | * |
||
448 | * @return int |
||
449 | */ |
||
450 | public function getMaxLength() |
||
451 | { |
||
452 | return $this->maxLength; |
||
453 | } |
||
454 | |||
455 | /** |
||
456 | * Set RabbitMq specific parameter : max length. |
||
457 | * |
||
458 | * @param int $maxLength |
||
459 | * |
||
460 | * @return Queue |
||
461 | */ |
||
462 | public function setMaxLength($maxLength) |
||
463 | { |
||
464 | $this->maxLength = $maxLength; |
||
465 | |||
466 | return $this; |
||
467 | } |
||
468 | |||
469 | /** |
||
470 | * Get RabbitMq specific parameter : max priority. |
||
471 | * |
||
472 | * @return int |
||
473 | */ |
||
474 | public function getMaxPriority() |
||
475 | { |
||
476 | return $this->maxPriority; |
||
477 | } |
||
478 | |||
479 | /** |
||
480 | * Set RabbitMq specific parameter : max priority. |
||
481 | * |
||
482 | * @param int $maxPriority |
||
483 | * |
||
484 | * @return Queue |
||
485 | */ |
||
486 | public function setMaxPriority($maxPriority) |
||
487 | { |
||
488 | $this->maxPriority = $maxPriority; |
||
489 | |||
490 | return $this; |
||
491 | } |
||
492 | |||
493 | public function init(AMQPChannel $amqpChannel) |
||
494 | { |
||
495 | if (!$this->init) { |
||
496 | if ($this->deadLetterExchanger) { |
||
497 | $this->deadLetterExchanger->init($amqpChannel); |
||
498 | } |
||
499 | |||
500 | $parameters = []; |
||
501 | if ($this->alternateExchange !== null) { |
||
502 | $parameters['alternate-exchange'] = ['S', $this->alternateExchange->getName()]; |
||
503 | } |
||
504 | if ($this->confirm !== null) { |
||
505 | $parameters['confirm'] = ['I', $this->confirm]; |
||
506 | } |
||
507 | if ($this->consumerCancelNotify !== null) { |
||
508 | $parameters['consumer_cancel_notify'] = ['I', $this->consumerCancelNotify]; |
||
509 | } |
||
510 | if ($this->deadLetterExchanger !== null) { |
||
511 | $parameters['x-dead-letter-exchange'] = ['S', $this->deadLetterExchanger->getName()]; |
||
512 | } |
||
513 | if ($this->maxLength) { |
||
514 | $parameters['x-max-length'] = ['I', $this->maxLength]; |
||
515 | } |
||
516 | if ($this->maxPriority) { |
||
517 | $parameters['x-max-priority'] = ['I', $this->maxPriority]; |
||
518 | } |
||
519 | if ($this->ttl) { |
||
520 | $parameters['x-message-ttl'] = ['I', $this->ttl]; |
||
521 | } |
||
522 | |||
523 | if (!$parameters) { |
||
524 | $parameters = null; |
||
525 | } |
||
526 | $amqpChannel->queue_declare($this->name, $this->passive, $this->durable, $this->exclusive, $this->autoDelete, $this->noWait, $parameters); |
||
527 | $this->init = true; |
||
528 | } |
||
529 | } |
||
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, |
|
579 | $immediate = false, |
||
580 | $ticket = null) |
||
581 | { |
||
582 | $channel = $this->client->getChannel(); |
||
583 | |||
584 | $channel->basic_publish($message->toAMQPMessage(), '', $this->name, $mandatory, $immediate, $ticket); |
||
585 | } |
||
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
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.