Complex classes like GenerateDocumentation 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 GenerateDocumentation, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class GenerateDocumentation extends Command |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * The name and signature of the console command. |
||
| 21 | * |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | protected $signature = 'api:generate |
||
| 25 | {--output=public/docs : The output path for the generated documentation} |
||
| 26 | {--routeDomain= : The route domain (or domains) to use for generation} |
||
| 27 | {--routePrefix= : The route prefix (or prefixes) to use for generation} |
||
| 28 | {--routes=* : The route names to use for generation} |
||
| 29 | {--middleware= : The middleware to use for generation} |
||
| 30 | {--noResponseCalls : Disable API response calls} |
||
| 31 | {--noPostmanCollection : Disable Postman collection creation} |
||
| 32 | {--useMiddlewares : Use all configured route middlewares} |
||
| 33 | {--authProvider=users : The authentication provider to use for API response calls} |
||
| 34 | {--authGuard=web : The authentication guard to use for API response calls} |
||
| 35 | {--actAsUserId= : The user ID to use for API response calls} |
||
| 36 | {--router=laravel : The router to be used (Laravel or Dingo)} |
||
| 37 | {--force : Force rewriting of existing routes} |
||
| 38 | {--bindings= : Route Model Bindings} |
||
| 39 | {--header=* : Custom HTTP headers to add to the example requests. Separate the header name and value with ":"} |
||
| 40 | '; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * The console command description. |
||
| 44 | * |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | protected $description = 'Generate your API documentation from existing Laravel routes.'; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Create a new command instance. |
||
| 51 | * |
||
| 52 | * @return void |
||
|
|
|||
| 53 | */ |
||
| 54 | public function __construct() |
||
| 55 | { |
||
| 56 | parent::__construct(); |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Execute the console command. |
||
| 61 | * |
||
| 62 | * @return false|null |
||
| 63 | */ |
||
| 64 | public function handle() |
||
| 65 | { |
||
| 66 | if ($this->option('router') === 'laravel') { |
||
| 67 | $generator = new LaravelGenerator(); |
||
| 68 | } else { |
||
| 69 | $generator = new DingoGenerator(); |
||
| 70 | } |
||
| 71 | |||
| 72 | $allowedRoutes = $this->option('routes'); |
||
| 73 | $routeDomain = $this->option('routeDomain'); |
||
| 74 | $routePrefix = $this->option('routePrefix'); |
||
| 75 | $middleware = $this->option('middleware'); |
||
| 76 | |||
| 77 | $this->setUserToBeImpersonated($this->option('actAsUserId')); |
||
| 78 | |||
| 79 | if ($routePrefix === null && $routeDomain === null && ! count($allowedRoutes) && $middleware === null) { |
||
| 80 | $this->error('You must provide either a route prefix, a route domain, a route or a middleware to generate the documentation.'); |
||
| 81 | |||
| 82 | return false; |
||
| 83 | } |
||
| 84 | |||
| 85 | $generator->prepareMiddleware($this->option('useMiddlewares')); |
||
| 86 | |||
| 87 | $routePrefixes = explode(',', $routePrefix ?: '*'); |
||
| 88 | $routeDomains = explode(',', $routeDomain ?: '*'); |
||
| 89 | |||
| 90 | $parsedRoutes = []; |
||
| 91 | |||
| 92 | foreach ($routeDomains as $routeDomain) { |
||
| 93 | foreach ($routePrefixes as $routePrefix) { |
||
| 94 | $parsedRoutes += $this->processRoutes($generator, $allowedRoutes, $routeDomain, $routePrefix, $middleware); |
||
| 95 | } |
||
| 96 | } |
||
| 97 | $parsedRoutes = collect($parsedRoutes)->groupBy('resource')->sort(function ($a, $b) { |
||
| 98 | return strcmp($a->first()['resource'], $b->first()['resource']); |
||
| 99 | }); |
||
| 100 | |||
| 101 | $this->writeMarkdown($parsedRoutes); |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @param Collection $parsedRoutes |
||
| 106 | * |
||
| 107 | * @return void |
||
| 108 | */ |
||
| 109 | private function writeMarkdown($parsedRoutes) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @return array |
||
| 204 | */ |
||
| 205 | private function getBindings() |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @param $actAs |
||
| 224 | */ |
||
| 225 | private function setUserToBeImpersonated($actAs) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @return mixed |
||
| 243 | */ |
||
| 244 | private function getRoutes() |
||
| 252 | |||
| 253 | /** |
||
| 254 | * @param AbstractGenerator $generator |
||
| 255 | * @param $allowedRoutes |
||
| 256 | * @param $routeDomain |
||
| 257 | * @param $routePrefix |
||
| 258 | * |
||
| 259 | * @return array |
||
| 260 | */ |
||
| 261 | private function processRoutes(AbstractGenerator $generator, array $allowedRoutes, $routeDomain, $routePrefix, $middleware) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @param $route |
||
| 288 | * |
||
| 289 | * @return bool |
||
| 290 | */ |
||
| 291 | private function isValidRoute(Route $route) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * @param $route |
||
| 298 | * |
||
| 299 | * @return bool |
||
| 300 | */ |
||
| 301 | private function isRouteVisibleForDocumentation($route) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Generate Postman collection JSON file. |
||
| 321 | * |
||
| 322 | * @param Collection $routes |
||
| 323 | * |
||
| 324 | * @return string |
||
| 325 | */ |
||
| 326 | private function generatePostmanCollection(Collection $routes) |
||
| 332 | } |
||
| 333 |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.