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 |
||
23 | class Services extends Api |
||
24 | { |
||
25 | /** |
||
26 | * The response array. |
||
27 | * |
||
28 | * @var array|null |
||
29 | */ |
||
30 | protected $response = null; |
||
31 | |||
32 | /** |
||
33 | * The cache directory. |
||
34 | * |
||
35 | * @var string|null |
||
36 | */ |
||
37 | protected $cacheDir = __DIR__ . '/cache/'; |
||
38 | |||
39 | /** |
||
40 | * The cache file name. |
||
41 | * |
||
42 | * @var string|null |
||
43 | */ |
||
44 | protected $file = __DIR__ . '/cache/' . 'services_'; |
||
45 | |||
46 | /** |
||
47 | * Returns a list of all the services categories. |
||
48 | * |
||
49 | * @param string $lang |
||
50 | * @return Services |
||
51 | */ |
||
52 | public function categories($lang = 'A') |
||
77 | |||
78 | /** |
||
79 | * Returns a list of all the services categories. |
||
80 | * |
||
81 | * @param string $lang |
||
82 | * @return array |
||
83 | */ |
||
84 | public function main($lang = 'A') |
||
88 | |||
89 | /** |
||
90 | * Returns a list of all the services categories. |
||
91 | * |
||
92 | * @param string $lang |
||
93 | * @return array |
||
94 | */ |
||
95 | public function cat($lang = 'A') |
||
99 | |||
100 | /** |
||
101 | * Returns a list of all the sub services of a service. |
||
102 | * |
||
103 | * @param int $serviceId |
||
104 | * @param string $lang |
||
105 | * @return Services |
||
106 | */ |
||
107 | public function sub(int $serviceId = 1, $lang = 'A') |
||
134 | |||
135 | /** |
||
136 | * Returns a list of all the sub services of a service. |
||
137 | * |
||
138 | * @param int $serviceId |
||
139 | * @param string $lang |
||
140 | * @return array |
||
141 | */ |
||
142 | public function subCategories(int $serviceId = 1, $lang = 'A') |
||
146 | |||
147 | /** |
||
148 | * Returns a list of all the sub services of a service. |
||
149 | * |
||
150 | * @param int $serviceId |
||
151 | * @param string $lang |
||
152 | * @return array |
||
153 | */ |
||
154 | public function subServices(int $serviceId = 1, $lang = 'A') |
||
158 | |||
159 | /** |
||
160 | * Returns a the response. |
||
161 | * |
||
162 | * @return array |
||
163 | */ |
||
164 | public function get() |
||
170 | |||
171 | /** |
||
172 | * Returns a specific service by id. |
||
173 | * |
||
174 | * @param int $serviceId |
||
175 | * @return array |
||
176 | */ |
||
177 | View Code Duplication | public function getId(int $serviceId) |
|
185 | |||
186 | /** |
||
187 | * Returns a specific service by id. |
||
188 | * |
||
189 | * @param int $serviceId |
||
190 | * @return array |
||
191 | */ |
||
192 | public function byId(int $serviceId) |
||
196 | |||
197 | /** |
||
198 | * Returns a specific service by id. |
||
199 | * |
||
200 | * @param int $serviceId |
||
201 | * @return array |
||
202 | */ |
||
203 | public function id(int $serviceId) |
||
207 | |||
208 | /** |
||
209 | * Returns a specific service by name. |
||
210 | * |
||
211 | * @param string $serviceName |
||
212 | * @return array |
||
213 | */ |
||
214 | View Code Duplication | public function getName($serviceName) |
|
222 | |||
223 | /** |
||
224 | * Returns a specific service by name. |
||
225 | * |
||
226 | * @param string $serviceName |
||
227 | * @return array |
||
228 | */ |
||
229 | public function byName($serviceName) |
||
233 | |||
234 | /** |
||
235 | * Returns a specific service by name. |
||
236 | * |
||
237 | * @param string $serviceName |
||
238 | * @return array |
||
239 | */ |
||
240 | public function named($serviceName) |
||
244 | |||
245 | /** |
||
246 | * Returns a specific service by name. |
||
247 | * |
||
248 | * @param string $serviceName |
||
249 | * @return array |
||
250 | */ |
||
251 | public function serviceName($serviceName) |
||
255 | |||
256 | /** |
||
257 | * Check if all() method was called first. |
||
258 | * |
||
259 | * @return void |
||
260 | * @throws \BadMethodCallException |
||
261 | */ |
||
262 | protected function check() |
||
268 | } |
||
269 |
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.