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 | 46 | 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 | 36 | public function setRoutePrefix($prefix = null) |
|
134 | |||
135 | /** |
||
136 | * Returns route prefix. |
||
137 | * |
||
138 | * @return string |
||
139 | */ |
||
140 | 36 | public function getRoutePrefix() |
|
144 | |||
145 | /** |
||
146 | * Sets route names prefix. |
||
147 | * |
||
148 | * @param string $prefix Route names prefix |
||
149 | */ |
||
150 | 36 | 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 | 36 | 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 | 36 | 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 | 36 | 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 | 36 | public function read(RestRouteCollection $collection, \ReflectionMethod $method, $resource) |
|
237 | { |
||
238 | // check that every route parent has non-empty singular name |
||
239 | 36 | 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 | 36 | } |
|
247 | |||
248 | // if method is not readable - skip |
||
249 | 36 | if (!$this->isMethodReadable($method)) { |
|
250 | 14 | return; |
|
251 | } |
||
252 | |||
253 | // if we can't get http-method and resources from method name - skip |
||
254 | 36 | $httpMethodAndResources = $this->getHttpMethodAndResourcesFromMethod($method, $resource); |
|
255 | 36 | if (!$httpMethodAndResources) { |
|
256 | 35 | return; |
|
257 | } |
||
258 | |||
259 | 36 | list($httpMethod, $resources, $isCollection, $isInflectable) = $httpMethodAndResources; |
|
260 | 36 | $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 | 36 | if (1 === count($resources) && 1 === count($arguments) - count($this->parents)) { |
|
265 | 20 | $collection->setSingularName($resources[0]); |
|
266 | 20 | } |
|
267 | |||
268 | // if we have parents passed - merge them with own resource names |
||
269 | 36 | if (count($this->parents)) { |
|
270 | 5 | $resources = array_merge($this->parents, $resources); |
|
271 | 5 | } |
|
272 | |||
273 | 36 | if (empty($resources)) { |
|
274 | 10 | $resources[] = null; |
|
275 | 10 | } |
|
276 | |||
277 | 36 | $routeName = $httpMethod.$this->generateRouteName($resources); |
|
278 | 36 | $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 | 36 | if (!in_array($httpMethod, $this->availableHTTPMethods)) { |
|
284 | 27 | $urlParts[] = $httpMethod; |
|
285 | 27 | $httpMethod = $this->getCustomHttpMethod($httpMethod, $resources, $arguments); |
|
286 | 27 | } |
|
287 | |||
288 | // generated parameters |
||
289 | 36 | $routeName = strtolower($routeName); |
|
290 | 36 | $path = implode('/', $urlParts); |
|
291 | 36 | $defaults = ['_controller' => $method->getName()]; |
|
|
|||
292 | 36 | $requirements = []; |
|
293 | 36 | $options = []; |
|
294 | 36 | $host = ''; |
|
295 | 36 | $versionCondition = $this->getVersionCondition(); |
|
296 | |||
297 | 36 | $annotations = $this->readRouteAnnotation($method); |
|
298 | 36 | if (!empty($annotations)) { |
|
299 | 18 | foreach ($annotations as $annotation) { |
|
300 | 18 | $path = implode('/', $urlParts); |
|
301 | 18 | $defaults = ['_controller' => $method->getName()]; |
|
302 | 18 | $requirements = []; |
|
303 | 18 | $options = []; |
|
304 | 18 | $methods = explode('|', $httpMethod); |
|
305 | |||
306 | 18 | $annoRequirements = $annotation->getRequirements(); |
|
307 | 18 | $annoMethods = $annotation->getMethods(); |
|
308 | |||
309 | 18 | if (!empty($annoMethods)) { |
|
310 | 18 | $methods = $annoMethods; |
|
311 | 18 | } |
|
312 | |||
313 | 18 | $path = $annotation->getPath() !== null ? $this->routePrefix.$annotation->getPath() : $path; |
|
314 | 18 | $requirements = array_merge($requirements, $annoRequirements); |
|
315 | 18 | $options = array_merge($options, $annotation->getOptions()); |
|
316 | 18 | $defaults = array_merge($defaults, $annotation->getDefaults()); |
|
317 | 18 | $host = $annotation->getHost(); |
|
318 | 18 | $schemes = $annotation->getSchemes(); |
|
319 | 18 | $combinedCondition = $this->combineConditions($versionCondition, $annotation->getCondition()); |
|
320 | |||
321 | 18 | $this->includeFormatIfNeeded($path, $requirements); |
|
322 | |||
323 | // add route to collection |
||
324 | 18 | $route = new Route( |
|
325 | 18 | $path, $defaults, $requirements, $options, $host, $schemes, $methods, $combinedCondition |
|
326 | 18 | ); |
|
327 | 18 | $this->addRoute($collection, $routeName, $route, $isCollection, $isInflectable, $annotation); |
|
328 | 18 | } |
|
329 | 18 | } else { |
|
330 | 36 | $this->includeFormatIfNeeded($path, $requirements); |
|
331 | |||
332 | 36 | $methods = explode('|', strtoupper($httpMethod)); |
|
333 | |||
334 | // add route to collection |
||
335 | 36 | $route = new Route( |
|
336 | 36 | $path, $defaults, $requirements, $options, $host, [], $methods, $versionCondition |
|
337 | 36 | ); |
|
338 | 36 | $this->addRoute($collection, $routeName, $route, $isCollection, $isInflectable); |
|
339 | } |
||
340 | 36 | } |
|
341 | |||
342 | /** |
||
343 | * @return string|null |
||
344 | */ |
||
345 | 36 | private function getVersionCondition() |
|
346 | { |
||
347 | 36 | if (empty($this->versions)) { |
|
348 | 35 | return; |
|
349 | } |
||
350 | |||
351 | 11 | return sprintf("request.attributes.get('version') in ['%s']", implode("', '", $this->versions)); |
|
352 | } |
||
353 | |||
354 | /** |
||
355 | * @param string|null $conditionOne |
||
356 | * @param string|null $conditionTwo |
||
357 | * |
||
358 | * @return string|null |
||
359 | */ |
||
360 | 18 | private function combineConditions($conditionOne, $conditionTwo) |
|
361 | { |
||
362 | 18 | if (null === $conditionOne) { |
|
363 | 7 | return $conditionTwo; |
|
364 | } |
||
365 | |||
366 | 11 | if (null === $conditionTwo) { |
|
367 | 11 | return $conditionOne; |
|
368 | } |
||
369 | |||
370 | 1 | return sprintf('(%s) and (%s)', $conditionOne, $conditionTwo); |
|
371 | } |
||
372 | |||
373 | /** |
||
374 | * Include the format in the path and requirements if its enabled. |
||
375 | * |
||
376 | * @param string $path |
||
377 | * @param array $requirements |
||
378 | */ |
||
379 | 36 | private function includeFormatIfNeeded(&$path, &$requirements) |
|
389 | |||
390 | /** |
||
391 | * Checks whether provided method is readable. |
||
392 | * |
||
393 | * @param \ReflectionMethod $method |
||
394 | * |
||
395 | * @return bool |
||
396 | */ |
||
397 | 36 | private function isMethodReadable(\ReflectionMethod $method) |
|
418 | |||
419 | /** |
||
420 | * Returns HTTP method and resources list from method signature. |
||
421 | * |
||
422 | * @param \ReflectionMethod $method |
||
423 | * @param string[] $resource |
||
424 | * |
||
425 | * @return bool|array |
||
426 | */ |
||
427 | 36 | private function getHttpMethodAndResourcesFromMethod(\ReflectionMethod $method, $resource) |
|
460 | |||
461 | /** |
||
462 | * Returns readable arguments from method. |
||
463 | * |
||
464 | * @param \ReflectionMethod $method |
||
465 | * |
||
466 | * @return \ReflectionParameter[] |
||
467 | */ |
||
468 | 36 | private function getMethodArguments(\ReflectionMethod $method) |
|
469 | { |
||
470 | // ignore all query params |
||
471 | 36 | $params = $this->paramReader->getParamsFromMethod($method); |
|
472 | |||
473 | // ignore several type hinted arguments |
||
474 | $ignoreClasses = [ |
||
475 | 36 | \Symfony\Component\HttpFoundation\Request::class, |
|
476 | 36 | \FOS\RestBundle\Request\ParamFetcherInterface::class, |
|
477 | 36 | \Symfony\Component\Validator\ConstraintViolationListInterface::class, |
|
478 | 36 | \Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter::class, |
|
479 | 36 | ]; |
|
480 | |||
481 | 36 | $arguments = []; |
|
482 | 36 | foreach ($method->getParameters() as $argument) { |
|
483 | 33 | if (isset($params[$argument->getName()])) { |
|
484 | continue; |
||
485 | } |
||
486 | |||
487 | 33 | $argumentClass = $argument->getClass(); |
|
488 | 33 | if ($argumentClass) { |
|
489 | 21 | $className = $argumentClass->getName(); |
|
490 | 21 | foreach ($ignoreClasses as $class) { |
|
491 | 21 | if ($className === $class || is_subclass_of($className, $class)) { |
|
492 | 20 | continue 2; |
|
493 | } |
||
494 | 2 | } |
|
495 | 1 | } |
|
496 | |||
497 | 33 | $arguments[] = $argument; |
|
498 | 36 | } |
|
499 | |||
500 | 36 | return $arguments; |
|
501 | } |
||
502 | |||
503 | /** |
||
504 | * Generates final resource name. |
||
505 | * |
||
506 | * @param string|bool $resource |
||
507 | * |
||
508 | * @return string |
||
509 | */ |
||
510 | 36 | private function generateResourceName($resource) |
|
518 | |||
519 | /** |
||
520 | * Generates route name from resources list. |
||
521 | * |
||
522 | * @param string[] $resources |
||
523 | * |
||
524 | * @return string |
||
525 | */ |
||
526 | 36 | private function generateRouteName(array $resources) |
|
537 | |||
538 | /** |
||
539 | * Generates URL parts for route from resources list. |
||
540 | * |
||
541 | * @param string[] $resources |
||
542 | * @param \ReflectionParameter[] $arguments |
||
543 | * @param string $httpMethod |
||
544 | * |
||
545 | * @return array |
||
546 | */ |
||
547 | 36 | private function generateUrlParts(array $resources, array $arguments, $httpMethod) |
|
548 | { |
||
549 | 36 | $urlParts = []; |
|
550 | 36 | foreach ($resources as $i => $resource) { |
|
551 | // if we already added all parent routes paths to URL & we have |
||
552 | // prefix - add it |
||
553 | 36 | if (!empty($this->routePrefix) && $i === count($this->parents)) { |
|
554 | 3 | $urlParts[] = $this->routePrefix; |
|
555 | 3 | } |
|
556 | |||
557 | // if we have argument for current resource, then it's object. |
||
558 | // otherwise - it's collection |
||
559 | 36 | if (isset($arguments[$i])) { |
|
560 | 33 | if (null !== $resource) { |
|
561 | 23 | $urlParts[] = |
|
562 | 23 | strtolower($this->generateResourceName($resource)) |
|
563 | 23 | .'/{'.$arguments[$i]->getName().'}'; |
|
564 | 23 | } else { |
|
565 | 10 | $urlParts[] = '{'.$arguments[$i]->getName().'}'; |
|
566 | } |
||
567 | 36 | } elseif (null !== $resource) { |
|
568 | 35 | if ((0 === count($arguments) && !in_array($httpMethod, $this->availableHTTPMethods)) |
|
569 | || 'new' === $httpMethod |
||
570 | 34 | || 'post' === $httpMethod |
|
571 | 35 | ) { |
|
572 | 28 | $urlParts[] = $this->generateResourceName(strtolower($resource)); |
|
573 | 28 | } else { |
|
574 | 34 | $urlParts[] = strtolower($resource); |
|
575 | } |
||
576 | 35 | } |
|
577 | 36 | } |
|
578 | |||
579 | 36 | return $urlParts; |
|
580 | } |
||
581 | |||
582 | /** |
||
583 | * Returns custom HTTP method for provided list of resources, arguments, method. |
||
584 | * |
||
585 | * @param string $httpMethod current HTTP method |
||
586 | * @param string[] $resources resources list |
||
587 | * @param \ReflectionParameter[] $arguments list of method arguments |
||
588 | * |
||
589 | * @return string |
||
590 | */ |
||
591 | 27 | private function getCustomHttpMethod($httpMethod, array $resources, array $arguments) |
|
592 | { |
||
593 | 27 | if (in_array($httpMethod, $this->availableConventionalActions)) { |
|
594 | // allow hypertext as the engine of application state |
||
595 | // through conventional GET actions |
||
596 | 12 | return 'get'; |
|
597 | } |
||
598 | |||
599 | 25 | if (count($arguments) < count($resources)) { |
|
600 | // resource collection |
||
601 | 15 | return 'get'; |
|
602 | } |
||
603 | |||
604 | // custom object |
||
605 | 24 | return 'patch'; |
|
606 | } |
||
607 | |||
608 | /** |
||
609 | * Returns first route annotation for method. |
||
610 | * |
||
611 | * @param \ReflectionMethod $reflectionMethod |
||
612 | * |
||
613 | * @return RouteAnnotation[] |
||
614 | */ |
||
615 | 36 | private function readRouteAnnotation(\ReflectionMethod $reflectionMethod) |
|
616 | { |
||
617 | 36 | $annotations = []; |
|
618 | |||
619 | 36 | if ($newAnnotations = $this->readMethodAnnotations($reflectionMethod, 'Route')) { |
|
620 | 18 | $annotations = array_merge($annotations, $newAnnotations); |
|
621 | 18 | } |
|
622 | |||
623 | 36 | return $annotations; |
|
624 | } |
||
625 | |||
626 | /** |
||
627 | * Reads class annotations. |
||
628 | * |
||
629 | * @param \ReflectionClass $reflectionClass |
||
630 | * @param string $annotationName |
||
631 | * |
||
632 | * @return RouteAnnotation|null |
||
633 | */ |
||
634 | 36 | private function readClassAnnotation(\ReflectionClass $reflectionClass, $annotationName) |
|
642 | |||
643 | /** |
||
644 | * Reads method annotations. |
||
645 | * |
||
646 | * @param \ReflectionMethod $reflectionMethod |
||
647 | * @param string $annotationName |
||
648 | * |
||
649 | * @return RouteAnnotation|null |
||
650 | */ |
||
651 | 36 | private function readMethodAnnotation(\ReflectionMethod $reflectionMethod, $annotationName) |
|
659 | |||
660 | /** |
||
661 | * Reads method annotations. |
||
662 | * |
||
663 | * @param \ReflectionMethod $reflectionMethod |
||
664 | * @param string $annotationName |
||
665 | * |
||
666 | * @return RouteAnnotation[] |
||
667 | */ |
||
668 | 36 | private function readMethodAnnotations(\ReflectionMethod $reflectionMethod, $annotationName) |
|
683 | |||
684 | /** |
||
685 | * @param RestRouteCollection $collection |
||
686 | * @param string $routeName |
||
687 | * @param Route $route |
||
688 | * @param bool $isCollection |
||
689 | * @param bool $isInflectable |
||
690 | * @param RouteAnnotation $annotation |
||
691 | */ |
||
692 | 36 | private function addRoute(RestRouteCollection $collection, $routeName, $route, $isCollection, $isInflectable, RouteAnnotation $annotation = null) |
|
715 | } |
||
716 |