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 RestActionReader 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 RestActionReader, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class RestActionReader |
||
| 28 | { |
||
| 29 | const COLLECTION_ROUTE_PREFIX = 'c'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var Reader |
||
| 33 | */ |
||
| 34 | private $annotationReader; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var ParamReaderInterface |
||
| 38 | */ |
||
| 39 | private $paramReader; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var InflectorInterface |
||
| 43 | */ |
||
| 44 | private $inflector; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | private $formats; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var bool |
||
| 53 | */ |
||
| 54 | private $includeFormat; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var string|null |
||
| 58 | */ |
||
| 59 | private $routePrefix; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var string|null |
||
| 63 | */ |
||
| 64 | private $namePrefix; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var array|string|null |
||
| 68 | */ |
||
| 69 | private $versions; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var bool|null |
||
| 73 | */ |
||
| 74 | private $pluralize; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var array |
||
| 78 | */ |
||
| 79 | private $parents = []; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var array |
||
| 83 | */ |
||
| 84 | private $availableHTTPMethods = [ |
||
| 85 | 'get', |
||
| 86 | 'post', |
||
| 87 | 'put', |
||
| 88 | 'patch', |
||
| 89 | 'delete', |
||
| 90 | 'link', |
||
| 91 | 'unlink', |
||
| 92 | 'head', |
||
| 93 | 'options', |
||
| 94 | 'mkcol', |
||
| 95 | 'propfind', |
||
| 96 | 'proppatch', |
||
| 97 | 'move', |
||
| 98 | 'copy', |
||
| 99 | 'lock', |
||
| 100 | 'unlock', |
||
| 101 | ]; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var array |
||
| 105 | */ |
||
| 106 | private $availableConventionalActions = ['new', 'edit', 'remove']; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Initializes controller reader. |
||
| 110 | * |
||
| 111 | * @param Reader $annotationReader |
||
| 112 | * @param ParamReaderInterface $paramReader |
||
| 113 | * @param InflectorInterface $inflector |
||
| 114 | * @param bool $includeFormat |
||
| 115 | * @param array $formats |
||
| 116 | */ |
||
| 117 | 56 | public function __construct(Reader $annotationReader, ParamReaderInterface $paramReader, InflectorInterface $inflector, $includeFormat, array $formats = []) |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Sets routes prefix. |
||
| 128 | * |
||
| 129 | * @param string $prefix Routes prefix |
||
| 130 | */ |
||
| 131 | 37 | public function setRoutePrefix($prefix = null) |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Returns route prefix. |
||
| 138 | * |
||
| 139 | * @return string |
||
| 140 | */ |
||
| 141 | 37 | public function getRoutePrefix() |
|
| 145 | |||
| 146 | /** |
||
| 147 | * Sets route names prefix. |
||
| 148 | * |
||
| 149 | * @param string $prefix Route names prefix |
||
| 150 | */ |
||
| 151 | 37 | public function setNamePrefix($prefix = null) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Returns name prefix. |
||
| 158 | * |
||
| 159 | * @return string |
||
| 160 | */ |
||
| 161 | public function getNamePrefix() |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Sets route names versions. |
||
| 168 | * |
||
| 169 | * @param array|string|null $versions Route names versions |
||
| 170 | */ |
||
| 171 | 37 | public function setVersions($versions = null) |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Returns versions. |
||
| 178 | * |
||
| 179 | * @return array|null |
||
| 180 | */ |
||
| 181 | public function getVersions() |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Sets pluralize. |
||
| 188 | * |
||
| 189 | * @param bool|null $pluralize Specify if resource name must be pluralized |
||
| 190 | */ |
||
| 191 | 37 | public function setPluralize($pluralize) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Returns pluralize. |
||
| 198 | * |
||
| 199 | * @return bool|null |
||
| 200 | */ |
||
| 201 | public function getPluralize() |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Set parent routes. |
||
| 208 | * |
||
| 209 | * @param array $parents Array of parent resources names |
||
| 210 | */ |
||
| 211 | 37 | public function setParents(array $parents) |
|
| 215 | |||
| 216 | /** |
||
| 217 | * Returns parents. |
||
| 218 | * |
||
| 219 | * @return array |
||
| 220 | */ |
||
| 221 | public function getParents() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Reads action route. |
||
| 228 | * |
||
| 229 | * @param RestRouteCollection $collection |
||
| 230 | * @param \ReflectionMethod $method |
||
| 231 | * @param string[] $resource |
||
| 232 | * |
||
| 233 | * @throws \InvalidArgumentException |
||
| 234 | * |
||
| 235 | * @return Route |
||
| 236 | */ |
||
| 237 | 37 | public function read(RestRouteCollection $collection, \ReflectionMethod $method, $resource) |
|
| 342 | |||
| 343 | /** |
||
| 344 | * @return string|null |
||
| 345 | */ |
||
| 346 | 37 | private function getVersionCondition() |
|
| 354 | |||
| 355 | /** |
||
| 356 | * @param string|null $conditionOne |
||
| 357 | * @param string|null $conditionTwo |
||
| 358 | * |
||
| 359 | * @return string|null |
||
| 360 | */ |
||
| 361 | 18 | private function combineConditions($conditionOne, $conditionTwo) |
|
| 373 | |||
| 374 | /** |
||
| 375 | * Include the format in the path and requirements if its enabled. |
||
| 376 | * |
||
| 377 | * @param string $path |
||
| 378 | * @param array $requirements |
||
| 379 | */ |
||
| 380 | 37 | private function includeFormatIfNeeded(&$path, &$requirements) |
|
| 390 | |||
| 391 | /** |
||
| 392 | * Checks whether provided method is readable. |
||
| 393 | * |
||
| 394 | * @param \ReflectionMethod $method |
||
| 395 | * |
||
| 396 | * @return bool |
||
| 397 | */ |
||
| 398 | 37 | private function isMethodReadable(\ReflectionMethod $method) |
|
| 419 | |||
| 420 | /** |
||
| 421 | * Returns HTTP method and resources list from method signature. |
||
| 422 | * |
||
| 423 | * @param \ReflectionMethod $method |
||
| 424 | * @param string[] $resource |
||
| 425 | * |
||
| 426 | * @return bool|array |
||
| 427 | */ |
||
| 428 | 37 | private function getHttpMethodAndResourcesFromMethod(\ReflectionMethod $method, $resource) |
|
| 461 | |||
| 462 | /** |
||
| 463 | * Returns readable arguments from method. |
||
| 464 | * |
||
| 465 | * @param \ReflectionMethod $method |
||
| 466 | * |
||
| 467 | * @return \ReflectionParameter[] |
||
| 468 | */ |
||
| 469 | 37 | private function getMethodArguments(\ReflectionMethod $method) |
|
| 504 | |||
| 505 | /** |
||
| 506 | * Generates final resource name. |
||
| 507 | * |
||
| 508 | * @param string|bool $resource |
||
| 509 | * |
||
| 510 | * @return string |
||
| 511 | */ |
||
| 512 | 37 | private function generateResourceName($resource) |
|
| 520 | |||
| 521 | /** |
||
| 522 | * Generates route name from resources list. |
||
| 523 | * |
||
| 524 | * @param string[] $resources |
||
| 525 | * |
||
| 526 | * @return string |
||
| 527 | */ |
||
| 528 | 37 | private function generateRouteName(array $resources) |
|
| 539 | |||
| 540 | /** |
||
| 541 | * Generates URL parts for route from resources list. |
||
| 542 | * |
||
| 543 | * @param string[] $resources |
||
| 544 | * @param \ReflectionParameter[] $arguments |
||
| 545 | * @param string $httpMethod |
||
| 546 | * |
||
| 547 | * @return array |
||
| 548 | */ |
||
| 549 | 37 | private function generateUrlParts(array $resources, array $arguments, $httpMethod) |
|
| 583 | |||
| 584 | /** |
||
| 585 | * Returns custom HTTP method for provided list of resources, arguments, method. |
||
| 586 | * |
||
| 587 | * @param string $httpMethod current HTTP method |
||
| 588 | * @param string[] $resources resources list |
||
| 589 | * @param \ReflectionParameter[] $arguments list of method arguments |
||
| 590 | * |
||
| 591 | * @return string |
||
| 592 | */ |
||
| 593 | 27 | private function getCustomHttpMethod($httpMethod, array $resources, array $arguments) |
|
| 609 | |||
| 610 | /** |
||
| 611 | * Returns first route annotation for method. |
||
| 612 | * |
||
| 613 | * @param \ReflectionMethod $reflectionMethod |
||
| 614 | * |
||
| 615 | * @return RouteAnnotation[] |
||
| 616 | */ |
||
| 617 | 37 | private function readRouteAnnotation(\ReflectionMethod $reflectionMethod) |
|
| 627 | |||
| 628 | /** |
||
| 629 | * Reads class annotations. |
||
| 630 | * |
||
| 631 | * @param \ReflectionClass $reflectionClass |
||
| 632 | * @param string $annotationName |
||
| 633 | * |
||
| 634 | * @return RouteAnnotation|null |
||
| 635 | */ |
||
| 636 | 37 | private function readClassAnnotation(\ReflectionClass $reflectionClass, $annotationName) |
|
| 644 | |||
| 645 | /** |
||
| 646 | * Reads method annotations. |
||
| 647 | * |
||
| 648 | * @param \ReflectionMethod $reflectionMethod |
||
| 649 | * @param string $annotationName |
||
| 650 | * |
||
| 651 | * @return RouteAnnotation|null |
||
| 652 | */ |
||
| 653 | 37 | private function readMethodAnnotation(\ReflectionMethod $reflectionMethod, $annotationName) |
|
| 661 | |||
| 662 | /** |
||
| 663 | * Reads method annotations. |
||
| 664 | * |
||
| 665 | * @param \ReflectionMethod $reflectionMethod |
||
| 666 | * @param string $annotationName |
||
| 667 | * |
||
| 668 | * @return RouteAnnotation[] |
||
| 669 | */ |
||
| 670 | 37 | private function readMethodAnnotations(\ReflectionMethod $reflectionMethod, $annotationName) |
|
| 685 | |||
| 686 | /** |
||
| 687 | * @param RestRouteCollection $collection |
||
| 688 | * @param string $routeName |
||
| 689 | * @param Route $route |
||
| 690 | * @param bool $isCollection |
||
| 691 | * @param bool $isInflectable |
||
| 692 | * @param RouteAnnotation $annotation |
||
| 693 | */ |
||
| 694 | 37 | private function addRoute(RestRouteCollection $collection, $routeName, $route, $isCollection, $isInflectable, RouteAnnotation $annotation = null) |
|
| 717 | } |
||
| 718 |