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 |
||
30 | class DictionaryClient extends AbstractServiceClient |
||
31 | { |
||
32 | /** |
||
33 | * @const |
||
34 | */ |
||
35 | const FAMILY_FLAG = 0x0001; |
||
36 | |||
37 | /** |
||
38 | * @const |
||
39 | */ |
||
40 | const MORPHO_FLAG = 0x0004; |
||
41 | |||
42 | /** |
||
43 | * @const |
||
44 | */ |
||
45 | const POSITION_FILTER_FLAG = 0x0008; |
||
46 | |||
47 | /** |
||
48 | * @var |
||
49 | */ |
||
50 | protected $apiKey; |
||
51 | |||
52 | /** |
||
53 | * @var string |
||
54 | */ |
||
55 | protected $serviceDomain = 'dictionary.yandex.net'; |
||
56 | |||
57 | /** |
||
58 | * @var |
||
59 | */ |
||
60 | protected $uiLanguage = 'en'; |
||
61 | |||
62 | /** |
||
63 | * @var |
||
64 | */ |
||
65 | protected $translateFrom = 'en'; |
||
66 | |||
67 | /** |
||
68 | * @var |
||
69 | */ |
||
70 | protected $translateTo = 'en'; |
||
71 | |||
72 | /** |
||
73 | * @var |
||
74 | */ |
||
75 | protected $flags = 0; |
||
76 | |||
77 | /** |
||
78 | * @param string $apiKey API key |
||
79 | */ |
||
80 | 13 | public function __construct($apiKey) |
|
84 | |||
85 | /** |
||
86 | * @param string $apiKey |
||
87 | * |
||
88 | * @return $this |
||
89 | */ |
||
90 | 13 | public function setApiKey($apiKey) |
|
96 | |||
97 | /** |
||
98 | * @param boolean $enabled optional boolean |
||
99 | * |
||
100 | * @return $this |
||
101 | */ |
||
102 | 2 | public function setFamilyFlag($enabled = true) |
|
108 | |||
109 | /** |
||
110 | * @param boolean $enabled optional boolean |
||
111 | * |
||
112 | * @return $this |
||
113 | */ |
||
114 | 2 | public function setMorphoFlag($enabled = true) |
|
120 | |||
121 | /** |
||
122 | * @param boolean $enabled optional boolean |
||
123 | * |
||
124 | * @return $this |
||
125 | */ |
||
126 | 2 | public function setPositionFilterFlag($enabled = true) |
|
132 | |||
133 | /** |
||
134 | * @return integer |
||
135 | */ |
||
136 | 9 | public function getFlags() |
|
140 | |||
141 | /** |
||
142 | * @param integer $flag |
||
143 | * @param boolean $enabled optional boolean |
||
144 | * |
||
145 | * @return $this |
||
146 | */ |
||
147 | 6 | public function setFlag($flag, $enabled = true) |
|
157 | |||
158 | /** |
||
159 | * @return string |
||
160 | */ |
||
161 | 8 | public function getApiKey() |
|
165 | |||
166 | /** |
||
167 | * @param string $uiLanguage |
||
168 | * |
||
169 | * @return $this |
||
170 | */ |
||
171 | 2 | public function setUiLanguage($uiLanguage) |
|
177 | |||
178 | /** |
||
179 | * @return string |
||
180 | */ |
||
181 | 5 | public function getUiLanguage() |
|
185 | |||
186 | /** |
||
187 | * @param string $translateFrom |
||
188 | * |
||
189 | * @return $this |
||
190 | */ |
||
191 | 5 | public function setTranslateFrom($translateFrom) |
|
197 | |||
198 | /** |
||
199 | * @return string |
||
200 | */ |
||
201 | 2 | public function getTranslateFrom() |
|
205 | |||
206 | /** |
||
207 | * @param $translateTo |
||
208 | * |
||
209 | * @return $this |
||
210 | */ |
||
211 | 5 | public function setTranslateTo($translateTo) |
|
217 | |||
218 | /** |
||
219 | * @return string |
||
220 | */ |
||
221 | 2 | public function getTranslateTo() |
|
225 | |||
226 | /** |
||
227 | * @return string |
||
228 | */ |
||
229 | 3 | public function getLanguage() |
|
233 | |||
234 | /** |
||
235 | * @param string $text |
||
236 | * |
||
237 | * @return string |
||
238 | */ |
||
239 | 3 | public function getLookupUrl($text) |
|
255 | |||
256 | /** |
||
257 | * @return string |
||
258 | */ |
||
259 | 4 | public function getGetLanguagesUrl() |
|
271 | |||
272 | /** |
||
273 | * Looks up a text in the dictionary |
||
274 | * |
||
275 | * @param string $text |
||
276 | * |
||
277 | * @return array|bool |
||
278 | * |
||
279 | * @throws DictionaryException |
||
280 | * @throws ForbiddenException |
||
281 | */ |
||
282 | 3 | View Code Duplication | public function lookup($text) |
298 | |||
299 | /** |
||
300 | * @return array|bool |
||
301 | * |
||
302 | * @throws DictionaryException |
||
303 | * @throws ForbiddenException |
||
304 | */ |
||
305 | 4 | View Code Duplication | public function getLanguages() |
321 | |||
322 | /** |
||
323 | * @param Response $response |
||
324 | * |
||
325 | * @return array |
||
326 | */ |
||
327 | 2 | protected function parseLookupResponse(Response $response) |
|
338 | |||
339 | /** |
||
340 | * @param Response $response |
||
341 | * |
||
342 | * @return array |
||
343 | */ |
||
344 | 1 | protected function parseGetLanguagesResponse(Response $response) |
|
357 | |||
358 | /** |
||
359 | * Sends a request |
||
360 | * |
||
361 | * @param string $method HTTP method |
||
362 | * @param string|UriInterface $uri URI object or string. |
||
363 | * @param array $options Request options to apply. |
||
364 | * |
||
365 | * @return Response |
||
366 | * |
||
367 | * @throws ForbiddenException |
||
368 | * @throws DictionaryException |
||
369 | */ |
||
370 | 7 | protected function sendRequest($method, $uri, array $options = []) |
|
391 | } |
||
392 |
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.