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 |
||
| 33 | class RestActionReader |
||
| 34 | { |
||
| 35 | const COLLECTION_ROUTE_PREFIX = 'c'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var Reader |
||
| 39 | */ |
||
| 40 | private $annotationReader; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var ParamReaderInterface |
||
| 44 | */ |
||
| 45 | private $paramReader; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var InflectorInterface |
||
| 49 | */ |
||
| 50 | private $inflector; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | private $formats; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var bool |
||
| 59 | */ |
||
| 60 | private $includeFormat; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var string|null |
||
| 64 | */ |
||
| 65 | private $routePrefix; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var string|null |
||
| 69 | */ |
||
| 70 | private $namePrefix; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var array|string|null |
||
| 74 | */ |
||
| 75 | private $versions; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var bool|null |
||
| 79 | */ |
||
| 80 | private $pluralize; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var array |
||
| 84 | */ |
||
| 85 | private $parents = []; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var array |
||
| 89 | */ |
||
| 90 | private $availableHTTPMethods = [ |
||
| 91 | 'get', |
||
| 92 | 'post', |
||
| 93 | 'put', |
||
| 94 | 'patch', |
||
| 95 | 'delete', |
||
| 96 | 'link', |
||
| 97 | 'unlink', |
||
| 98 | 'head', |
||
| 99 | 'options', |
||
| 100 | 'mkcol', |
||
| 101 | 'propfind', |
||
| 102 | 'proppatch', |
||
| 103 | 'move', |
||
| 104 | 'copy', |
||
| 105 | 'lock', |
||
| 106 | 'unlock', |
||
| 107 | ]; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @var array |
||
| 111 | */ |
||
| 112 | private $availableConventionalActions = ['new', 'edit', 'remove']; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @var bool |
||
| 116 | */ |
||
| 117 | private $hasMethodPrefix; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Initializes controller reader. |
||
| 121 | * |
||
| 122 | * @param Reader $annotationReader |
||
| 123 | * @param ParamReaderInterface $paramReader |
||
| 124 | * @param InflectorInterface $inflector |
||
| 125 | * @param bool $includeFormat |
||
| 126 | * @param array $formats |
||
| 127 | * @param bool $hasMethodPrefix |
||
| 128 | */ |
||
| 129 | 56 | public function __construct(Reader $annotationReader, ParamReaderInterface $paramReader, InflectorInterface $inflector, $includeFormat, array $formats = [], $hasMethodPrefix = true) |
|
| 130 | { |
||
| 131 | 56 | $this->annotationReader = $annotationReader; |
|
| 132 | 56 | $this->paramReader = $paramReader; |
|
| 133 | 56 | $this->inflector = $inflector; |
|
| 134 | 56 | $this->includeFormat = $includeFormat; |
|
| 135 | 56 | $this->formats = $formats; |
|
| 136 | 56 | $this->hasMethodPrefix = $hasMethodPrefix; |
|
| 137 | 56 | } |
|
| 138 | |||
| 139 | /** |
||
| 140 | * Sets routes prefix. |
||
| 141 | * |
||
| 142 | * @param string $prefix Routes prefix |
||
| 143 | */ |
||
| 144 | 39 | public function setRoutePrefix($prefix = null) |
|
| 145 | { |
||
| 146 | 39 | $this->routePrefix = $prefix; |
|
| 147 | 39 | } |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Returns route prefix. |
||
| 151 | * |
||
| 152 | * @return string |
||
| 153 | */ |
||
| 154 | 39 | public function getRoutePrefix() |
|
| 155 | { |
||
| 156 | 39 | return $this->routePrefix; |
|
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Sets route names prefix. |
||
| 161 | * |
||
| 162 | * @param string $prefix Route names prefix |
||
| 163 | */ |
||
| 164 | 39 | public function setNamePrefix($prefix = null) |
|
| 165 | { |
||
| 166 | 39 | $this->namePrefix = $prefix; |
|
| 167 | 39 | } |
|
| 168 | |||
| 169 | /** |
||
| 170 | * Returns name prefix. |
||
| 171 | * |
||
| 172 | * @return string |
||
| 173 | */ |
||
| 174 | public function getNamePrefix() |
||
| 175 | { |
||
| 176 | return $this->namePrefix; |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Sets route names versions. |
||
| 181 | * |
||
| 182 | * @param array|string|null $versions Route names versions |
||
| 183 | */ |
||
| 184 | 39 | public function setVersions($versions = null) |
|
| 185 | { |
||
| 186 | 39 | $this->versions = (array) $versions; |
|
| 187 | 39 | } |
|
| 188 | |||
| 189 | /** |
||
| 190 | * Returns versions. |
||
| 191 | * |
||
| 192 | * @return array|null |
||
| 193 | */ |
||
| 194 | public function getVersions() |
||
| 195 | { |
||
| 196 | return $this->versions; |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Sets pluralize. |
||
| 201 | * |
||
| 202 | * @param bool|null $pluralize Specify if resource name must be pluralized |
||
| 203 | */ |
||
| 204 | 39 | public function setPluralize($pluralize) |
|
| 205 | { |
||
| 206 | 39 | $this->pluralize = $pluralize; |
|
| 207 | 39 | } |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Returns pluralize. |
||
| 211 | * |
||
| 212 | * @return bool|null |
||
| 213 | */ |
||
| 214 | public function getPluralize() |
||
| 215 | { |
||
| 216 | return $this->pluralize; |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Set parent routes. |
||
| 221 | * |
||
| 222 | * @param array $parents Array of parent resources names |
||
| 223 | */ |
||
| 224 | 39 | public function setParents(array $parents) |
|
| 225 | { |
||
| 226 | 39 | $this->parents = $parents; |
|
| 227 | 39 | } |
|
| 228 | |||
| 229 | /** |
||
| 230 | * Returns parents. |
||
| 231 | * |
||
| 232 | * @return array |
||
| 233 | */ |
||
| 234 | public function getParents() |
||
| 235 | { |
||
| 236 | return $this->parents; |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Reads action route. |
||
| 241 | * |
||
| 242 | * @param RestRouteCollection $collection |
||
| 243 | * @param \ReflectionMethod $method |
||
| 244 | * @param string[] $resource |
||
| 245 | * |
||
| 246 | * @throws \InvalidArgumentException |
||
| 247 | * |
||
| 248 | * @return Route |
||
| 249 | */ |
||
| 250 | 39 | public function read(RestRouteCollection $collection, \ReflectionMethod $method, $resource) |
|
| 251 | { |
||
| 252 | // check that every route parent has non-empty singular name |
||
| 253 | 39 | foreach ($this->parents as $parent) { |
|
| 254 | 5 | if (empty($parent) || '/' === substr($parent, -1)) { |
|
| 255 | throw new \InvalidArgumentException('Every parent controller must have `get{SINGULAR}Action(\$id)` method where {SINGULAR} is a singular form of associated object'); |
||
| 256 | } |
||
| 257 | } |
||
| 258 | |||
| 259 | // if method is not readable - skip |
||
| 260 | 39 | if (!$this->isMethodReadable($method)) { |
|
| 261 | 15 | return; |
|
| 262 | } |
||
| 263 | |||
| 264 | // if we can't get http-method and resources from method name - skip |
||
| 265 | 39 | $httpMethodAndResources = $this->getHttpMethodAndResourcesFromMethod($method, $resource); |
|
| 266 | 39 | if (!$httpMethodAndResources) { |
|
| 267 | 36 | return; |
|
| 268 | } |
||
| 269 | |||
| 270 | 39 | list($httpMethod, $resources, $isCollection, $isInflectable) = $httpMethodAndResources; |
|
| 271 | 39 | $arguments = $this->getMethodArguments($method); |
|
| 272 | |||
| 273 | // if we have only 1 resource & 1 argument passed, then it's object call, so |
||
| 274 | // we can set collection singular name |
||
| 275 | 39 | if (1 === count($resources) && 1 === count($arguments) - count($this->parents)) { |
|
| 276 | 32 | $collection->setSingularName($resources[0]); |
|
| 277 | } |
||
| 278 | |||
| 279 | // if we have parents passed - merge them with own resource names |
||
| 280 | 39 | if (count($this->parents)) { |
|
| 281 | 5 | $resources = array_merge($this->parents, $resources); |
|
| 282 | } |
||
| 283 | |||
| 284 | 39 | if (empty($resources)) { |
|
| 285 | 10 | $resources[] = null; |
|
| 286 | } |
||
| 287 | |||
| 288 | 39 | $routeName = $httpMethod.$this->generateRouteName($resources); |
|
| 289 | 39 | $urlParts = $this->generateUrlParts($resources, $arguments, $httpMethod); |
|
| 290 | |||
| 291 | // if passed method is not valid HTTP method then it's either |
||
| 292 | // a hypertext driver, a custom object (PUT) or collection (GET) |
||
| 293 | // method |
||
| 294 | 39 | if (!in_array($httpMethod, $this->availableHTTPMethods)) { |
|
| 295 | 28 | $urlParts[] = $httpMethod; |
|
| 296 | 28 | $httpMethod = $this->getCustomHttpMethod($httpMethod, $resources, $arguments); |
|
| 297 | } |
||
| 298 | |||
| 299 | // generated parameters |
||
| 300 | 39 | $routeName = strtolower($routeName); |
|
| 301 | 39 | $path = implode('/', $urlParts); |
|
| 302 | 39 | $defaults = ['_controller' => $method->getName()]; |
|
|
|
|||
| 303 | 39 | $requirements = []; |
|
| 304 | 39 | $options = []; |
|
| 305 | 39 | $host = ''; |
|
| 306 | 39 | $versionCondition = $this->getVersionCondition(); |
|
| 307 | 39 | $versionRequirement = $this->getVersionRequirement(); |
|
| 308 | |||
| 309 | 39 | $annotations = $this->readRouteAnnotation($method); |
|
| 310 | 39 | if (!empty($annotations)) { |
|
| 311 | 19 | foreach ($annotations as $annotation) { |
|
| 312 | 19 | $path = implode('/', $urlParts); |
|
| 313 | 19 | $defaults = ['_controller' => $method->getName()]; |
|
| 314 | 19 | $requirements = []; |
|
| 315 | 19 | $options = []; |
|
| 316 | 19 | $methods = explode('|', $httpMethod); |
|
| 317 | |||
| 318 | 19 | $annoRequirements = $annotation->getRequirements(); |
|
| 319 | 19 | $annoMethods = $annotation->getMethods(); |
|
| 320 | |||
| 321 | 19 | if (!empty($annoMethods)) { |
|
| 322 | 19 | $methods = $annoMethods; |
|
| 323 | } |
||
| 324 | |||
| 325 | 19 | $path = null !== $annotation->getPath() ? $this->routePrefix.$annotation->getPath() : $path; |
|
| 326 | 19 | $requirements = array_merge($requirements, $annoRequirements); |
|
| 327 | 19 | $options = array_merge($options, $annotation->getOptions()); |
|
| 328 | 19 | $defaults = array_merge($defaults, $annotation->getDefaults()); |
|
| 329 | 19 | $host = $annotation->getHost(); |
|
| 330 | 19 | $schemes = $annotation->getSchemes(); |
|
| 331 | |||
| 332 | 19 | if ($this->hasVersionPlaceholder($path)) { |
|
| 333 | 11 | $combinedCondition = $annotation->getCondition(); |
|
| 334 | 11 | $requirements = array_merge($versionRequirement, $requirements); |
|
| 335 | } else { |
||
| 336 | 19 | $combinedCondition = $this->combineConditions($versionCondition, $annotation->getCondition()); |
|
| 337 | } |
||
| 338 | |||
| 339 | 19 | $this->includeFormatIfNeeded($path, $requirements); |
|
| 340 | |||
| 341 | // add route to collection |
||
| 342 | 19 | $route = new Route( |
|
| 343 | 19 | $path, $defaults, $requirements, $options, $host, $schemes, $methods, $combinedCondition |
|
| 344 | ); |
||
| 345 | 19 | $this->addRoute($collection, $routeName, $route, $isCollection, $isInflectable, $annotation); |
|
| 346 | } |
||
| 347 | } else { |
||
| 348 | 39 | if ($this->hasVersionPlaceholder($path)) { |
|
| 349 | $versionCondition = null; |
||
| 350 | $requirements = $versionRequirement; |
||
| 351 | } |
||
| 352 | |||
| 353 | 39 | $this->includeFormatIfNeeded($path, $requirements); |
|
| 354 | |||
| 355 | 39 | $methods = explode('|', strtoupper($httpMethod)); |
|
| 356 | |||
| 357 | // add route to collection |
||
| 358 | 39 | $route = new Route( |
|
| 359 | 39 | $path, $defaults, $requirements, $options, $host, [], $methods, $versionCondition |
|
| 360 | ); |
||
| 361 | 39 | $this->addRoute($collection, $routeName, $route, $isCollection, $isInflectable); |
|
| 362 | } |
||
| 363 | 39 | } |
|
| 364 | |||
| 365 | /** |
||
| 366 | * @return string|null |
||
| 367 | */ |
||
| 368 | 39 | private function getVersionCondition() |
|
| 369 | { |
||
| 370 | 39 | if (empty($this->versions)) { |
|
| 371 | 38 | return; |
|
| 372 | } |
||
| 373 | |||
| 374 | 11 | return sprintf("request.attributes.get('version') in ['%s']", implode("', '", $this->versions)); |
|
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @param string|null $conditionOne |
||
| 379 | * @param string|null $conditionTwo |
||
| 380 | * |
||
| 381 | * @return string|null |
||
| 382 | */ |
||
| 383 | 19 | private function combineConditions($conditionOne, $conditionTwo) |
|
| 395 | |||
| 396 | /** |
||
| 397 | * @return array |
||
| 398 | */ |
||
| 399 | 39 | private function getVersionRequirement() |
|
| 407 | |||
| 408 | /** |
||
| 409 | * Checks whether provided path contains {version} placeholder. |
||
| 410 | * |
||
| 411 | * @param string $path |
||
| 412 | * |
||
| 413 | * @return bool |
||
| 414 | */ |
||
| 415 | 39 | private function hasVersionPlaceholder($path) |
|
| 416 | { |
||
| 417 | 39 | return false !== strpos($path, '{version}'); |
|
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Include the format in the path and requirements if its enabled. |
||
| 422 | * |
||
| 423 | * @param string $path |
||
| 424 | * @param array $requirements |
||
| 425 | */ |
||
| 426 | 39 | private function includeFormatIfNeeded(&$path, &$requirements) |
|
| 436 | |||
| 437 | /** |
||
| 438 | * Checks whether provided method is readable. |
||
| 439 | * |
||
| 440 | * @param \ReflectionMethod $method |
||
| 441 | * |
||
| 442 | * @return bool |
||
| 443 | */ |
||
| 444 | 39 | private function isMethodReadable(\ReflectionMethod $method) |
|
| 465 | |||
| 466 | /** |
||
| 467 | * Returns HTTP method and resources list from method signature. |
||
| 468 | * |
||
| 469 | * @param \ReflectionMethod $method |
||
| 470 | * @param string[] $resource |
||
| 471 | * |
||
| 472 | * @return bool|array |
||
| 473 | */ |
||
| 474 | 39 | private function getHttpMethodAndResourcesFromMethod(\ReflectionMethod $method, $resource) |
|
| 507 | |||
| 508 | /** |
||
| 509 | * Returns readable arguments from method. |
||
| 510 | * |
||
| 511 | * @param \ReflectionMethod $method |
||
| 512 | * |
||
| 513 | * @return \ReflectionParameter[] |
||
| 514 | */ |
||
| 515 | 39 | private function getMethodArguments(\ReflectionMethod $method) |
|
| 567 | |||
| 568 | /** |
||
| 569 | * Generates final resource name. |
||
| 570 | * |
||
| 571 | * @param string|bool $resource |
||
| 572 | * |
||
| 573 | * @return string |
||
| 574 | */ |
||
| 575 | 39 | private function generateResourceName($resource) |
|
| 583 | |||
| 584 | /** |
||
| 585 | * Generates route name from resources list. |
||
| 586 | * |
||
| 587 | * @param string[] $resources |
||
| 588 | * |
||
| 589 | * @return string |
||
| 590 | */ |
||
| 591 | 39 | private function generateRouteName(array $resources) |
|
| 602 | |||
| 603 | /** |
||
| 604 | * Generates URL parts for route from resources list. |
||
| 605 | * |
||
| 606 | * @param string[] $resources |
||
| 607 | * @param \ReflectionParameter[] $arguments |
||
| 608 | * @param string $httpMethod |
||
| 609 | * |
||
| 610 | * @return array |
||
| 611 | */ |
||
| 612 | 39 | private function generateUrlParts(array $resources, array $arguments, $httpMethod) |
|
| 646 | |||
| 647 | /** |
||
| 648 | * Returns custom HTTP method for provided list of resources, arguments, method. |
||
| 649 | * |
||
| 650 | * @param string $httpMethod current HTTP method |
||
| 651 | * @param string[] $resources resources list |
||
| 652 | * @param \ReflectionParameter[] $arguments list of method arguments |
||
| 653 | * |
||
| 654 | * @return string |
||
| 655 | */ |
||
| 656 | 28 | private function getCustomHttpMethod($httpMethod, array $resources, array $arguments) |
|
| 672 | |||
| 673 | /** |
||
| 674 | * Returns first route annotation for method. |
||
| 675 | * |
||
| 676 | * @param \ReflectionMethod $reflectionMethod |
||
| 677 | * |
||
| 678 | * @return RouteAnnotation[] |
||
| 679 | */ |
||
| 680 | 39 | private function readRouteAnnotation(\ReflectionMethod $reflectionMethod) |
|
| 690 | |||
| 691 | /** |
||
| 692 | * Reads class annotations. |
||
| 693 | * |
||
| 694 | * @param \ReflectionClass $reflectionClass |
||
| 695 | * @param string $annotationName |
||
| 696 | * |
||
| 697 | * @return RouteAnnotation|null |
||
| 698 | */ |
||
| 699 | 39 | private function readClassAnnotation(\ReflectionClass $reflectionClass, $annotationName) |
|
| 707 | |||
| 708 | /** |
||
| 709 | * Reads method annotations. |
||
| 710 | * |
||
| 711 | * @param \ReflectionMethod $reflectionMethod |
||
| 712 | * @param string $annotationName |
||
| 713 | * |
||
| 714 | * @return RouteAnnotation|null |
||
| 715 | */ |
||
| 716 | 39 | private function readMethodAnnotation(\ReflectionMethod $reflectionMethod, $annotationName) |
|
| 724 | |||
| 725 | /** |
||
| 726 | * Reads method annotations. |
||
| 727 | * |
||
| 728 | * @param \ReflectionMethod $reflectionMethod |
||
| 729 | * @param string $annotationName |
||
| 730 | * |
||
| 731 | * @return RouteAnnotation[] |
||
| 732 | */ |
||
| 733 | 39 | private function readMethodAnnotations(\ReflectionMethod $reflectionMethod, $annotationName) |
|
| 748 | |||
| 749 | /** |
||
| 750 | * @param RestRouteCollection $collection |
||
| 751 | * @param string $routeName |
||
| 752 | * @param Route $route |
||
| 753 | * @param bool $isCollection |
||
| 754 | * @param bool $isInflectable |
||
| 755 | * @param RouteAnnotation $annotation |
||
| 756 | */ |
||
| 757 | 39 | private function addRoute(RestRouteCollection $collection, $routeName, $route, $isCollection, $isInflectable, RouteAnnotation $annotation = null) |
|
| 780 | } |
||
| 781 |