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 |
||
| 31 | class RestActionReader |
||
| 32 | { |
||
| 33 | const COLLECTION_ROUTE_PREFIX = 'c'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var Reader |
||
| 37 | */ |
||
| 38 | private $annotationReader; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var ParamReaderInterface |
||
| 42 | */ |
||
| 43 | private $paramReader; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var InflectorInterface |
||
| 47 | */ |
||
| 48 | private $inflector; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | private $formats; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var bool |
||
| 57 | */ |
||
| 58 | private $includeFormat; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var string|null |
||
| 62 | */ |
||
| 63 | private $routePrefix; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var string|null |
||
| 67 | */ |
||
| 68 | private $namePrefix; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var array|string|null |
||
| 72 | */ |
||
| 73 | private $versions; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var bool|null |
||
| 77 | */ |
||
| 78 | private $pluralize; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var array |
||
| 82 | */ |
||
| 83 | private $parents = []; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var array |
||
| 87 | */ |
||
| 88 | private $availableHTTPMethods = [ |
||
| 89 | 'get', |
||
| 90 | 'post', |
||
| 91 | 'put', |
||
| 92 | 'patch', |
||
| 93 | 'delete', |
||
| 94 | 'link', |
||
| 95 | 'unlink', |
||
| 96 | 'head', |
||
| 97 | 'options', |
||
| 98 | 'mkcol', |
||
| 99 | 'propfind', |
||
| 100 | 'proppatch', |
||
| 101 | 'move', |
||
| 102 | 'copy', |
||
| 103 | 'lock', |
||
| 104 | 'unlock', |
||
| 105 | ]; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var array |
||
| 109 | */ |
||
| 110 | private $availableConventionalActions = ['new', 'edit', 'remove']; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var bool |
||
| 114 | */ |
||
| 115 | private $hasMethodPrefix; |
||
| 116 | |||
| 117 | 56 | /** |
|
| 118 | * Initializes controller reader. |
||
| 119 | 56 | * |
|
| 120 | 56 | * @param Reader $annotationReader |
|
| 121 | 56 | * @param ParamReaderInterface $paramReader |
|
| 122 | 56 | * @param InflectorInterface $inflector |
|
| 123 | 56 | * @param bool $includeFormat |
|
| 124 | 56 | * @param array $formats |
|
| 125 | * @param bool $hasMethodPrefix |
||
| 126 | */ |
||
| 127 | public function __construct(Reader $annotationReader, ParamReaderInterface $paramReader, InflectorInterface $inflector, $includeFormat, array $formats = [], $hasMethodPrefix = true) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Sets routes prefix. |
||
| 139 | * |
||
| 140 | * @param string $prefix Routes prefix |
||
| 141 | 37 | */ |
|
| 142 | public function setRoutePrefix($prefix = null) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Returns route prefix. |
||
| 149 | * |
||
| 150 | * @return string |
||
| 151 | 37 | */ |
|
| 152 | public function getRoutePrefix() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Sets route names prefix. |
||
| 159 | * |
||
| 160 | * @param string $prefix Route names prefix |
||
| 161 | */ |
||
| 162 | public function setNamePrefix($prefix = null) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Returns name prefix. |
||
| 169 | * |
||
| 170 | * @return string |
||
| 171 | 37 | */ |
|
| 172 | public function getNamePrefix() |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Sets route names versions. |
||
| 179 | * |
||
| 180 | * @param array|string|null $versions Route names versions |
||
| 181 | */ |
||
| 182 | public function setVersions($versions = null) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Returns versions. |
||
| 189 | * |
||
| 190 | * @return array|null |
||
| 191 | 37 | */ |
|
| 192 | public function getVersions() |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Sets pluralize. |
||
| 199 | * |
||
| 200 | * @param bool|null $pluralize Specify if resource name must be pluralized |
||
| 201 | */ |
||
| 202 | public function setPluralize($pluralize) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Returns pluralize. |
||
| 209 | * |
||
| 210 | * @return bool|null |
||
| 211 | 37 | */ |
|
| 212 | public function getPluralize() |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Set parent routes. |
||
| 219 | * |
||
| 220 | * @param array $parents Array of parent resources names |
||
| 221 | */ |
||
| 222 | public function setParents(array $parents) |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Returns parents. |
||
| 229 | * |
||
| 230 | * @return array |
||
| 231 | */ |
||
| 232 | public function getParents() |
||
| 236 | |||
| 237 | 37 | /** |
|
| 238 | * Reads action route. |
||
| 239 | * |
||
| 240 | 37 | * @param RestRouteCollection $collection |
|
| 241 | 5 | * @param \ReflectionMethod $method |
|
| 242 | * @param string[] $resource |
||
| 243 | * |
||
| 244 | * @throws \InvalidArgumentException |
||
| 245 | * |
||
| 246 | * @return Route |
||
| 247 | 37 | */ |
|
| 248 | public function read(RestRouteCollection $collection, \ReflectionMethod $method, $resource) |
||
| 362 | |||
| 363 | 18 | /** |
|
| 364 | 7 | * @return string|null |
|
| 365 | */ |
||
| 366 | private function getVersionCondition() |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @param string|null $conditionOne |
||
| 377 | * @param string|null $conditionTwo |
||
| 378 | * |
||
| 379 | * @return string|null |
||
| 380 | 37 | */ |
|
| 381 | private function combineConditions($conditionOne, $conditionTwo) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @return array |
||
| 396 | */ |
||
| 397 | private function getVersionRequirement() |
||
| 405 | 37 | ||
| 406 | 37 | /** |
|
| 407 | * Checks whether provided path contains {version} placeholder. |
||
| 408 | 37 | * |
|
| 409 | * @param string $path |
||
| 410 | 37 | * |
|
| 411 | * @return bool |
||
| 412 | */ |
||
| 413 | 37 | private function hasVersionPlaceholder($path) |
|
| 417 | 37 | ||
| 418 | /** |
||
| 419 | * Include the format in the path and requirements if its enabled. |
||
| 420 | * |
||
| 421 | * @param string $path |
||
| 422 | * @param array $requirements |
||
| 423 | */ |
||
| 424 | private function includeFormatIfNeeded(&$path, &$requirements) |
||
| 434 | |||
| 435 | 37 | /** |
|
| 436 | 37 | * Checks whether provided method is readable. |
|
| 437 | 37 | * |
|
| 438 | 37 | * @param \ReflectionMethod $method |
|
| 439 | 37 | * |
|
| 440 | 37 | * @return bool |
|
| 441 | */ |
||
| 442 | 37 | private function isMethodReadable(\ReflectionMethod $method) |
|
| 463 | |||
| 464 | /** |
||
| 465 | * Returns HTTP method and resources list from method signature. |
||
| 466 | * |
||
| 467 | * @param \ReflectionMethod $method |
||
| 468 | * @param string[] $resource |
||
| 469 | 37 | * |
|
| 470 | * @return bool|array |
||
| 471 | */ |
||
| 472 | 37 | private function getHttpMethodAndResourcesFromMethod(\ReflectionMethod $method, $resource) |
|
| 505 | |||
| 506 | /** |
||
| 507 | * Returns readable arguments from method. |
||
| 508 | * |
||
| 509 | * @param \ReflectionMethod $method |
||
| 510 | * |
||
| 511 | * @return \ReflectionParameter[] |
||
| 512 | 37 | */ |
|
| 513 | private function getMethodArguments(\ReflectionMethod $method) |
||
| 563 | 24 | ||
| 564 | 24 | /** |
|
| 565 | 24 | * Generates final resource name. |
|
| 566 | 24 | * |
|
| 567 | 10 | * @param string|bool $resource |
|
| 568 | * |
||
| 569 | 37 | * @return string |
|
| 570 | 36 | */ |
|
| 571 | private function generateResourceName($resource) |
||
| 579 | 37 | ||
| 580 | /** |
||
| 581 | 37 | * Generates route name from resources list. |
|
| 582 | * |
||
| 583 | * @param string[] $resources |
||
| 584 | * |
||
| 585 | * @return string |
||
| 586 | */ |
||
| 587 | private function generateRouteName(array $resources) |
||
| 598 | 12 | ||
| 599 | /** |
||
| 600 | * Generates URL parts for route from resources list. |
||
| 601 | 25 | * |
|
| 602 | * @param string[] $resources |
||
| 603 | 15 | * @param \ReflectionParameter[] $arguments |
|
| 604 | * @param string $httpMethod |
||
| 605 | * |
||
| 606 | * @return array |
||
| 607 | 24 | */ |
|
| 608 | private function generateUrlParts(array $resources, array $arguments, $httpMethod) |
||
| 642 | |||
| 643 | 37 | /** |
|
| 644 | * Returns custom HTTP method for provided list of resources, arguments, method. |
||
| 645 | * |
||
| 646 | * @param string $httpMethod current HTTP method |
||
| 647 | * @param string[] $resources resources list |
||
| 648 | * @param \ReflectionParameter[] $arguments list of method arguments |
||
| 649 | * |
||
| 650 | * @return string |
||
| 651 | */ |
||
| 652 | private function getCustomHttpMethod($httpMethod, array $resources, array $arguments) |
||
| 668 | |||
| 669 | /** |
||
| 670 | 37 | * Returns first route annotation for method. |
|
| 671 | * |
||
| 672 | 37 | * @param \ReflectionMethod $reflectionMethod |
|
| 673 | 37 | * |
|
| 674 | * @return RouteAnnotation[] |
||
| 675 | 37 | */ |
|
| 676 | 18 | private function readRouteAnnotation(\ReflectionMethod $reflectionMethod) |
|
| 686 | |||
| 687 | /** |
||
| 688 | * Reads class annotations. |
||
| 689 | * |
||
| 690 | * @param \ReflectionClass $reflectionClass |
||
| 691 | * @param string $annotationName |
||
| 692 | * |
||
| 693 | * @return RouteAnnotation|null |
||
| 694 | 37 | */ |
|
| 695 | private function readClassAnnotation(\ReflectionClass $reflectionClass, $annotationName) |
||
| 703 | |||
| 704 | 4 | /** |
|
| 705 | * Reads method annotations. |
||
| 706 | 37 | * |
|
| 707 | * @param \ReflectionMethod $reflectionMethod |
||
| 708 | 37 | * @param string $annotationName |
|
| 709 | 4 | * |
|
| 710 | 4 | * @return RouteAnnotation|null |
|
| 711 | 4 | */ |
|
| 712 | 4 | private function readMethodAnnotation(\ReflectionMethod $reflectionMethod, $annotationName) |
|
| 720 | |||
| 721 | /** |
||
| 722 | * Reads method annotations. |
||
| 723 | * |
||
| 724 | * @param \ReflectionMethod $reflectionMethod |
||
| 725 | * @param string $annotationName |
||
| 726 | * |
||
| 727 | * @return RouteAnnotation[] |
||
| 728 | */ |
||
| 729 | private function readMethodAnnotations(\ReflectionMethod $reflectionMethod, $annotationName) |
||
| 744 | |||
| 745 | /** |
||
| 746 | * @param RestRouteCollection $collection |
||
| 747 | * @param string $routeName |
||
| 748 | * @param Route $route |
||
| 749 | * @param bool $isCollection |
||
| 750 | * @param bool $isInflectable |
||
| 751 | * @param RouteAnnotation $annotation |
||
| 752 | */ |
||
| 753 | private function addRoute(RestRouteCollection $collection, $routeName, $route, $isCollection, $isInflectable, RouteAnnotation $annotation = null) |
||
| 776 | } |
||
| 777 |