Complex classes like Module 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 Module, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | abstract class Module extends ServiceProvider |
||
| 12 | { |
||
| 13 | use Macroable; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * The laravel|lumen application instance. |
||
| 17 | * |
||
| 18 | * @var \Illuminate\Contracts\Foundation\Application|\Laravel\Lumen\Application |
||
| 19 | */ |
||
| 20 | protected $app; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * The module name. |
||
| 24 | * |
||
| 25 | * @var |
||
| 26 | */ |
||
| 27 | protected $name; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The module path. |
||
| 31 | * |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $path; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var array of cached Json objects, keyed by filename |
||
| 38 | */ |
||
| 39 | protected $moduleJson = []; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The constructor. |
||
| 43 | * |
||
| 44 | * @param Container $app |
||
| 45 | * @param $name |
||
| 46 | * @param $path |
||
| 47 | 147 | */ |
|
| 48 | public function __construct(Container $app, $name, $path) |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Get laravel instance. |
||
| 57 | * |
||
| 58 | * @return \Illuminate\Contracts\Foundation\Application|\Laravel\Lumen\Application |
||
| 59 | 1 | */ |
|
| 60 | public function getLaravel() |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Get name. |
||
| 67 | * |
||
| 68 | * @return string |
||
| 69 | 2 | */ |
|
| 70 | public function getName() |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Get name in lower case. |
||
| 77 | * |
||
| 78 | * @return string |
||
| 79 | 111 | */ |
|
| 80 | public function getLowerName() |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Get name in studly case. |
||
| 87 | * |
||
| 88 | * @return string |
||
| 89 | 97 | */ |
|
| 90 | public function getStudlyName() |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Get name in snake case. |
||
| 97 | * |
||
| 98 | * @return string |
||
| 99 | 6 | */ |
|
| 100 | public function getSnakeName() |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Get description. |
||
| 107 | * |
||
| 108 | * @return string |
||
| 109 | 2 | */ |
|
| 110 | public function getDescription() |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Get alias. |
||
| 117 | * |
||
| 118 | * @return string |
||
| 119 | 4 | */ |
|
| 120 | public function getAlias() |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Get priority. |
||
| 127 | * |
||
| 128 | * @return string |
||
| 129 | */ |
||
| 130 | public function getPriority() |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Get module requirements. |
||
| 137 | * |
||
| 138 | * @return array |
||
| 139 | 3 | */ |
|
| 140 | public function getRequires() |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Get path. |
||
| 147 | * |
||
| 148 | * @return string |
||
| 149 | 131 | */ |
|
| 150 | public function getPath() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Set path. |
||
| 157 | * |
||
| 158 | * @param string $path |
||
| 159 | * |
||
| 160 | * @return $this |
||
| 161 | */ |
||
| 162 | public function setPath($path) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Bootstrap the application events. |
||
| 171 | 2 | */ |
|
| 172 | public function boot() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Register module's translation. |
||
| 187 | * |
||
| 188 | * @return void |
||
| 189 | 2 | */ |
|
| 190 | protected function registerTranslation() |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Get json contents from the cache, setting as needed. |
||
| 203 | * |
||
| 204 | * @param string $file |
||
| 205 | * |
||
| 206 | * @return Json |
||
| 207 | 34 | */ |
|
| 208 | public function json($file = null) : Json |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Get a specific data from json file by given the key. |
||
| 221 | * |
||
| 222 | * @param string $key |
||
| 223 | * @param null $default |
||
| 224 | * |
||
| 225 | * @return mixed |
||
| 226 | 24 | */ |
|
| 227 | public function get(string $key, $default = null) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Get a specific data from composer.json file by given the key. |
||
| 234 | * |
||
| 235 | * @param $key |
||
| 236 | * @param null $default |
||
| 237 | * |
||
| 238 | * @return mixed |
||
| 239 | 2 | */ |
|
| 240 | public function getComposerAttr($key, $default = null) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Register the module. |
||
| 247 | */ |
||
| 248 | public function register() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Register the module event. |
||
| 263 | * |
||
| 264 | * @param string $event |
||
| 265 | 8 | */ |
|
| 266 | protected function fireEvent($event) |
||
| 270 | /** |
||
| 271 | * Register the aliases from this module. |
||
| 272 | */ |
||
| 273 | abstract public function registerAliases(); |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Register the service providers from this module. |
||
| 277 | */ |
||
| 278 | abstract public function registerProviders(); |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Get the path to the cached *_module.php file. |
||
| 282 | * |
||
| 283 | * @return string |
||
| 284 | */ |
||
| 285 | abstract public function getCachedServicesPath(); |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Register the files from this module. |
||
| 289 | */ |
||
| 290 | protected function registerFiles() |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Handle call __toString. |
||
| 299 | * |
||
| 300 | * @return string |
||
| 301 | 3 | */ |
|
| 302 | public function __toString() |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Determine whether the given status same with the current module status. |
||
| 309 | * |
||
| 310 | * @param $status |
||
| 311 | * |
||
| 312 | * @return bool |
||
| 313 | 11 | */ |
|
| 314 | public function isStatus($status) : bool |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Determine whether the current module activated. |
||
| 321 | * |
||
| 322 | * @return bool |
||
| 323 | 6 | */ |
|
| 324 | public function enabled() : bool |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Determine whether the current module not disabled. |
||
| 331 | * |
||
| 332 | * @return bool |
||
| 333 | 2 | */ |
|
| 334 | public function disabled() : bool |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Set active state for current module. |
||
| 341 | * |
||
| 342 | * @param $active |
||
| 343 | * |
||
| 344 | * @return bool |
||
| 345 | 6 | */ |
|
| 346 | public function setActive($active) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Disable the current module. |
||
| 353 | 3 | */ |
|
| 354 | public function disable() |
||
| 363 | |||
| 364 | /** |
||
| 365 | 3 | * Enable the current module. |
|
| 366 | */ |
||
| 367 | 3 | public function enable() |
|
| 376 | |||
| 377 | /** |
||
| 378 | * Delete the current module. |
||
| 379 | 2 | * |
|
| 380 | * @return bool |
||
| 381 | 2 | */ |
|
| 382 | public function delete() |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Get extra path. |
||
| 389 | * |
||
| 390 | * @param string $path |
||
| 391 | 3 | * |
|
| 392 | * @return string |
||
| 393 | 3 | */ |
|
| 394 | public function getExtraPath(string $path) : string |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Handle call to __get method. |
||
| 401 | * |
||
| 402 | * @param $key |
||
| 403 | * |
||
| 404 | * @return mixed |
||
| 405 | */ |
||
| 406 | public function __get($key) |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Check if can load files of module on boot method. |
||
| 413 | 2 | * |
|
| 414 | * @return bool |
||
| 415 | 2 | */ |
|
| 416 | protected function isLoadFilesOnBoot() |
||
| 422 | |||
| 423 | private function flushCache(): void |
||
| 429 | } |
||
| 430 |