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 | 40 | 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 | 30 | public function setRoutePrefix($prefix = null) |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Returns route prefix. |
||
| 137 | * |
||
| 138 | * @return string |
||
| 139 | */ |
||
| 140 | 30 | public function getRoutePrefix() |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Sets route names prefix. |
||
| 147 | * |
||
| 148 | * @param string $prefix Route names prefix |
||
| 149 | */ |
||
| 150 | 30 | 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 | 30 | public function setVersions($versions = null) |
|
| 174 | |||
| 175 | /** |
||
| 176 | * Returns versions. |
||
| 177 | * |
||
| 178 | * @return array|null |
||
| 179 | */ |
||
| 180 | public function getVersions() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Sets pluralize. |
||
| 187 | * |
||
| 188 | * @param bool|null $pluralize Specify if resource name must be pluralized |
||
| 189 | */ |
||
| 190 | 30 | 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 | 30 | 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 | 30 | public function read(RestRouteCollection $collection, \ReflectionMethod $method, $resource) |
|
| 237 | { |
||
| 238 | // check that every route parent has non-empty singular name |
||
| 239 | 30 | foreach ($this->parents as $parent) { |
|
| 240 | 5 | if (empty($parent) || '/' === substr($parent, -1)) { |
|
| 241 | throw new \InvalidArgumentException( |
||
| 242 | "Every parent controller must have `get{SINGULAR}Action(\$id)` method\n". |
||
| 243 | 'where {SINGULAR} is a singular form of associated object' |
||
| 244 | ); |
||
| 245 | } |
||
| 246 | 30 | } |
|
| 247 | |||
| 248 | // if method is not readable - skip |
||
| 249 | 30 | if (!$this->isMethodReadable($method)) { |
|
| 250 | 14 | return; |
|
| 251 | } |
||
| 252 | |||
| 253 | // if we can't get http-method and resources from method name - skip |
||
| 254 | 30 | $httpMethodAndResources = $this->getHttpMethodAndResourcesFromMethod($method, $resource); |
|
| 255 | 30 | if (!$httpMethodAndResources) { |
|
| 256 | 30 | return; |
|
| 257 | } |
||
| 258 | |||
| 259 | 30 | list($httpMethod, $resources, $isCollection, $isInflectable) = $httpMethodAndResources; |
|
| 260 | 30 | $arguments = $this->getMethodArguments($method); |
|
| 261 | |||
| 262 | // if we have only 1 resource & 1 argument passed, then it's object call, so |
||
| 263 | // we can set collection singular name |
||
| 264 | 30 | if (1 === count($resources) && 1 === count($arguments) - count($this->parents)) { |
|
| 265 | 18 | $collection->setSingularName($resources[0]); |
|
| 266 | 18 | } |
|
| 267 | |||
| 268 | // if we have parents passed - merge them with own resource names |
||
| 269 | 30 | if (count($this->parents)) { |
|
| 270 | 5 | $resources = array_merge($this->parents, $resources); |
|
| 271 | 5 | } |
|
| 272 | |||
| 273 | 30 | if (empty($resources)) { |
|
| 274 | $resources[] = null; |
||
| 275 | } |
||
| 276 | |||
| 277 | 30 | $routeName = $httpMethod.$this->generateRouteName($resources); |
|
| 278 | 30 | $urlParts = $this->generateUrlParts($resources, $arguments, $httpMethod); |
|
| 279 | |||
| 280 | // if passed method is not valid HTTP method then it's either |
||
| 281 | // a hypertext driver, a custom object (PUT) or collection (GET) |
||
| 282 | // method |
||
| 283 | 30 | if (!in_array($httpMethod, $this->availableHTTPMethods)) { |
|
| 284 | 17 | $urlParts[] = $httpMethod; |
|
| 285 | 17 | $httpMethod = $this->getCustomHttpMethod($httpMethod, $resources, $arguments); |
|
| 286 | 17 | } |
|
| 287 | |||
| 288 | // generated parameters |
||
| 289 | 30 | $routeName = strtolower($routeName); |
|
| 290 | 30 | $path = implode('/', $urlParts); |
|
| 291 | 30 | $defaults = ['_controller' => $method->getName()]; |
|
|
|
|||
| 292 | 30 | $requirements = []; |
|
| 293 | 30 | $options = []; |
|
| 294 | 30 | $host = ''; |
|
| 295 | 30 | $condition = null; |
|
| 296 | |||
| 297 | 30 | $annotations = $this->readRouteAnnotation($method); |
|
| 298 | 30 | if (!empty($annotations)) { |
|
| 299 | 8 | foreach ($annotations as $annotation) { |
|
| 300 | 8 | $path = implode('/', $urlParts); |
|
| 301 | 8 | $defaults = ['_controller' => $method->getName()]; |
|
| 302 | 8 | $requirements = []; |
|
| 303 | 8 | $options = []; |
|
| 304 | 8 | $methods = explode('|', $httpMethod); |
|
| 305 | |||
| 306 | 8 | $annoRequirements = $annotation->getRequirements(); |
|
| 307 | 8 | $annoMethods = $annotation->getMethods(); |
|
| 308 | |||
| 309 | 8 | if (!empty($annoMethods)) { |
|
| 310 | 8 | $methods = $annoMethods; |
|
| 311 | 8 | } |
|
| 312 | |||
| 313 | 8 | $path = $annotation->getPath() !== null ? $this->routePrefix.$annotation->getPath() : $path; |
|
| 314 | 8 | $requirements = array_merge($requirements, $annoRequirements); |
|
| 315 | 8 | $options = array_merge($options, $annotation->getOptions()); |
|
| 316 | 8 | $defaults = array_merge($defaults, $annotation->getDefaults()); |
|
| 317 | 8 | $host = $annotation->getHost(); |
|
| 318 | 8 | $schemes = $annotation->getSchemes(); |
|
| 319 | 8 | $condition = $this->getCondition($method, $annotation); |
|
| 320 | |||
| 321 | 8 | $this->includeFormatIfNeeded($path, $requirements); |
|
| 322 | |||
| 323 | // add route to collection |
||
| 324 | 8 | $route = new Route( |
|
| 325 | 8 | $path, $defaults, $requirements, $options, $host, $schemes, $methods, $condition |
|
| 326 | 8 | ); |
|
| 327 | 8 | $this->addRoute($collection, $routeName, $route, $isCollection, $isInflectable, $annotation); |
|
| 328 | 8 | } |
|
| 329 | 8 | } else { |
|
| 330 | 29 | $this->includeFormatIfNeeded($path, $requirements); |
|
| 331 | |||
| 332 | 29 | $methods = explode('|', strtoupper($httpMethod)); |
|
| 333 | |||
| 334 | // add route to collection |
||
| 335 | 29 | $route = new Route( |
|
| 336 | 29 | $path, $defaults, $requirements, $options, $host, [], $methods, $condition |
|
| 337 | 29 | ); |
|
| 338 | 29 | $this->addRoute($collection, $routeName, $route, $isCollection, $isInflectable); |
|
| 339 | } |
||
| 340 | 30 | } |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Determine the Route condition by combining Route annotations with Version annotation. |
||
| 344 | * |
||
| 345 | * @param \ReflectionMethod $method |
||
| 346 | * @param RouteAnnotation $annotation |
||
| 347 | * |
||
| 348 | * @return string |
||
| 349 | */ |
||
| 350 | 8 | private function getCondition(\ReflectionMethod $method, RouteAnnotation $annotation) |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Include the format in the path and requirements if its enabled. |
||
| 373 | * |
||
| 374 | * @param string $path |
||
| 375 | * @param array $requirements |
||
| 376 | */ |
||
| 377 | 30 | private function includeFormatIfNeeded(&$path, &$requirements) |
|
| 387 | |||
| 388 | /** |
||
| 389 | * Checks whether provided method is readable. |
||
| 390 | * |
||
| 391 | * @param \ReflectionMethod $method |
||
| 392 | * |
||
| 393 | * @return bool |
||
| 394 | */ |
||
| 395 | 30 | private function isMethodReadable(\ReflectionMethod $method) |
|
| 416 | |||
| 417 | /** |
||
| 418 | * Returns HTTP method and resources list from method signature. |
||
| 419 | * |
||
| 420 | * @param \ReflectionMethod $method |
||
| 421 | * @param string[] $resource |
||
| 422 | * |
||
| 423 | * @return bool|array |
||
| 424 | */ |
||
| 425 | 30 | private function getHttpMethodAndResourcesFromMethod(\ReflectionMethod $method, $resource) |
|
| 426 | { |
||
| 427 | // if method doesn't match regex - skip |
||
| 428 | 30 | if (!preg_match('/([a-z][_a-z0-9]+)(.*)Action/', $method->getName(), $matches)) { |
|
| 429 | 30 | return false; |
|
| 430 | } |
||
| 431 | |||
| 432 | 30 | $httpMethod = strtolower($matches[1]); |
|
| 433 | 30 | $resources = preg_split( |
|
| 434 | 30 | '/([A-Z][^A-Z]*)/', $matches[2], -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE |
|
| 435 | 30 | ); |
|
| 436 | 30 | $isCollection = false; |
|
| 437 | 30 | $isInflectable = true; |
|
| 438 | |||
| 439 | 30 | if (0 === strpos($httpMethod, self::COLLECTION_ROUTE_PREFIX) |
|
| 440 | 30 | && in_array(substr($httpMethod, 1), $this->availableHTTPMethods) |
|
| 441 | 30 | ) { |
|
| 442 | 11 | $isCollection = true; |
|
| 443 | 11 | $httpMethod = substr($httpMethod, 1); |
|
| 444 | 30 | } elseif ('options' === $httpMethod) { |
|
| 445 | 14 | $isCollection = true; |
|
| 446 | 14 | } |
|
| 447 | |||
| 448 | 30 | if ($isCollection && !empty($resource)) { |
|
| 449 | 11 | $resourcePluralized = $this->generateResourceName(end($resource)); |
|
| 450 | 11 | $isInflectable = ($resourcePluralized != $resource[count($resource) - 1]); |
|
| 451 | 11 | $resource[count($resource) - 1] = $resourcePluralized; |
|
| 452 | 11 | } |
|
| 453 | |||
| 454 | 30 | $resources = array_merge($resource, $resources); |
|
| 455 | |||
| 456 | 30 | return [$httpMethod, $resources, $isCollection, $isInflectable]; |
|
| 457 | } |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Returns readable arguments from method. |
||
| 461 | * |
||
| 462 | * @param \ReflectionMethod $method |
||
| 463 | * |
||
| 464 | * @return \ReflectionParameter[] |
||
| 465 | */ |
||
| 466 | 30 | private function getMethodArguments(\ReflectionMethod $method) |
|
| 500 | |||
| 501 | /** |
||
| 502 | * Generates final resource name. |
||
| 503 | * |
||
| 504 | * @param string|bool $resource |
||
| 505 | * |
||
| 506 | * @return string |
||
| 507 | */ |
||
| 508 | 30 | private function generateResourceName($resource) |
|
| 516 | |||
| 517 | /** |
||
| 518 | * Generates route name from resources list. |
||
| 519 | * |
||
| 520 | * @param string[] $resources |
||
| 521 | * |
||
| 522 | * @return string |
||
| 523 | */ |
||
| 524 | 30 | private function generateRouteName(array $resources) |
|
| 535 | |||
| 536 | /** |
||
| 537 | * Generates URL parts for route from resources list. |
||
| 538 | * |
||
| 539 | * @param string[] $resources |
||
| 540 | * @param \ReflectionParameter[] $arguments |
||
| 541 | * @param string $httpMethod |
||
| 542 | * |
||
| 543 | * @return array |
||
| 544 | */ |
||
| 545 | 30 | private function generateUrlParts(array $resources, array $arguments, $httpMethod) |
|
| 579 | |||
| 580 | /** |
||
| 581 | * Returns custom HTTP method for provided list of resources, arguments, method. |
||
| 582 | * |
||
| 583 | * @param string $httpMethod current HTTP method |
||
| 584 | * @param string[] $resources resources list |
||
| 585 | * @param \ReflectionParameter[] $arguments list of method arguments |
||
| 586 | * |
||
| 587 | * @return string |
||
| 588 | */ |
||
| 589 | 17 | private function getCustomHttpMethod($httpMethod, array $resources, array $arguments) |
|
| 605 | |||
| 606 | /** |
||
| 607 | * Returns first route annotation for method. |
||
| 608 | * |
||
| 609 | * @param \ReflectionMethod $reflectionMethod |
||
| 610 | * |
||
| 611 | * @return RouteAnnotation[] |
||
| 612 | */ |
||
| 613 | 30 | private function readRouteAnnotation(\ReflectionMethod $reflectionMethod) |
|
| 623 | |||
| 624 | /** |
||
| 625 | * Reads class annotations. |
||
| 626 | * |
||
| 627 | * @param \ReflectionClass $reflectionClass |
||
| 628 | * @param string $annotationName |
||
| 629 | * |
||
| 630 | * @return RouteAnnotation|null |
||
| 631 | */ |
||
| 632 | 30 | private function readClassAnnotation(\ReflectionClass $reflectionClass, $annotationName) |
|
| 640 | |||
| 641 | /** |
||
| 642 | * Reads method annotations. |
||
| 643 | * |
||
| 644 | * @param \ReflectionMethod $reflectionMethod |
||
| 645 | * @param string $annotationName |
||
| 646 | * |
||
| 647 | * @return RouteAnnotation|null |
||
| 648 | */ |
||
| 649 | 30 | private function readMethodAnnotation(\ReflectionMethod $reflectionMethod, $annotationName) |
|
| 657 | |||
| 658 | /** |
||
| 659 | * Reads method annotations. |
||
| 660 | * |
||
| 661 | * @param \ReflectionMethod $reflectionMethod |
||
| 662 | * @param string $annotationName |
||
| 663 | * |
||
| 664 | * @return RouteAnnotation[] |
||
| 665 | */ |
||
| 666 | 30 | private function readMethodAnnotations(\ReflectionMethod $reflectionMethod, $annotationName) |
|
| 667 | { |
||
| 668 | 30 | $annotations = []; |
|
| 669 | 30 | $annotationClass = "FOS\\RestBundle\\Controller\\Annotations\\$annotationName"; |
|
| 670 | |||
| 671 | 30 | if ($annotations_new = $this->annotationReader->getMethodAnnotations($reflectionMethod)) { |
|
| 672 | 14 | foreach ($annotations_new as $annotation) { |
|
| 673 | 14 | if ($annotation instanceof $annotationClass) { |
|
| 674 | 8 | $annotations[] = $annotation; |
|
| 675 | 8 | } |
|
| 676 | 14 | } |
|
| 677 | 14 | } |
|
| 678 | |||
| 679 | 30 | return $annotations; |
|
| 680 | } |
||
| 681 | |||
| 682 | /** |
||
| 683 | * @param RestRouteCollection $collection |
||
| 684 | * @param string $routeName |
||
| 685 | * @param Route $route |
||
| 686 | * @param bool $isCollection |
||
| 687 | * @param bool $isInflectable |
||
| 688 | * @param RouteAnnotation $annotation |
||
| 689 | */ |
||
| 690 | 30 | private function addRoute(RestRouteCollection $collection, $routeName, $route, $isCollection, $isInflectable, RouteAnnotation $annotation = null) |
|
| 713 | } |
||
| 714 |