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 | 62 | public function __construct(Reader $annotationReader, ParamReaderInterface $paramReader, InflectorInterface $inflector, $includeFormat, array $formats = [], $hasMethodPrefix = true) |
|
138 | |||
139 | /** |
||
140 | * Sets routes prefix. |
||
141 | * |
||
142 | * @param string $prefix Routes prefix |
||
143 | */ |
||
144 | 43 | public function setRoutePrefix($prefix = null) |
|
148 | |||
149 | /** |
||
150 | * Returns route prefix. |
||
151 | * |
||
152 | * @return string |
||
153 | */ |
||
154 | 43 | public function getRoutePrefix() |
|
158 | |||
159 | /** |
||
160 | * Sets route names prefix. |
||
161 | * |
||
162 | * @param string $prefix Route names prefix |
||
163 | */ |
||
164 | 43 | public function setNamePrefix($prefix = null) |
|
168 | |||
169 | /** |
||
170 | * Returns name prefix. |
||
171 | * |
||
172 | * @return string |
||
173 | */ |
||
174 | public function getNamePrefix() |
||
178 | |||
179 | /** |
||
180 | * Sets route names versions. |
||
181 | * |
||
182 | * @param array|string|null $versions Route names versions |
||
183 | */ |
||
184 | 43 | public function setVersions($versions = null) |
|
188 | |||
189 | /** |
||
190 | * Returns versions. |
||
191 | * |
||
192 | * @return array|null |
||
193 | */ |
||
194 | public function getVersions() |
||
198 | |||
199 | /** |
||
200 | * Sets pluralize. |
||
201 | * |
||
202 | * @param bool|null $pluralize Specify if resource name must be pluralized |
||
203 | */ |
||
204 | 43 | public function setPluralize($pluralize) |
|
208 | |||
209 | /** |
||
210 | * Returns pluralize. |
||
211 | * |
||
212 | * @return bool|null |
||
213 | */ |
||
214 | public function getPluralize() |
||
218 | |||
219 | /** |
||
220 | * Set parent routes. |
||
221 | * |
||
222 | * @param array $parents Array of parent resources names |
||
223 | */ |
||
224 | 43 | public function setParents(array $parents) |
|
228 | |||
229 | /** |
||
230 | * Returns parents. |
||
231 | * |
||
232 | * @return array |
||
233 | */ |
||
234 | public function getParents() |
||
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 | 43 | public function read(RestRouteCollection $collection, \ReflectionMethod $method, $resource) |
|
378 | |||
379 | /** |
||
380 | * @return string|null |
||
381 | */ |
||
382 | 43 | private function getVersionCondition() |
|
390 | |||
391 | /** |
||
392 | * @param string|null $conditionOne |
||
393 | * @param string|null $conditionTwo |
||
394 | * |
||
395 | * @return string|null |
||
396 | */ |
||
397 | 23 | private function combineConditions($conditionOne, $conditionTwo) |
|
409 | |||
410 | /** |
||
411 | * @return array |
||
412 | */ |
||
413 | 43 | private function getVersionRequirement() |
|
421 | |||
422 | /** |
||
423 | * Checks whether provided path contains {version} placeholder. |
||
424 | * |
||
425 | * @param string $path |
||
426 | * |
||
427 | * @return bool |
||
428 | */ |
||
429 | 43 | private function hasVersionPlaceholder($path) |
|
433 | |||
434 | /** |
||
435 | * Include the format in the path and requirements if its enabled. |
||
436 | * |
||
437 | * @param string $path |
||
438 | * @param array $requirements |
||
439 | */ |
||
440 | 43 | private function includeFormatIfNeeded(&$path, &$requirements) |
|
450 | |||
451 | /** |
||
452 | * Checks whether provided method is readable. |
||
453 | * |
||
454 | * @param \ReflectionMethod $method |
||
455 | * |
||
456 | * @return bool |
||
457 | */ |
||
458 | 43 | private function isMethodReadable(\ReflectionMethod $method) |
|
479 | |||
480 | /** |
||
481 | * Returns HTTP method and resources list from method signature. |
||
482 | * |
||
483 | * @param \ReflectionMethod $method |
||
484 | * @param string[] $resource |
||
485 | * |
||
486 | * @return bool|array |
||
487 | */ |
||
488 | 43 | private function getHttpMethodAndResourcesFromMethod(\ReflectionMethod $method, $resource) |
|
524 | |||
525 | /** |
||
526 | * Returns readable arguments from method. |
||
527 | * |
||
528 | * @param \ReflectionMethod $method |
||
529 | * |
||
530 | * @return \ReflectionParameter[] |
||
531 | */ |
||
532 | 43 | private function getMethodArguments(\ReflectionMethod $method) |
|
584 | |||
585 | /** |
||
586 | * Generates final resource name. |
||
587 | * |
||
588 | * @param string|bool $resource |
||
589 | * |
||
590 | * @return string |
||
591 | */ |
||
592 | 43 | private function generateResourceName($resource) |
|
600 | |||
601 | /** |
||
602 | * Generates route name from resources list. |
||
603 | * |
||
604 | * @param string[] $resources |
||
605 | * |
||
606 | * @return string |
||
607 | */ |
||
608 | 43 | private function generateRouteName(array $resources) |
|
619 | |||
620 | /** |
||
621 | * Generates URL parts for route from resources list. |
||
622 | * |
||
623 | * @param string[] $resources |
||
624 | * @param \ReflectionParameter[] $arguments |
||
625 | * @param string $httpMethod |
||
626 | * |
||
627 | * @return array |
||
628 | */ |
||
629 | 43 | private function generateUrlParts(array $resources, array $arguments, $httpMethod) |
|
663 | |||
664 | /** |
||
665 | * Returns custom HTTP method for provided list of resources, arguments, method. |
||
666 | * |
||
667 | * @param string $httpMethod current HTTP method |
||
668 | * @param string[] $resources resources list |
||
669 | * @param \ReflectionParameter[] $arguments list of method arguments |
||
670 | * |
||
671 | * @return string |
||
672 | */ |
||
673 | 32 | private function getCustomHttpMethod($httpMethod, array $resources, array $arguments) |
|
689 | |||
690 | /** |
||
691 | * Returns first route annotation for method. |
||
692 | * |
||
693 | * @param \ReflectionMethod $reflectionMethod |
||
694 | * |
||
695 | * @return RouteAnnotation[] |
||
696 | */ |
||
697 | 43 | private function readRouteAnnotation(\ReflectionMethod $reflectionMethod) |
|
707 | |||
708 | /** |
||
709 | * Reads class annotations. |
||
710 | * |
||
711 | * @param \ReflectionClass $reflectionClass |
||
712 | * @param string $annotationName |
||
713 | * |
||
714 | * @return RouteAnnotation|null |
||
715 | */ |
||
716 | 43 | private function readClassAnnotation(\ReflectionClass $reflectionClass, $annotationName) |
|
724 | |||
725 | /** |
||
726 | * Reads method annotations. |
||
727 | * |
||
728 | * @param \ReflectionMethod $reflectionMethod |
||
729 | * @param string $annotationName |
||
730 | * |
||
731 | * @return RouteAnnotation|null |
||
732 | */ |
||
733 | 43 | private function readMethodAnnotation(\ReflectionMethod $reflectionMethod, $annotationName) |
|
741 | |||
742 | /** |
||
743 | * Reads method annotations. |
||
744 | * |
||
745 | * @param \ReflectionMethod $reflectionMethod |
||
746 | * @param string $annotationName |
||
747 | * |
||
748 | * @return RouteAnnotation[] |
||
749 | */ |
||
750 | 43 | private function readMethodAnnotations(\ReflectionMethod $reflectionMethod, $annotationName) |
|
765 | |||
766 | /** |
||
767 | * @param RestRouteCollection $collection |
||
768 | * @param string $routeName |
||
769 | * @param Route $route |
||
770 | * @param bool $isCollection |
||
771 | * @param bool $isInflectable |
||
772 | * @param RouteAnnotation $annotation |
||
773 | */ |
||
774 | 43 | private function addRoute(RestRouteCollection $collection, $routeName, $route, $isCollection, $isInflectable, RouteAnnotation $annotation = null) |
|
797 | } |
||
798 |