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:
Complex classes like AnnotationProvider often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AnnotationProvider, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | class AnnotationProvider |
||
38 | { |
||
39 | /** |
||
40 | * @var AnnotationCollection |
||
41 | */ |
||
42 | private $annotations; |
||
43 | |||
44 | /** |
||
45 | * @var MethodModel |
||
46 | */ |
||
47 | private $methodModel; |
||
48 | |||
49 | /** |
||
50 | * @var string |
||
51 | */ |
||
52 | private $multipartBoundary; |
||
53 | |||
54 | /** |
||
55 | * Constructor |
||
56 | * |
||
57 | * @param AnnotationCollection $annotations |
||
58 | * @param MethodModel $methodModel |
||
59 | */ |
||
60 | public function __construct(AnnotationCollection $annotations, MethodModel $methodModel) |
||
65 | |||
66 | /** |
||
67 | * Get base url |
||
68 | * |
||
69 | * @return null|string |
||
70 | */ |
||
71 | public function getBaseUrl() |
||
82 | |||
83 | /** |
||
84 | * Get request method |
||
85 | * |
||
86 | * @return string |
||
87 | * @throws LogicException |
||
88 | */ |
||
89 | public function getRequestMethod() |
||
93 | |||
94 | /** |
||
95 | * Get request uri |
||
96 | * |
||
97 | * @return string |
||
98 | * @throws LogicException |
||
99 | */ |
||
100 | public function getRequestUri() |
||
104 | |||
105 | /** |
||
106 | * Get request queries |
||
107 | * |
||
108 | * @return null|array |
||
109 | * @throws LogicException |
||
110 | */ |
||
111 | public function getQueries() |
||
128 | |||
129 | /** |
||
130 | * Get query map |
||
131 | * |
||
132 | * @return null|string |
||
133 | */ |
||
134 | public function getQueryMap() |
||
145 | |||
146 | /** |
||
147 | * Get header variables |
||
148 | * |
||
149 | * @return array|null |
||
150 | */ |
||
151 | View Code Duplication | public function getHeaders() |
|
167 | |||
168 | /** |
||
169 | * Get headers defined by "@Headers" |
||
170 | * |
||
171 | * @return null|array |
||
172 | */ |
||
173 | public function getStaticHeaders() |
||
189 | |||
190 | /** |
||
191 | * If the request is json encoded |
||
192 | * |
||
193 | * @return bool |
||
194 | */ |
||
195 | public function isJsonEncoded() |
||
203 | |||
204 | /** |
||
205 | * If the request is form encoded |
||
206 | * |
||
207 | * @return bool |
||
208 | */ |
||
209 | public function isFormUrlEncoded() |
||
217 | |||
218 | /** |
||
219 | * If the request is multipart encoded |
||
220 | * |
||
221 | * @return bool |
||
222 | */ |
||
223 | public function isMultipart() |
||
231 | |||
232 | /** |
||
233 | * Get the multipart boundary |
||
234 | * |
||
235 | * @return string |
||
236 | */ |
||
237 | public function getMultipartBoundary() |
||
249 | |||
250 | /** |
||
251 | * If there is a request body |
||
252 | * |
||
253 | * @return bool |
||
254 | */ |
||
255 | public function hasBody() |
||
259 | |||
260 | /** |
||
261 | * If there is a body annotation |
||
262 | * |
||
263 | * @return bool |
||
264 | */ |
||
265 | public function hasBodyAnnotation() |
||
269 | |||
270 | /** |
||
271 | * Get body variable |
||
272 | * |
||
273 | * @return null|string |
||
274 | * @throws LogicException |
||
275 | */ |
||
276 | public function getBody() |
||
284 | |||
285 | /** |
||
286 | * Get body parts |
||
287 | * |
||
288 | * @return array|null |
||
289 | */ |
||
290 | View Code Duplication | public function getBodyParts() |
|
306 | |||
307 | /** |
||
308 | * If the body parameter is an object |
||
309 | * |
||
310 | * @return bool |
||
311 | * @throws LogicException |
||
312 | */ |
||
313 | View Code Duplication | public function isBodyObject() |
|
321 | |||
322 | /** |
||
323 | * If the body parameter is an array |
||
324 | * |
||
325 | * @return bool |
||
326 | * @throws LogicException |
||
327 | */ |
||
328 | View Code Duplication | public function isBodyArray() |
|
336 | |||
337 | /** |
||
338 | * If the body parameter is optional |
||
339 | * |
||
340 | * @return bool |
||
341 | * @throws LogicException |
||
342 | */ |
||
343 | View Code Duplication | public function isBodyOptional() |
|
351 | |||
352 | /** |
||
353 | * If the body parameter implements \JsonSerializable |
||
354 | * |
||
355 | * @return bool |
||
356 | * @throws LogicException |
||
357 | */ |
||
358 | public function isBodyJsonSerializable() |
||
371 | |||
372 | /** |
||
373 | * Get JMS Serialization context |
||
374 | * |
||
375 | * @return array|null |
||
376 | */ |
||
377 | View Code Duplication | public function getSerializationContext() |
|
394 | |||
395 | /** |
||
396 | * Get JMS Deserialization context |
||
397 | * |
||
398 | * @return array|null |
||
399 | */ |
||
400 | View Code Duplication | public function getDeserializationContext() |
|
418 | |||
419 | /** |
||
420 | * Get the expected return |
||
421 | * |
||
422 | * @return null|string |
||
423 | */ |
||
424 | public function getReturnType() |
||
435 | |||
436 | /** |
||
437 | * Get the return type for a response return |
||
438 | * |
||
439 | * @return null|string |
||
440 | */ |
||
441 | public function getResponseType() |
||
452 | |||
453 | /** |
||
454 | * Get the callback parameter variable |
||
455 | * |
||
456 | * @return null|string |
||
457 | * @throws RetrofitException |
||
458 | */ |
||
459 | View Code Duplication | public function getCallback() |
|
469 | |||
470 | /** |
||
471 | * Returns if the callback is optional |
||
472 | * |
||
473 | * @return bool |
||
474 | * @throws LogicException |
||
475 | * @throws RetrofitException |
||
476 | */ |
||
477 | View Code Duplication | public function isCallbackOptional() |
|
487 | |||
488 | /** |
||
489 | * Get the request annotation |
||
490 | * |
||
491 | * @return HttpRequest |
||
492 | * @throws LogicException |
||
493 | */ |
||
494 | private function getRequestAnnotation() |
||
504 | |||
505 | /*** |
||
506 | * Get the body annotation |
||
507 | * |
||
508 | * @return Body |
||
509 | */ |
||
510 | private function getBodyAnnotation() |
||
514 | |||
515 | /** |
||
516 | * Get the callback method parameter |
||
517 | * |
||
518 | * @return null|ParameterModel |
||
519 | * @throws RetrofitException |
||
520 | */ |
||
521 | private function getCallbackParameter() |
||
545 | } |
||
546 |