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