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 | 39 | 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 | 24 | public function setRoutePrefix($prefix = null) |
|
71 | |||
72 | /** |
||
73 | * Returns route prefix. |
||
74 | * |
||
75 | * @return string |
||
76 | */ |
||
77 | 24 | public function getRoutePrefix() |
|
81 | |||
82 | /** |
||
83 | * Sets route names prefix. |
||
84 | * |
||
85 | * @param string $prefix Route names prefix |
||
86 | */ |
||
87 | 24 | 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 | 24 | 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 | 24 | 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 | 24 | 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 | 24 | 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 | 24 | 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 | 24 | 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 | 24 | 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 | 24 | private function getMethodArguments(\ReflectionMethod $method) |
|
436 | |||
437 | /** |
||
438 | * Generates final resource name. |
||
439 | * |
||
440 | * @param string|bool $resource |
||
441 | * |
||
442 | * @return string |
||
443 | */ |
||
444 | 24 | private function generateResourceName($resource) |
|
452 | |||
453 | /** |
||
454 | * Generates route name from resources list. |
||
455 | * |
||
456 | * @param string[] $resources |
||
457 | * |
||
458 | * @return string |
||
459 | */ |
||
460 | 24 | private function generateRouteName(array $resources) |
|
471 | |||
472 | /** |
||
473 | * Generates URL parts for route from resources list. |
||
474 | * |
||
475 | * @param string[] $resources |
||
476 | * @param \ReflectionParameter[] $arguments |
||
477 | * @param string $httpMethod |
||
478 | * |
||
479 | * @return array |
||
480 | */ |
||
481 | 24 | private function generateUrlParts(array $resources, array $arguments, $httpMethod) |
|
515 | |||
516 | /** |
||
517 | * Returns custom HTTP method for provided list of resources, arguments, method. |
||
518 | * |
||
519 | * @param string $httpMethod current HTTP method |
||
520 | * @param string[] $resources resources list |
||
521 | * @param \ReflectionParameter[] $arguments list of method arguments |
||
522 | * |
||
523 | * @return string |
||
524 | */ |
||
525 | 17 | private function getCustomHttpMethod($httpMethod, array $resources, array $arguments) |
|
541 | |||
542 | /** |
||
543 | * Returns first route annotation for method. |
||
544 | * |
||
545 | * @param \ReflectionMethod $reflectionMethod |
||
546 | * |
||
547 | * @return RouteAnnotation[] |
||
548 | */ |
||
549 | 24 | private function readRouteAnnotation(\ReflectionMethod $reflectionMethod) |
|
561 | |||
562 | /** |
||
563 | * Reads class annotations. |
||
564 | * |
||
565 | * @param \ReflectionClass $reflectionClass |
||
566 | * @param string $annotationName |
||
567 | * |
||
568 | * @return RouteAnnotation|null |
||
569 | */ |
||
570 | 24 | private function readClassAnnotation(\ReflectionClass $reflectionClass, $annotationName) |
|
578 | |||
579 | /** |
||
580 | * Reads method annotations. |
||
581 | * |
||
582 | * @param \ReflectionMethod $reflectionMethod |
||
583 | * @param string $annotationName |
||
584 | * |
||
585 | * @return RouteAnnotation|null |
||
586 | */ |
||
587 | 24 | private function readMethodAnnotation(\ReflectionMethod $reflectionMethod, $annotationName) |
|
595 | |||
596 | /** |
||
597 | * Reads method annotations. |
||
598 | * |
||
599 | * @param \ReflectionMethod $reflectionMethod |
||
600 | * @param string $annotationName |
||
601 | * |
||
602 | * @return RouteAnnotation[] |
||
603 | */ |
||
604 | 24 | private function readMethodAnnotations(\ReflectionMethod $reflectionMethod, $annotationName) |
|
619 | |||
620 | /** |
||
621 | * @param RestRouteCollection $collection |
||
622 | * @param string $routeName |
||
623 | * @param Route $route |
||
624 | * @param bool $isCollection |
||
625 | * @param bool $isInflectable |
||
626 | * @param RouteAnnotation $annotation |
||
627 | */ |
||
628 | 24 | private function addRoute(RestRouteCollection $collection, $routeName, $route, $isCollection, $isInflectable, RouteAnnotation $annotation = null) |
|
651 | } |
||
652 |