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:
1 | <?php |
||
12 | class RpcClient implements QueueProducerInterface |
||
13 | { |
||
14 | private $connectionManager; |
||
15 | private $channel; |
||
16 | private $fromName; |
||
17 | private $queueName; |
||
18 | private $response; |
||
19 | private $correlationId; |
||
20 | private $callbackQueue; |
||
21 | |||
22 | public function __construct($queueName, ConnectionManager $manager, $fromName = '') |
||
28 | |||
29 | /** |
||
30 | * @param AMQPMessage $rep |
||
31 | */ |
||
32 | public function onResponse(AMQPMessage $rep) |
||
38 | |||
39 | /** |
||
40 | * @return \PhpAmqpLib\Channel\AMQPChannel |
||
41 | * |
||
42 | * @throws \Cmobi\RabbitmqBundle\Connection\Exception\NotFoundAMQPConnectionFactoryException |
||
43 | */ |
||
44 | View Code Duplication | public function refreshChannel() |
|
55 | |||
56 | /** |
||
57 | * @param $data |
||
58 | * @param int $expire |
||
59 | * @param int $priority |
||
60 | * @throws QueueNotFoundException |
||
61 | * @throws \Cmobi\RabbitmqBundle\Connection\Exception\NotFoundAMQPConnectionFactoryException |
||
62 | */ |
||
63 | public function publish($data, $expire = self::DEFAULT_TTL, $priority = self::PRIORITY_LOW) |
||
101 | |||
102 | /** |
||
103 | * @return bool |
||
104 | */ |
||
105 | View Code Duplication | public function queueHasExists() |
|
115 | |||
116 | /** |
||
117 | * @return string |
||
118 | */ |
||
119 | public function getQueueName() |
||
123 | |||
124 | /** |
||
125 | * @return CmobiAMQPChannel |
||
126 | */ |
||
127 | public function getChannel() |
||
131 | |||
132 | /** |
||
133 | * @return string |
||
134 | */ |
||
135 | public function getFromName() |
||
139 | |||
140 | /** |
||
141 | * @todo unecessary method set, its only exists to run tests whitout stay jailed in infinite while waiting response. |
||
142 | * |
||
143 | * @param $content |
||
144 | */ |
||
145 | public function setResponse($content) |
||
149 | |||
150 | /** |
||
151 | * @return string |
||
152 | */ |
||
153 | public function getResponse() |
||
157 | |||
158 | /** @return string */ |
||
159 | public function generateCorrelationId() |
||
163 | |||
164 | /** |
||
165 | * @return string |
||
166 | */ |
||
167 | public function getCurrentCorrelationId() |
||
171 | |||
172 | /** |
||
173 | * @return string |
||
174 | */ |
||
175 | public function getExchange() |
||
179 | |||
180 | /** |
||
181 | * @return string |
||
182 | */ |
||
183 | public function getExchangeType() |
||
187 | } |
||
188 |
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.