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 = []) |
|
61 | |||
62 | /** |
||
63 | * Sets routes prefix. |
||
64 | * |
||
65 | * @param string $prefix Routes prefix |
||
66 | */ |
||
67 | 25 | public function setRoutePrefix($prefix = null) |
|
71 | |||
72 | /** |
||
73 | * Returns route prefix. |
||
74 | * |
||
75 | * @return string |
||
76 | */ |
||
77 | 25 | public function getRoutePrefix() |
|
81 | |||
82 | /** |
||
83 | * Sets route names prefix. |
||
84 | * |
||
85 | * @param string $prefix Route names prefix |
||
86 | */ |
||
87 | 25 | 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 | 25 | 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 | 25 | public function setPluralize($pluralize) |
|
131 | |||
132 | /** |
||
133 | * Returns pluralize. |
||
134 | * |
||
135 | * @return bool|null |
||
136 | */ |
||
137 | public function getPluralize() |
||
141 | |||
142 | /** |
||
143 | * Set parent routes. |
||
144 | * |
||
145 | * @param array $parents Array of parent resources names |
||
146 | */ |
||
147 | 25 | 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 | 25 | public function read(RestRouteCollection $collection, \ReflectionMethod $method, $resource) |
|
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) |
|
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 | 25 | 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 | 25 | 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 | 25 | 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 | 25 | private function getMethodArguments(\ReflectionMethod $method) |
|
440 | |||
441 | /** |
||
442 | * Generates final resource name. |
||
443 | * |
||
444 | * @param string|bool $resource |
||
445 | * |
||
446 | * @return string |
||
447 | */ |
||
448 | 25 | private function generateResourceName($resource) |
|
456 | |||
457 | /** |
||
458 | * Generates route name from resources list. |
||
459 | * |
||
460 | * @param string[] $resources |
||
461 | * |
||
462 | * @return string |
||
463 | */ |
||
464 | 25 | private function generateRouteName(array $resources) |
|
475 | |||
476 | /** |
||
477 | * Generates URL parts for route from resources list. |
||
478 | * |
||
479 | * @param string[] $resources |
||
480 | * @param \ReflectionParameter[] $arguments |
||
481 | * @param string $httpMethod |
||
482 | * |
||
483 | * @return array |
||
484 | */ |
||
485 | 25 | private function generateUrlParts(array $resources, array $arguments, $httpMethod) |
|
519 | |||
520 | /** |
||
521 | * Returns custom HTTP method for provided list of resources, arguments, method. |
||
522 | * |
||
523 | * @param string $httpMethod current HTTP method |
||
524 | * @param string[] $resources resources list |
||
525 | * @param \ReflectionParameter[] $arguments list of method arguments |
||
526 | * |
||
527 | * @return string |
||
528 | */ |
||
529 | 17 | private function getCustomHttpMethod($httpMethod, array $resources, array $arguments) |
|
545 | |||
546 | /** |
||
547 | * Returns first route annotation for method. |
||
548 | * |
||
549 | * @param \ReflectionMethod $reflectionMethod |
||
550 | * |
||
551 | * @return RouteAnnotation[] |
||
552 | */ |
||
553 | 25 | private function readRouteAnnotation(\ReflectionMethod $reflectionMethod) |
|
565 | |||
566 | /** |
||
567 | * Reads class annotations. |
||
568 | * |
||
569 | * @param \ReflectionClass $reflectionClass |
||
570 | * @param string $annotationName |
||
571 | * |
||
572 | * @return RouteAnnotation|null |
||
573 | */ |
||
574 | 25 | private function readClassAnnotation(\ReflectionClass $reflectionClass, $annotationName) |
|
582 | |||
583 | /** |
||
584 | * Reads method annotations. |
||
585 | * |
||
586 | * @param \ReflectionMethod $reflectionMethod |
||
587 | * @param string $annotationName |
||
588 | * |
||
589 | * @return RouteAnnotation|null |
||
590 | */ |
||
591 | 25 | private function readMethodAnnotation(\ReflectionMethod $reflectionMethod, $annotationName) |
|
599 | |||
600 | /** |
||
601 | * Reads method annotations. |
||
602 | * |
||
603 | * @param \ReflectionMethod $reflectionMethod |
||
604 | * @param string $annotationName |
||
605 | * |
||
606 | * @return RouteAnnotation[] |
||
607 | */ |
||
608 | 25 | private function readMethodAnnotations(\ReflectionMethod $reflectionMethod, $annotationName) |
|
623 | |||
624 | /** |
||
625 | * @param RestRouteCollection $collection |
||
626 | * @param string $routeName |
||
627 | * @param Route $route |
||
628 | * @param bool $isCollection |
||
629 | * @param bool $isInflectable |
||
630 | * @param RouteAnnotation $annotation |
||
631 | */ |
||
632 | 25 | private function addRoute(RestRouteCollection $collection, $routeName, $route, $isCollection, $isInflectable, RouteAnnotation $annotation = null) |
|
655 | } |
||
656 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.