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 |
||
31 | class Client |
||
32 | { |
||
33 | /** |
||
34 | * The multicast address |
||
35 | */ |
||
36 | const MULTICAST_ADDRESS = '239.255.255.250'; |
||
37 | |||
38 | /** |
||
39 | * The multicast port |
||
40 | */ |
||
41 | const MULTICAST_PORT = 1900; |
||
42 | |||
43 | /** |
||
44 | * @var LoopInterface |
||
45 | */ |
||
46 | private $loop; |
||
47 | |||
48 | /** |
||
49 | * @var MulticastFactory |
||
50 | */ |
||
51 | private $multicastFactory; |
||
52 | |||
53 | /** |
||
54 | * @var AliveFactory |
||
55 | */ |
||
56 | protected $aliveRequestFactory; |
||
57 | |||
58 | /** |
||
59 | * @var ByebyeFactory |
||
60 | */ |
||
61 | protected $byebyeRequestFactory; |
||
62 | |||
63 | /** |
||
64 | * @var DiscoverFactory |
||
65 | */ |
||
66 | protected $discoverRequestFactory; |
||
67 | |||
68 | /** |
||
69 | * @var UpdateFactory |
||
70 | */ |
||
71 | protected $updateRequestFactory; |
||
72 | |||
73 | /** |
||
74 | * Create client object |
||
75 | * |
||
76 | * @param LoopInterface $loop |
||
77 | */ |
||
78 | 4 | public function __construct(LoopInterface $loop) |
|
82 | |||
83 | /** |
||
84 | * Get multicast factory |
||
85 | * |
||
86 | * @return MulticastFactory |
||
87 | */ |
||
88 | 4 | public function getMulticastFactory() |
|
96 | |||
97 | /** |
||
98 | * Set multicast factory |
||
99 | * |
||
100 | * @param MulticastFactory $multicastFactory |
||
101 | * |
||
102 | * @return $this |
||
103 | */ |
||
104 | 2 | public function setMulticastFactory(MulticastFactory $multicastFactory) |
|
109 | |||
110 | /** |
||
111 | * Get alive request factory |
||
112 | * |
||
113 | * @return AliveFactory |
||
114 | */ |
||
115 | public function getAliveRequestFactory() |
||
123 | |||
124 | /** |
||
125 | * Get byebye request factory |
||
126 | * |
||
127 | * @return ByebyeFactory |
||
128 | */ |
||
129 | public function getByebyeRequestFactory() |
||
137 | |||
138 | /** |
||
139 | * Get discover request factory |
||
140 | * |
||
141 | * @return DiscoverFactory |
||
142 | */ |
||
143 | 4 | protected function getDiscoverRequestFactory() |
|
151 | |||
152 | /** |
||
153 | * Get update request factory |
||
154 | * |
||
155 | * @return UpdateFactory |
||
156 | */ |
||
157 | public function getUpdateRequestFactory() |
||
165 | |||
166 | /** |
||
167 | * Send alive request |
||
168 | * |
||
169 | * @param AliveOptions $options |
||
170 | * |
||
171 | * @return PromiseInterface |
||
172 | */ |
||
173 | View Code Duplication | public function alive(AliveOptions $options) |
|
184 | |||
185 | /** |
||
186 | * Send byebye request |
||
187 | * |
||
188 | * @param ByebyeOptions $options |
||
189 | * |
||
190 | * @return PromiseInterface |
||
191 | */ |
||
192 | View Code Duplication | public function byebye(ByebyeOptions $options) |
|
203 | |||
204 | /** |
||
205 | * Send discover request |
||
206 | * |
||
207 | * @param DiscoverOptions $options |
||
208 | * @param int $timeout |
||
209 | * |
||
210 | * @return PromiseInterface |
||
211 | */ |
||
212 | 4 | public function discover(DiscoverOptions $options, $timeout = 2) |
|
241 | |||
242 | /** |
||
243 | * Send update request |
||
244 | * |
||
245 | * @param UpdateOptions $options |
||
246 | * |
||
247 | * @return PromiseInterface |
||
248 | */ |
||
249 | View Code Duplication | public function update(UpdateOptions $options) |
|
260 | |||
261 | /** |
||
262 | * Parse message |
||
263 | * |
||
264 | * @param string $message |
||
265 | * |
||
266 | * @return Message |
||
267 | */ |
||
268 | protected function parseMessage($message) |
||
300 | } |
||
301 |
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.