Complex classes like ApiDocs 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 ApiDocs, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class ApiDocs |
||
| 12 | { |
||
| 13 | |||
| 14 | private $router; |
||
| 15 | private $config; |
||
| 16 | private $request; |
||
| 17 | |||
| 18 | public function __construct(Router $router, Config $config, Request $request) |
||
| 24 | |||
| 25 | public function show($routePrefix = null) |
||
| 34 | |||
| 35 | public function blueprint($routePrefix = null) |
||
| 45 | |||
| 46 | private function getEndpoints($routePrefix) |
||
| 47 | { |
||
| 48 | $endpoints = []; |
||
| 49 | |||
| 50 | foreach ($this->router->getRoutes() as $route) { |
||
| 51 | if (!$this->isPrefixedRoute($route, $routePrefix) || $this->isClosureRoute($route) || $this->isExcluded($route)) { |
||
| 52 | continue; |
||
| 53 | } |
||
| 54 | |||
| 55 | $actionController = explode("@", $this->getRouteParam($route, 'action.controller')); |
||
| 56 | $class = $actionController[0]; |
||
| 57 | $method = $actionController[1]; |
||
| 58 | |||
| 59 | if (!class_exists($class) || !method_exists($class, $method)) { |
||
| 60 | continue; |
||
| 61 | } |
||
| 62 | |||
| 63 | |||
| 64 | list($title, $description, $params) = $this->getRouteDocBlock($class, $method); |
||
| 65 | $key = $this->generateEndpointKey($class); |
||
| 66 | |||
| 67 | $endpoints[$key][] = [ |
||
| 68 | 'hash' => $this->generateHashForUrl($key, $route, $method), |
||
| 69 | 'uri' => $this->getRouteParam($route, 'uri'), |
||
| 70 | 'name' => $method, |
||
| 71 | 'methods' => $this->getRouteParam($route, 'methods'), |
||
| 72 | 'docs' => [ |
||
| 73 | 'title' => $title, |
||
| 74 | 'description' => trim($description), |
||
| 75 | 'params' => $params, |
||
| 76 | 'uri_params' => $this->getUriParams($route), |
||
| 77 | ], |
||
| 78 | ]; |
||
| 79 | } |
||
| 80 | |||
| 81 | return $endpoints; |
||
| 82 | } // end getEndpoints |
||
| 83 | |||
| 84 | private function isExcluded($route) |
||
| 85 | { |
||
| 86 | $uri = $this->getRouteParam($route, 'uri'); |
||
| 87 | $actionController = $this->getRouteParam($route, 'action.controller'); |
||
| 88 | |||
| 89 | return $this->isExcludedClass($actionController) || $this->isExcludedRoute($uri); |
||
| 90 | } // end isExcluded |
||
| 91 | |||
| 92 | private function isExcludedRoute($uri) |
||
| 93 | { |
||
| 94 | foreach ($this->config->get('yaro.apidocs.exclude.routes', []) as $pattern) { |
||
| 95 | if (str_is($pattern, $uri)) { |
||
| 96 | return true; |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | return false; |
||
| 101 | } // end isExcludedRoute |
||
| 102 | |||
| 103 | private function isExcludedClass($actionController) |
||
| 104 | { |
||
| 105 | foreach ($this->config->get('yaro.apidocs.exclude.classes', []) as $pattern) { |
||
| 106 | if (str_is($pattern, $actionController)) { |
||
| 107 | return true; |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | return false; |
||
| 112 | } // end isExcludedClass |
||
| 113 | |||
| 114 | private function isPrefixedRoute($route, $routePrefix) |
||
| 120 | |||
| 121 | private function getRoutePrefix($routePrefix) |
||
| 127 | |||
| 128 | private function getRoutePrefixes() |
||
| 137 | |||
| 138 | private function isClosureRoute($route) |
||
| 144 | |||
| 145 | private function getRouteDocBlock($class, $method) |
||
| 146 | { |
||
| 147 | $reflector = new ReflectionClass($class); |
||
| 148 | |||
| 213 | |||
| 214 | private function fillParamsWithRequestRules($params, $rules) |
||
| 231 | |||
| 232 | private function getParamTemplateByType($paramType) |
||
| 251 | |||
| 252 | private function getParamChunksFromLine($line) |
||
| 262 | |||
| 263 | private function filterDocBlock($docs) |
||
| 273 | |||
| 274 | private function generateEndpointKey($class) |
||
| 294 | |||
| 295 | private function getSortedEndpoints($endpoints) |
||
| 306 | |||
| 307 | private function getUriParams($route) |
||
| 313 | |||
| 314 | private function generateHashForUrl($key, $route, $method) |
||
| 324 | |||
| 325 | private function splitCamelCaseToWords($chunk) |
||
| 337 | |||
| 338 | private function getRouteParam($route, $param) |
||
| 349 | |||
| 350 | private function ins(&$ary, $keys, $val) |
||
| 356 | |||
| 357 | private function arrayGet($array, $key, $default = null) |
||
| 376 | |||
| 377 | } |
||
| 378 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.