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 |
||
32 | class Client |
||
33 | { |
||
34 | /** |
||
35 | * The multicast address |
||
36 | */ |
||
37 | const MULTICAST_ADDRESS = '239.255.255.250'; |
||
38 | |||
39 | /** |
||
40 | * The multicast port |
||
41 | */ |
||
42 | const MULTICAST_PORT = 1900; |
||
43 | |||
44 | /** |
||
45 | * @var EventDispatcherInterface |
||
46 | */ |
||
47 | protected $eventDispatcher; |
||
48 | |||
49 | /** |
||
50 | * @var MulticastFactory |
||
51 | */ |
||
52 | protected $multicastFactory; |
||
53 | |||
54 | /** |
||
55 | * @var RequestFactory |
||
56 | */ |
||
57 | protected $requestFactory; |
||
58 | |||
59 | /** |
||
60 | * Get event dispatcher |
||
61 | * |
||
62 | * @return EventDispatcherInterface |
||
63 | */ |
||
64 | public function getEventDispatcher() |
||
72 | |||
73 | /** |
||
74 | * Set event dispatcher |
||
75 | * |
||
76 | * @param EventDispatcherInterface $eventDispatcher |
||
77 | * |
||
78 | * @return $this |
||
79 | */ |
||
80 | 2 | public function setEventDispatcher(EventDispatcherInterface $eventDispatcher) |
|
86 | |||
87 | /** |
||
88 | * Get multicast factory |
||
89 | * |
||
90 | * @return MulticastFactory |
||
91 | */ |
||
92 | 2 | public function getMulticastFactory() |
|
100 | |||
101 | /** |
||
102 | * Set multicast factory |
||
103 | * |
||
104 | * @param MulticastFactory $multicastFactory |
||
105 | * |
||
106 | * @return $this |
||
107 | */ |
||
108 | 2 | public function setMulticastFactory(MulticastFactory $multicastFactory) |
|
114 | |||
115 | /** |
||
116 | * Get request factory |
||
117 | * |
||
118 | * @return RequestFactory |
||
119 | */ |
||
120 | 2 | public function getRequestFactory() |
|
128 | |||
129 | /** |
||
130 | * Set request factory |
||
131 | * |
||
132 | * @param RequestFactory $requestFactory |
||
133 | * |
||
134 | * @return $this |
||
135 | */ |
||
136 | 2 | public function setRequestFactory(RequestFactory $requestFactory) |
|
142 | |||
143 | /** |
||
144 | * Send request |
||
145 | * |
||
146 | * @param Socket $socket |
||
147 | * @param RequestInterface $request |
||
148 | * @param callable $onSuccess |
||
149 | * @param callable $onFailure |
||
150 | * |
||
151 | * @return boolean |
||
152 | */ |
||
153 | 2 | protected function sendRequest(Socket $socket, RequestInterface $request, $onSuccess, $onFailure) |
|
165 | |||
166 | /** |
||
167 | * Receive response |
||
168 | * |
||
169 | * @param Socket $socket |
||
170 | * @param callable $onSuccess |
||
171 | * @param callable $onFailure |
||
172 | * |
||
173 | * @return boolean |
||
174 | */ |
||
175 | 2 | protected function receiveResponse(Socket $socket, $onSuccess, $onFailure) |
|
185 | |||
186 | /** |
||
187 | * Send alive request |
||
188 | * |
||
189 | * @param AliveOptions $options |
||
190 | * |
||
191 | * @return boolean |
||
192 | */ |
||
193 | View Code Duplication | public function alive(AliveOptions $options) |
|
214 | |||
215 | /** |
||
216 | * Send byebye request |
||
217 | * |
||
218 | * @param ByebyeOptions $options |
||
219 | * |
||
220 | * @return boolean |
||
221 | */ |
||
222 | View Code Duplication | public function byebye(ByebyeOptions $options) |
|
243 | |||
244 | /** |
||
245 | * Send discover request |
||
246 | * |
||
247 | * @param DiscoverOptions $options |
||
248 | * @param int $timeout |
||
249 | * |
||
250 | * @return boolean |
||
251 | */ |
||
252 | 2 | public function discover(DiscoverOptions $options, $timeout = 2) |
|
302 | |||
303 | /** |
||
304 | * Send update request |
||
305 | * |
||
306 | * @param UpdateOptions $options |
||
307 | * |
||
308 | * @return boolean |
||
309 | */ |
||
310 | View Code Duplication | public function update(UpdateOptions $options) |
|
331 | |||
332 | /** |
||
333 | * Create discover event |
||
334 | * |
||
335 | * @param string $message |
||
336 | * |
||
337 | * @return DiscoverEvent |
||
338 | */ |
||
339 | protected function createDiscoverEvent($message) |
||
371 | } |
||
372 |
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.