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() |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Get query map |
||
| 128 | * |
||
| 129 | * @return null|string |
||
| 130 | */ |
||
| 131 | public function getQueryMap() |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Get header variables |
||
| 145 | * |
||
| 146 | * @return array|null |
||
| 147 | */ |
||
| 148 | View Code Duplication | public function getHeaders() |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Get headers defined by "@Headers" |
||
| 167 | * |
||
| 168 | * @return null|array |
||
| 169 | */ |
||
| 170 | public function getStaticHeaders() |
||
| 186 | |||
| 187 | /** |
||
| 188 | * If the request is json encoded |
||
| 189 | * |
||
| 190 | * @return bool |
||
| 191 | */ |
||
| 192 | public function isJsonEncoded() |
||
| 200 | |||
| 201 | /** |
||
| 202 | * If the request is form encoded |
||
| 203 | * |
||
| 204 | * @return bool |
||
| 205 | */ |
||
| 206 | public function isFormUrlEncoded() |
||
| 214 | |||
| 215 | /** |
||
| 216 | * If the request is multipart encoded |
||
| 217 | * |
||
| 218 | * @return bool |
||
| 219 | */ |
||
| 220 | public function isMultipart() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Get the multipart boundary |
||
| 231 | * |
||
| 232 | * @return string |
||
| 233 | */ |
||
| 234 | public function getMultipartBoundary() |
||
| 246 | |||
| 247 | /** |
||
| 248 | * If there is a request body |
||
| 249 | * |
||
| 250 | * @return bool |
||
| 251 | */ |
||
| 252 | public function hasBody() |
||
| 256 | |||
| 257 | /** |
||
| 258 | * If there is a body annotation |
||
| 259 | * |
||
| 260 | * @return bool |
||
| 261 | */ |
||
| 262 | public function hasBodyAnnotation() |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Get body variable |
||
| 269 | * |
||
| 270 | * @return null|string |
||
| 271 | * @throws LogicException |
||
| 272 | */ |
||
| 273 | public function getBody() |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Get body parts |
||
| 284 | * |
||
| 285 | * @return array|null |
||
| 286 | */ |
||
| 287 | View Code Duplication | public function getBodyParts() |
|
| 303 | |||
| 304 | /** |
||
| 305 | * If the body parameter is an object |
||
| 306 | * |
||
| 307 | * @return bool |
||
| 308 | * @throws LogicException |
||
| 309 | */ |
||
| 310 | View Code Duplication | public function isBodyObject() |
|
| 318 | |||
| 319 | /** |
||
| 320 | * If the body parameter is an array |
||
| 321 | * |
||
| 322 | * @return bool |
||
| 323 | * @throws LogicException |
||
| 324 | */ |
||
| 325 | View Code Duplication | public function isBodyArray() |
|
| 333 | |||
| 334 | /** |
||
| 335 | * If the body parameter is optional |
||
| 336 | * |
||
| 337 | * @return bool |
||
| 338 | * @throws LogicException |
||
| 339 | */ |
||
| 340 | View Code Duplication | public function isBodyOptional() |
|
| 348 | |||
| 349 | /** |
||
| 350 | * If the body parameter implements \JsonSerializable |
||
| 351 | * |
||
| 352 | * @return bool |
||
| 353 | * @throws LogicException |
||
| 354 | */ |
||
| 355 | public function isBodyJsonSerializable() |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Get JMS Serialization context |
||
| 371 | * |
||
| 372 | * @return array|null |
||
| 373 | */ |
||
| 374 | View Code Duplication | public function getSerializationContext() |
|
| 391 | |||
| 392 | /** |
||
| 393 | * Get JMS Deserialization context |
||
| 394 | * |
||
| 395 | * @return array|null |
||
| 396 | */ |
||
| 397 | View Code Duplication | public function getDeserializationContext() |
|
| 415 | |||
| 416 | /** |
||
| 417 | * Get the expected return |
||
| 418 | * |
||
| 419 | * @return null|string |
||
| 420 | */ |
||
| 421 | public function getReturnType() |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Get the return type for a response return |
||
| 435 | * |
||
| 436 | * @return null|string |
||
| 437 | */ |
||
| 438 | public function getResponseType() |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Get the callback parameter variable |
||
| 452 | * |
||
| 453 | * @return null|string |
||
| 454 | * @throws RetrofitException |
||
| 455 | */ |
||
| 456 | View Code Duplication | public function getCallback() |
|
| 466 | |||
| 467 | /** |
||
| 468 | * Returns if the callback is optional |
||
| 469 | * |
||
| 470 | * @return bool |
||
| 471 | * @throws LogicException |
||
| 472 | * @throws RetrofitException |
||
| 473 | */ |
||
| 474 | View Code Duplication | public function isCallbackOptional() |
|
| 484 | |||
| 485 | /** |
||
| 486 | * Get the request annotation |
||
| 487 | * |
||
| 488 | * @return HttpRequest |
||
| 489 | * @throws LogicException |
||
| 490 | */ |
||
| 491 | private function getRequestAnnotation() |
||
| 501 | |||
| 502 | /*** |
||
| 503 | * Get the body annotation |
||
| 504 | * |
||
| 505 | * @return Body |
||
| 506 | */ |
||
| 507 | private function getBodyAnnotation() |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Get the callback method parameter |
||
| 514 | * |
||
| 515 | * @return null|ParameterModel |
||
| 516 | * @throws RetrofitException |
||
| 517 | */ |
||
| 518 | private function getCallbackParameter() |
||
| 542 | } |
||
| 543 |