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 | private $annotationReader; |
||
| 32 | private $paramReader; |
||
| 33 | private $inflector; |
||
| 34 | private $formats; |
||
| 35 | private $includeFormat; |
||
| 36 | private $routePrefix; |
||
| 37 | private $namePrefix; |
||
| 38 | private $versions; |
||
| 39 | private $pluralize; |
||
| 40 | private $parents = []; |
||
| 41 | private $availableHTTPMethods = ['get', 'post', 'put', 'patch', 'delete', 'link', 'unlink', 'head', 'options']; |
||
| 42 | private $availableConventionalActions = ['new', 'edit', 'remove']; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Initializes controller reader. |
||
| 46 | * |
||
| 47 | * @param Reader $annotationReader |
||
| 48 | * @param ParamReaderInterface $paramReader |
||
| 49 | * @param InflectorInterface $inflector |
||
| 50 | * @param bool $includeFormat |
||
| 51 | * @param array $formats |
||
| 52 | */ |
||
| 53 | 40 | public function __construct(Reader $annotationReader, ParamReaderInterface $paramReader, InflectorInterface $inflector, $includeFormat, array $formats = []) |
|
| 54 | { |
||
| 55 | 40 | $this->annotationReader = $annotationReader; |
|
| 56 | 40 | $this->paramReader = $paramReader; |
|
| 57 | 40 | $this->inflector = $inflector; |
|
| 58 | 40 | $this->includeFormat = $includeFormat; |
|
| 59 | 40 | $this->formats = $formats; |
|
| 60 | 40 | } |
|
| 61 | |||
| 62 | /** |
||
| 63 | * Sets routes prefix. |
||
| 64 | * |
||
| 65 | * @param string $prefix Routes prefix |
||
| 66 | */ |
||
| 67 | 30 | public function setRoutePrefix($prefix = null) |
|
| 71 | |||
| 72 | /** |
||
| 73 | * Returns route prefix. |
||
| 74 | * |
||
| 75 | * @return string |
||
| 76 | */ |
||
| 77 | 30 | public function getRoutePrefix() |
|
| 81 | |||
| 82 | /** |
||
| 83 | * Sets route names prefix. |
||
| 84 | * |
||
| 85 | * @param string $prefix Route names prefix |
||
| 86 | */ |
||
| 87 | 30 | public function setNamePrefix($prefix = null) |
|
| 91 | |||
| 92 | /** |
||
| 93 | * Returns name prefix. |
||
| 94 | * |
||
| 95 | * @return string |
||
| 96 | */ |
||
| 97 | public function getNamePrefix() |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Sets route names versions. |
||
| 104 | * |
||
| 105 | * @param array|string|null $versions Route names versions |
||
| 106 | */ |
||
| 107 | 30 | public function setVersions($versions = null) |
|
| 111 | |||
| 112 | /** |
||
| 113 | * Returns versions. |
||
| 114 | * |
||
| 115 | * @return array|null |
||
| 116 | */ |
||
| 117 | public function getVersions() |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Sets pluralize. |
||
| 124 | * |
||
| 125 | * @param bool|null $pluralize Specify if resource name must be pluralized |
||
| 126 | */ |
||
| 127 | 30 | public function setPluralize($pluralize) |
|
| 128 | { |
||
| 129 | 30 | $this->pluralize = $pluralize; |
|
| 130 | 30 | } |
|
| 131 | |||
| 132 | /** |
||
| 133 | * Returns pluralize. |
||
| 134 | * |
||
| 135 | * @return bool|null |
||
| 136 | */ |
||
| 137 | public function getPluralize() |
||
| 138 | { |
||
| 139 | return $this->pluralize; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Set parent routes. |
||
| 144 | * |
||
| 145 | * @param array $parents Array of parent resources names |
||
| 146 | */ |
||
| 147 | 30 | public function setParents(array $parents) |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Returns parents. |
||
| 154 | * |
||
| 155 | * @return array |
||
| 156 | */ |
||
| 157 | public function getParents() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Reads action route. |
||
| 164 | * |
||
| 165 | * @param RestRouteCollection $collection |
||
| 166 | * @param \ReflectionMethod $method |
||
| 167 | * @param string[] $resource |
||
| 168 | * |
||
| 169 | * @throws \InvalidArgumentException |
||
| 170 | * |
||
| 171 | * @return Route |
||
| 172 | */ |
||
| 173 | 30 | public function read(RestRouteCollection $collection, \ReflectionMethod $method, $resource) |
|
| 174 | { |
||
| 175 | // check that every route parent has non-empty singular name |
||
| 176 | 30 | foreach ($this->parents as $parent) { |
|
| 177 | 5 | if (empty($parent) || '/' === substr($parent, -1)) { |
|
| 178 | throw new \InvalidArgumentException( |
||
| 179 | "Every parent controller must have `get{SINGULAR}Action(\$id)` method\n". |
||
| 180 | 'where {SINGULAR} is a singular form of associated object' |
||
| 181 | ); |
||
| 182 | } |
||
| 183 | 30 | } |
|
| 184 | |||
| 185 | // if method is not readable - skip |
||
| 186 | 30 | if (!$this->isMethodReadable($method)) { |
|
| 187 | 14 | return; |
|
| 188 | } |
||
| 189 | |||
| 190 | // if we can't get http-method and resources from method name - skip |
||
| 191 | 30 | $httpMethodAndResources = $this->getHttpMethodAndResourcesFromMethod($method, $resource); |
|
| 192 | 30 | if (!$httpMethodAndResources) { |
|
| 193 | 30 | return; |
|
| 194 | } |
||
| 195 | |||
| 196 | 30 | list($httpMethod, $resources, $isCollection, $isInflectable) = $httpMethodAndResources; |
|
| 197 | 30 | $arguments = $this->getMethodArguments($method); |
|
| 198 | |||
| 199 | // if we have only 1 resource & 1 argument passed, then it's object call, so |
||
| 200 | // we can set collection singular name |
||
| 201 | 30 | if (1 === count($resources) && 1 === count($arguments) - count($this->parents)) { |
|
| 202 | 18 | $collection->setSingularName($resources[0]); |
|
| 203 | 18 | } |
|
| 204 | |||
| 205 | // if we have parents passed - merge them with own resource names |
||
| 206 | 30 | if (count($this->parents)) { |
|
| 207 | 5 | $resources = array_merge($this->parents, $resources); |
|
| 208 | 5 | } |
|
| 209 | |||
| 210 | 30 | if (empty($resources)) { |
|
| 211 | $resources[] = null; |
||
| 212 | } |
||
| 213 | |||
| 214 | 30 | $routeName = $httpMethod.$this->generateRouteName($resources); |
|
| 215 | 30 | $urlParts = $this->generateUrlParts($resources, $arguments, $httpMethod); |
|
| 216 | |||
| 217 | // if passed method is not valid HTTP method then it's either |
||
| 218 | // a hypertext driver, a custom object (PUT) or collection (GET) |
||
| 219 | // method |
||
| 220 | 30 | if (!in_array($httpMethod, $this->availableHTTPMethods)) { |
|
| 221 | 17 | $urlParts[] = $httpMethod; |
|
| 222 | 17 | $httpMethod = $this->getCustomHttpMethod($httpMethod, $resources, $arguments); |
|
| 223 | 17 | } |
|
| 224 | |||
| 225 | // generated parameters |
||
| 226 | 30 | $routeName = strtolower($routeName); |
|
| 227 | 30 | $path = implode('/', $urlParts); |
|
| 228 | 30 | $defaults = ['_controller' => $method->getName()]; |
|
|
|
|||
| 229 | 30 | $requirements = []; |
|
| 230 | 30 | $options = []; |
|
| 231 | 30 | $host = ''; |
|
| 232 | 30 | $condition = null; |
|
| 233 | |||
| 234 | 30 | $annotations = $this->readRouteAnnotation($method); |
|
| 235 | 30 | if (!empty($annotations)) { |
|
| 236 | 8 | foreach ($annotations as $annotation) { |
|
| 237 | 8 | $path = implode('/', $urlParts); |
|
| 238 | 8 | $defaults = ['_controller' => $method->getName()]; |
|
| 239 | 8 | $requirements = []; |
|
| 240 | 8 | $options = []; |
|
| 241 | 8 | $methods = explode('|', $httpMethod); |
|
| 242 | |||
| 243 | 8 | $annoRequirements = $annotation->getRequirements(); |
|
| 244 | 8 | $annoMethods = $annotation->getMethods(); |
|
| 245 | |||
| 246 | 8 | if (!empty($annoMethods)) { |
|
| 247 | 8 | $methods = $annoMethods; |
|
| 248 | 8 | } |
|
| 249 | |||
| 250 | 8 | $path = $annotation->getPath() !== null ? $this->routePrefix.$annotation->getPath() : $path; |
|
| 251 | 8 | $requirements = array_merge($requirements, $annoRequirements); |
|
| 252 | 8 | $options = array_merge($options, $annotation->getOptions()); |
|
| 253 | 8 | $defaults = array_merge($defaults, $annotation->getDefaults()); |
|
| 254 | 8 | $host = $annotation->getHost(); |
|
| 255 | 8 | $schemes = $annotation->getSchemes(); |
|
| 256 | 8 | $condition = $this->getCondition($method, $annotation); |
|
| 257 | |||
| 258 | 8 | $this->includeFormatIfNeeded($path, $requirements); |
|
| 259 | |||
| 260 | // add route to collection |
||
| 261 | 8 | $route = new Route( |
|
| 262 | 8 | $path, $defaults, $requirements, $options, $host, $schemes, $methods, $condition |
|
| 263 | 8 | ); |
|
| 264 | 8 | $this->addRoute($collection, $routeName, $route, $isCollection, $isInflectable, $annotation); |
|
| 265 | 8 | } |
|
| 266 | 8 | } else { |
|
| 267 | 29 | $this->includeFormatIfNeeded($path, $requirements); |
|
| 268 | |||
| 269 | 29 | $methods = explode('|', strtoupper($httpMethod)); |
|
| 270 | |||
| 271 | // add route to collection |
||
| 272 | 29 | $route = new Route( |
|
| 273 | 29 | $path, $defaults, $requirements, $options, $host, [], $methods, $condition |
|
| 274 | 29 | ); |
|
| 275 | 29 | $this->addRoute($collection, $routeName, $route, $isCollection, $isInflectable); |
|
| 276 | } |
||
| 277 | 30 | } |
|
| 278 | |||
| 279 | /** |
||
| 280 | * Determine the Route condition by combining Route annotations with Version annotation. |
||
| 281 | * |
||
| 282 | * @param \ReflectionMethod $method |
||
| 283 | * @param RouteAnnotation $annotation |
||
| 284 | * |
||
| 285 | * @return string |
||
| 286 | */ |
||
| 287 | 8 | private function getCondition(\ReflectionMethod $method, RouteAnnotation $annotation) |
|
| 288 | { |
||
| 289 | 8 | $condition = $annotation->getCondition(); |
|
| 290 | |||
| 291 | 8 | if (!empty($this->versions)) { |
|
| 292 | 1 | $versionCondition = "request.attributes.get('version') == ("; |
|
| 293 | 1 | $first = true; |
|
| 294 | 1 | foreach ($this->versions as $version) { |
|
| 295 | 1 | if (!$first) { |
|
| 296 | 1 | $versionCondition .= ' or '; |
|
| 297 | 1 | } |
|
| 298 | 1 | $versionCondition .= '\''.$version.'\''; |
|
| 299 | 1 | $first = false; |
|
| 300 | 1 | } |
|
| 301 | 1 | $versionCondition .= ')'; |
|
| 302 | 1 | $condition = $condition ? '('.$condition.') and '.$versionCondition : $versionCondition; |
|
| 303 | 1 | } |
|
| 304 | |||
| 305 | 8 | return $condition; |
|
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Include the format in the path and requirements if its enabled. |
||
| 310 | * |
||
| 311 | * @param string $path |
||
| 312 | * @param array $requirements |
||
| 313 | */ |
||
| 314 | 30 | private function includeFormatIfNeeded(&$path, &$requirements) |
|
| 324 | |||
| 325 | /** |
||
| 326 | * Checks whether provided method is readable. |
||
| 327 | * |
||
| 328 | * @param \ReflectionMethod $method |
||
| 329 | * |
||
| 330 | * @return bool |
||
| 331 | */ |
||
| 332 | 30 | private function isMethodReadable(\ReflectionMethod $method) |
|
| 353 | |||
| 354 | /** |
||
| 355 | * Returns HTTP method and resources list from method signature. |
||
| 356 | * |
||
| 357 | * @param \ReflectionMethod $method |
||
| 358 | * @param string[] $resource |
||
| 359 | * |
||
| 360 | * @return bool|array |
||
| 361 | */ |
||
| 362 | 30 | private function getHttpMethodAndResourcesFromMethod(\ReflectionMethod $method, $resource) |
|
| 395 | |||
| 396 | /** |
||
| 397 | * Returns readable arguments from method. |
||
| 398 | * |
||
| 399 | * @param \ReflectionMethod $method |
||
| 400 | * |
||
| 401 | * @return \ReflectionParameter[] |
||
| 402 | */ |
||
| 403 | 30 | private function getMethodArguments(\ReflectionMethod $method) |
|
| 404 | { |
||
| 405 | // ignore all query params |
||
| 406 | 30 | $params = $this->paramReader->getParamsFromMethod($method); |
|
| 407 | |||
| 408 | // ignore several type hinted arguments |
||
| 409 | $ignoreClasses = [ |
||
| 410 | 30 | \Symfony\Component\HttpFoundation\Request::class, |
|
| 411 | 30 | \FOS\RestBundle\Request\ParamFetcherInterface::class, |
|
| 412 | 30 | \Symfony\Component\Validator\ConstraintViolationListInterface::class, |
|
| 413 | 30 | \Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter::class, |
|
| 414 | 30 | ]; |
|
| 415 | |||
| 416 | 30 | $arguments = []; |
|
| 417 | 30 | foreach ($method->getParameters() as $argument) { |
|
| 418 | 27 | if (isset($params[$argument->getName()])) { |
|
| 419 | continue; |
||
| 420 | } |
||
| 421 | |||
| 422 | 27 | $argumentClass = $argument->getClass(); |
|
| 423 | 27 | if ($argumentClass) { |
|
| 424 | 17 | $className = $argumentClass->getName(); |
|
| 425 | 17 | foreach ($ignoreClasses as $class) { |
|
| 426 | 17 | if ($className === $class || is_subclass_of($className, $class)) { |
|
| 427 | 16 | continue 2; |
|
| 428 | } |
||
| 429 | 2 | } |
|
| 430 | 1 | } |
|
| 431 | |||
| 432 | 21 | $arguments[] = $argument; |
|
| 433 | 30 | } |
|
| 434 | |||
| 435 | 30 | return $arguments; |
|
| 436 | } |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Generates final resource name. |
||
| 440 | * |
||
| 441 | * @param string|bool $resource |
||
| 442 | * |
||
| 443 | * @return string |
||
| 444 | */ |
||
| 445 | 30 | private function generateResourceName($resource) |
|
| 453 | |||
| 454 | /** |
||
| 455 | * Generates route name from resources list. |
||
| 456 | * |
||
| 457 | * @param string[] $resources |
||
| 458 | * |
||
| 459 | * @return string |
||
| 460 | */ |
||
| 461 | 30 | private function generateRouteName(array $resources) |
|
| 472 | |||
| 473 | /** |
||
| 474 | * Generates URL parts for route from resources list. |
||
| 475 | * |
||
| 476 | * @param string[] $resources |
||
| 477 | * @param \ReflectionParameter[] $arguments |
||
| 478 | * @param string $httpMethod |
||
| 479 | * |
||
| 480 | * @return array |
||
| 481 | */ |
||
| 482 | 30 | private function generateUrlParts(array $resources, array $arguments, $httpMethod) |
|
| 516 | |||
| 517 | /** |
||
| 518 | * Returns custom HTTP method for provided list of resources, arguments, method. |
||
| 519 | * |
||
| 520 | * @param string $httpMethod current HTTP method |
||
| 521 | * @param string[] $resources resources list |
||
| 522 | * @param \ReflectionParameter[] $arguments list of method arguments |
||
| 523 | * |
||
| 524 | * @return string |
||
| 525 | */ |
||
| 526 | 17 | private function getCustomHttpMethod($httpMethod, array $resources, array $arguments) |
|
| 542 | |||
| 543 | /** |
||
| 544 | * Returns first route annotation for method. |
||
| 545 | * |
||
| 546 | * @param \ReflectionMethod $reflectionMethod |
||
| 547 | * |
||
| 548 | * @return RouteAnnotation[] |
||
| 549 | */ |
||
| 550 | 30 | private function readRouteAnnotation(\ReflectionMethod $reflectionMethod) |
|
| 562 | |||
| 563 | /** |
||
| 564 | * Reads class annotations. |
||
| 565 | * |
||
| 566 | * @param \ReflectionClass $reflectionClass |
||
| 567 | * @param string $annotationName |
||
| 568 | * |
||
| 569 | * @return RouteAnnotation|null |
||
| 570 | */ |
||
| 571 | 30 | private function readClassAnnotation(\ReflectionClass $reflectionClass, $annotationName) |
|
| 579 | |||
| 580 | /** |
||
| 581 | * Reads method annotations. |
||
| 582 | * |
||
| 583 | * @param \ReflectionMethod $reflectionMethod |
||
| 584 | * @param string $annotationName |
||
| 585 | * |
||
| 586 | * @return RouteAnnotation|null |
||
| 587 | */ |
||
| 588 | 30 | private function readMethodAnnotation(\ReflectionMethod $reflectionMethod, $annotationName) |
|
| 596 | |||
| 597 | /** |
||
| 598 | * Reads method annotations. |
||
| 599 | * |
||
| 600 | * @param \ReflectionMethod $reflectionMethod |
||
| 601 | * @param string $annotationName |
||
| 602 | * |
||
| 603 | * @return RouteAnnotation[] |
||
| 604 | */ |
||
| 605 | 30 | private function readMethodAnnotations(\ReflectionMethod $reflectionMethod, $annotationName) |
|
| 620 | |||
| 621 | /** |
||
| 622 | * @param RestRouteCollection $collection |
||
| 623 | * @param string $routeName |
||
| 624 | * @param Route $route |
||
| 625 | * @param bool $isCollection |
||
| 626 | * @param bool $isInflectable |
||
| 627 | * @param RouteAnnotation $annotation |
||
| 628 | */ |
||
| 629 | 30 | private function addRoute(RestRouteCollection $collection, $routeName, $route, $isCollection, $isInflectable, RouteAnnotation $annotation = null) |
|
| 652 | } |
||
| 653 |