Complex classes like GenerateDocCommand 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 GenerateDocCommand, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class GenerateDocCommand extends Command |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * The name and signature of the console command. |
||
| 13 | * |
||
| 14 | * @var string |
||
| 15 | */ |
||
| 16 | protected $signature = 'doc:generate'; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * The console command description. |
||
| 20 | * |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | protected $description = 'Generate api documentation'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var ReprotService |
||
| 27 | */ |
||
| 28 | protected $reportService; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Init new object. |
||
| 32 | * |
||
| 33 | * @return void |
||
|
|
|||
| 34 | */ |
||
| 35 | public function __construct(ReportService $reportService) |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Execute the console command. |
||
| 43 | * |
||
| 44 | * @return mixed |
||
| 45 | */ |
||
| 46 | public function handle() |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Get list of all registered routes. |
||
| 92 | * |
||
| 93 | * @return collection |
||
| 94 | */ |
||
| 95 | protected function getRoutes() |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Generate headers for the given route. |
||
| 112 | * |
||
| 113 | * @param array &$route |
||
| 114 | * @param string $method |
||
| 115 | * @param array $skipLoginCheck |
||
| 116 | * @return void |
||
| 117 | */ |
||
| 118 | protected function getHeaders(&$route, $method, $skipLoginCheck) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Generate description and params for the given route |
||
| 135 | * based on the docblock. |
||
| 136 | * |
||
| 137 | * @param array &$route |
||
| 138 | * @param \ReflectionMethod $reflectionMethod |
||
| 139 | * @return void |
||
| 140 | */ |
||
| 141 | protected function processDocBlock(&$route, $reflectionMethod) |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Generate post body for the given route. |
||
| 166 | * |
||
| 167 | * @param array &$route |
||
| 168 | * @param \ReflectionMethod $reflectionMethod |
||
| 169 | * @return void |
||
| 170 | */ |
||
| 171 | protected function getPostData(&$route, $reflectionMethod) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Generate application errors. |
||
| 197 | * |
||
| 198 | * @return array |
||
| 199 | */ |
||
| 200 | protected function getErrors() |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Get the given method body code. |
||
| 225 | * |
||
| 226 | * @param object $reflectionMethod |
||
| 227 | * @return string |
||
| 228 | */ |
||
| 229 | protected function getMethodBody($reflectionMethod) |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Get example object of all availble models. |
||
| 244 | * |
||
| 245 | * @param string $modelName |
||
| 246 | * @param array $docData |
||
| 247 | * @return string |
||
| 248 | */ |
||
| 249 | protected function getModels($modelName, &$docData, $reflectionClass) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Get the route response object type. |
||
| 273 | * |
||
| 274 | * @param string $modelName |
||
| 275 | * @param string $method |
||
| 276 | * @param string $returnDocBlock |
||
| 277 | * @return array |
||
| 278 | */ |
||
| 279 | protected function getResponseObject($modelName, $method, $returnDocBlock) |
||
| 287 | } |
||
| 288 |
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.