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 |
||
18 | class Exchange |
||
19 | { |
||
20 | /** @var string */ |
||
21 | private $name; |
||
22 | |||
23 | /** @var string */ |
||
24 | private $type; |
||
25 | |||
26 | /** @var bool */ |
||
27 | private $passive = false; |
||
28 | |||
29 | /** @var bool */ |
||
30 | private $durable = false; |
||
31 | |||
32 | /** @var bool */ |
||
33 | private $internal = false; |
||
34 | |||
35 | /** @var bool */ |
||
36 | private $autoDelete = false; |
||
37 | |||
38 | /** @var bool */ |
||
39 | private $nowait = false; |
||
40 | |||
41 | /** @var array|null */ |
||
42 | private $arguments = null; |
||
43 | |||
44 | /** @var int|null */ |
||
45 | private $ticket = null; |
||
46 | |||
47 | |||
48 | /** |
||
49 | * Named constructor to get more fluent interface |
||
50 | * @param $name |
||
51 | * @param $type |
||
52 | * @return static |
||
53 | */ |
||
54 | public static function factory($name, $type) |
||
58 | |||
59 | /** |
||
60 | * Exchange constructor. |
||
61 | * @param string $name Exchange name |
||
62 | * @param string $type Exchange type. |
||
63 | * |
||
64 | * @see https://www.rabbitmq.com/tutorials/amqp-concepts.html#exchanges |
||
65 | * @throws \AmqpWorkers\Exception\DefinitionException |
||
66 | */ |
||
67 | public function __construct($name, $type) |
||
79 | |||
80 | /** |
||
81 | * @return string |
||
82 | */ |
||
83 | public function getName() |
||
87 | |||
88 | /** |
||
89 | * @return string |
||
90 | */ |
||
91 | public function getType() |
||
95 | |||
96 | /** |
||
97 | * @see https://www.rabbitmq.com/amqp-0-9-1-reference.html#exchange.declare.passive |
||
98 | * @default false |
||
99 | * @param $passive |
||
100 | * @return Exchange $this |
||
101 | */ |
||
102 | public function passive($passive) |
||
107 | |||
108 | /** |
||
109 | * @see https://www.rabbitmq.com/amqp-0-9-1-reference.html#exchange.declare.durable |
||
110 | * @default false |
||
111 | * @param bool $durable |
||
112 | * @return Exchange $this |
||
113 | */ |
||
114 | public function durable($durable) |
||
119 | |||
120 | /** |
||
121 | * @see https://www.rabbitmq.com/amqp-0-9-1-reference.html#exchange.declare.auto-delete |
||
122 | * @default false |
||
123 | * @param bool $autoDelete |
||
124 | * @return Exchange $this |
||
125 | */ |
||
126 | public function autoDelete($autoDelete) |
||
131 | |||
132 | /** |
||
133 | * @see * @see https://www.rabbitmq.com/amqp-0-9-1-reference.html#exchange.declare.internal |
||
134 | * @default false |
||
135 | * @param $internal |
||
136 | * @return Exchange $this |
||
137 | */ |
||
138 | public function internal($internal) |
||
143 | |||
144 | /** |
||
145 | * @see https://www.rabbitmq.com/amqp-0-9-1-reference.html#exchange.declare.no-wait |
||
146 | * @default false |
||
147 | * @param bool $nowait |
||
148 | * @return Exchange $this |
||
149 | */ |
||
150 | public function nowait($nowait) |
||
155 | |||
156 | /** |
||
157 | * @see https://www.rabbitmq.com/amqp-0-9-1-reference.html#queue.declare.arguments |
||
158 | * @default null |
||
159 | * @param array $arguments |
||
160 | * @return $this |
||
161 | */ |
||
162 | public function arguments(array $arguments) |
||
167 | |||
168 | /** |
||
169 | * @default null |
||
170 | * @param int|null $ticket |
||
171 | * @return $this |
||
172 | */ |
||
173 | public function ticket($ticket) |
||
178 | |||
179 | /** |
||
180 | * Returns the list of parameters for exchange_declare |
||
181 | * |
||
182 | * @see \PhpAmqpLib\Channel\AMQPChannel::exchange_declare() |
||
183 | * @return array |
||
184 | */ |
||
185 | View Code Duplication | public function listParams() |
|
197 | } |
||
198 |
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.