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 Message 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 Message, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class Message |
||
18 | { |
||
19 | |||
20 | /** |
||
21 | * @var int |
||
22 | */ |
||
23 | protected $contentLength; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $contentType; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $contentEncoding; |
||
34 | |||
35 | /** |
||
36 | * @var int |
||
37 | */ |
||
38 | protected $tag; |
||
39 | |||
40 | /** |
||
41 | * @var bool |
||
42 | */ |
||
43 | protected $isDelivered = false; |
||
44 | |||
45 | /** |
||
46 | * @var ExchangeInterface |
||
47 | */ |
||
48 | protected $exchange; |
||
49 | |||
50 | /** |
||
51 | * @var string |
||
52 | */ |
||
53 | protected $routingKey; |
||
54 | |||
55 | /** |
||
56 | * @var QueueInterface |
||
57 | */ |
||
58 | protected $queue; |
||
59 | |||
60 | /** |
||
61 | * @var Channel |
||
62 | */ |
||
63 | protected $channel; |
||
64 | |||
65 | /** |
||
66 | * @var array |
||
67 | */ |
||
68 | protected $headers = []; |
||
69 | |||
70 | /** |
||
71 | * @var string |
||
72 | */ |
||
73 | protected $messageId; |
||
74 | |||
75 | /** |
||
76 | * @var int |
||
77 | */ |
||
78 | protected $deliveryMode; |
||
79 | |||
80 | /** |
||
81 | * @var int |
||
82 | */ |
||
83 | protected $priority; |
||
84 | |||
85 | /** |
||
86 | * @var string |
||
87 | */ |
||
88 | protected $correlationId; |
||
89 | |||
90 | /** |
||
91 | * @var string |
||
92 | */ |
||
93 | protected $replyTo; |
||
94 | |||
95 | /** |
||
96 | * @var string |
||
97 | */ |
||
98 | protected $expiration; |
||
99 | |||
100 | /** |
||
101 | * @var int |
||
102 | */ |
||
103 | protected $timestamp; |
||
104 | |||
105 | /** |
||
106 | * @var string |
||
107 | */ |
||
108 | protected $type; |
||
109 | |||
110 | /** |
||
111 | * @var string |
||
112 | */ |
||
113 | protected $userId; |
||
114 | |||
115 | /** |
||
116 | * @var string |
||
117 | */ |
||
118 | protected $appId; |
||
119 | |||
120 | /** |
||
121 | * @var string |
||
122 | */ |
||
123 | protected $clusterId; |
||
124 | |||
125 | /** |
||
126 | * @var string |
||
127 | */ |
||
128 | protected $content; |
||
129 | |||
130 | /** |
||
131 | * Acknowledge the message. |
||
132 | * |
||
133 | * @throws \InvalidArgumentException |
||
134 | * @throws \PHPDaemon\Clients\AMQP\Driver\Protocol\Exception\AMQPProtocolException |
||
135 | * @throws \PHPDaemon\Clients\AMQP\Driver\Exception\AMQPMessageException |
||
136 | */ |
||
137 | public function ack() |
||
146 | |||
147 | /** |
||
148 | * Not Acknowledge the message. |
||
149 | * |
||
150 | * @param null $multiple |
||
151 | * @param null $requeue |
||
152 | * @throws \InvalidArgumentException |
||
153 | * @throws \PHPDaemon\Clients\AMQP\Driver\Protocol\Exception\AMQPProtocolException |
||
154 | */ |
||
155 | View Code Duplication | public function nack($multiple = null, $requeue = null) |
|
163 | |||
164 | /** |
||
165 | * Reject the message and requeue it. |
||
166 | * |
||
167 | * @see ConsumerOptionsInterface::$noAck to consume messages without requiring |
||
168 | * excplicit acknowledgement by the consumer. |
||
169 | * |
||
170 | * @param bool $requeue |
||
171 | * @throws \InvalidArgumentException |
||
172 | * @throws \PHPDaemon\Clients\AMQP\Driver\Protocol\Exception\AMQPProtocolException |
||
173 | * @throws \PHPDaemon\Clients\AMQP\Driver\Exception\AMQPMessageException |
||
174 | */ |
||
175 | View Code Duplication | public function reject($requeue = true) |
|
186 | |||
187 | /** |
||
188 | * Get the length of the message content, in bytes. |
||
189 | * @return int |
||
190 | */ |
||
191 | public function getContentLength() |
||
195 | |||
196 | /** |
||
197 | * Set the length of the message content, in bytes. |
||
198 | * @param int $contentLength |
||
199 | * @return Message |
||
200 | */ |
||
201 | public function setContentLength($contentLength) |
||
206 | |||
207 | /** |
||
208 | * @return string |
||
209 | */ |
||
210 | public function getContentType() |
||
214 | |||
215 | /** |
||
216 | * @param string $contentType |
||
217 | * @return $this |
||
218 | */ |
||
219 | public function setContentType($contentType) |
||
224 | |||
225 | /** |
||
226 | * @return string |
||
227 | */ |
||
228 | public function getContentEncoding() |
||
232 | |||
233 | /** |
||
234 | * @param string $contentEncoding |
||
235 | * @return $this |
||
236 | */ |
||
237 | public function setContentEncoding($contentEncoding) |
||
242 | |||
243 | /** |
||
244 | * Get the delivery tag. |
||
245 | * @return int |
||
246 | */ |
||
247 | public function getTag() |
||
251 | |||
252 | /** |
||
253 | * Set the delivery tag. |
||
254 | * @param int $tag |
||
255 | * @return Message |
||
256 | */ |
||
257 | public function setTag($tag) |
||
262 | |||
263 | /** |
||
264 | * Check if the message has previously been delivered to a consumer but was |
||
265 | * implicitly or explicitly rejected. |
||
266 | * @return bool |
||
267 | */ |
||
268 | public function isRedelivered() |
||
272 | |||
273 | /** |
||
274 | * Get the name of the exchange that the message was published to. |
||
275 | * @return ExchangeInterface |
||
276 | */ |
||
277 | public function getExchange() |
||
281 | |||
282 | /** |
||
283 | * Set the name of the exchange that the message was published to. |
||
284 | * @param ExchangeInterface $exchange |
||
285 | * @return Message |
||
286 | */ |
||
287 | public function setExchange($exchange) |
||
292 | |||
293 | /** |
||
294 | * Get the routing key used when the message was published. |
||
295 | * @return string |
||
296 | */ |
||
297 | public function getRoutingKey() |
||
301 | |||
302 | /** |
||
303 | * Set the routing key used when the message was published. |
||
304 | * @param $routingKey |
||
305 | * @return Message |
||
306 | */ |
||
307 | public function setRoutingKey($routingKey) |
||
312 | |||
313 | /** |
||
314 | * @return QueueInterface |
||
315 | */ |
||
316 | public function getQueue() |
||
320 | |||
321 | /** |
||
322 | * @param QueueInterface $queue |
||
323 | * @return $this |
||
324 | */ |
||
325 | public function setQueue($queue) |
||
330 | |||
331 | /** |
||
332 | * @return Channel |
||
333 | */ |
||
334 | public function getChannel() |
||
338 | |||
339 | /** |
||
340 | * @param Channel $channel |
||
341 | * @return $this |
||
342 | */ |
||
343 | public function setChannel($channel) |
||
348 | |||
349 | /** |
||
350 | * @return string |
||
351 | */ |
||
352 | public function getContent() |
||
356 | |||
357 | /** |
||
358 | * @param string $content |
||
359 | * @return $this |
||
360 | */ |
||
361 | public function setContent($content) |
||
366 | |||
367 | /** |
||
368 | * Проеряет, что канал еще жив |
||
369 | * |
||
370 | * @throws AMQPMessageException |
||
371 | */ |
||
372 | private function checkChannel() |
||
384 | |||
385 | /** |
||
386 | * @return string |
||
387 | */ |
||
388 | public function getCorrelationId() |
||
392 | |||
393 | /** |
||
394 | * @param string $correlationId |
||
395 | * @return $this |
||
396 | */ |
||
397 | public function setCorrelationId($correlationId) |
||
402 | |||
403 | /** |
||
404 | * @return string |
||
405 | */ |
||
406 | public function getReplyTo() |
||
410 | |||
411 | /** |
||
412 | * @param string $replyTo |
||
413 | * @return $this |
||
414 | */ |
||
415 | public function setReplyTo($replyTo) |
||
420 | |||
421 | /** |
||
422 | * @return bool |
||
423 | */ |
||
424 | public function isDelivered() |
||
428 | |||
429 | /** |
||
430 | * @param bool $isDelivered |
||
431 | * @return $this |
||
432 | */ |
||
433 | public function setIsDelivered($isDelivered) |
||
438 | |||
439 | /** |
||
440 | * @return array |
||
441 | */ |
||
442 | public function getHeaders() |
||
446 | |||
447 | /** |
||
448 | * @param array $headers |
||
449 | * @return $this |
||
450 | */ |
||
451 | public function setHeaders($headers) |
||
456 | |||
457 | /** |
||
458 | * @return string |
||
459 | */ |
||
460 | public function getMessageId() |
||
464 | |||
465 | /** |
||
466 | * @param string $messageId |
||
467 | * @return $this |
||
468 | */ |
||
469 | public function setMessageId($messageId) |
||
474 | |||
475 | /** |
||
476 | * @return int |
||
477 | */ |
||
478 | public function getDeliveryMode() |
||
482 | |||
483 | /** |
||
484 | * @param int $deliveryMode |
||
485 | * @return $this |
||
486 | */ |
||
487 | public function setDeliveryMode($deliveryMode) |
||
492 | |||
493 | /** |
||
494 | * @return int |
||
495 | */ |
||
496 | public function getPriority() |
||
500 | |||
501 | /** |
||
502 | * @param int $priority |
||
503 | * @return $this |
||
504 | */ |
||
505 | public function setPriority($priority) |
||
510 | |||
511 | /** |
||
512 | * @return string |
||
513 | */ |
||
514 | public function getExpiration() |
||
518 | |||
519 | /** |
||
520 | * @param string $expiration |
||
521 | * @return $this |
||
522 | */ |
||
523 | public function setExpiration($expiration) |
||
528 | |||
529 | /** |
||
530 | * @return int |
||
531 | */ |
||
532 | public function getTimestamp() |
||
536 | |||
537 | /** |
||
538 | * @param int $timestamp |
||
539 | * @return $this |
||
540 | */ |
||
541 | public function setTimestamp($timestamp) |
||
546 | |||
547 | /** |
||
548 | * @return string |
||
549 | */ |
||
550 | public function getType() |
||
554 | |||
555 | /** |
||
556 | * @param string $type |
||
557 | * @return $this |
||
558 | */ |
||
559 | public function setType($type) |
||
564 | |||
565 | /** |
||
566 | * @return string |
||
567 | */ |
||
568 | public function getUserId() |
||
572 | |||
573 | /** |
||
574 | * @param string $userId |
||
575 | * @return $this |
||
576 | */ |
||
577 | public function setUserId($userId) |
||
582 | |||
583 | /** |
||
584 | * @return string |
||
585 | */ |
||
586 | public function getAppId() |
||
590 | |||
591 | /** |
||
592 | * @param string $appId |
||
593 | * @return $this |
||
594 | */ |
||
595 | public function setAppId($appId) |
||
600 | |||
601 | /** |
||
602 | * @return string |
||
603 | */ |
||
604 | public function getClusterId() |
||
608 | |||
609 | /** |
||
610 | * @param string $clusterId |
||
611 | * @return $this |
||
612 | */ |
||
613 | public function setClusterId($clusterId) |
||
618 | } |
||
619 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.