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 |
||
| 13 | class Module extends ServiceProvider |
||
| 14 | { |
||
| 15 | use Macroable; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * The laravel|lumen application instance. |
||
| 19 | * |
||
| 20 | * @var Illuminate\Contracts\Foundation\Application|Laravel\Lumen\Application |
||
| 21 | */ |
||
| 22 | protected $app; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * The module name. |
||
| 26 | * |
||
| 27 | * @var |
||
| 28 | */ |
||
| 29 | protected $name; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * The module path. |
||
| 33 | * |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | protected $path; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var array of cached Json objects, keyed by filename |
||
| 40 | */ |
||
| 41 | protected $moduleJson = []; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * The constructor. |
||
| 45 | * |
||
| 46 | * @param Container $app |
||
| 47 | * @param $name |
||
| 48 | * @param $path |
||
| 49 | */ |
||
| 50 | 89 | public function __construct(Container $app, $name, $path) |
|
| 56 | |||
| 57 | /** |
||
| 58 | * Get laravel instance. |
||
| 59 | * |
||
| 60 | * @return Illuminate\Contracts\Foundation\Application|Laravel\Lumen\Application |
||
| 61 | */ |
||
| 62 | 1 | public function getLaravel() |
|
| 66 | |||
| 67 | /** |
||
| 68 | * Get name. |
||
| 69 | * |
||
| 70 | * @return string |
||
| 71 | */ |
||
| 72 | 1 | public function getName() |
|
| 76 | |||
| 77 | /** |
||
| 78 | * Get name in lower case. |
||
| 79 | * |
||
| 80 | * @return string |
||
| 81 | */ |
||
| 82 | 68 | public function getLowerName() |
|
| 86 | |||
| 87 | /** |
||
| 88 | * Get name in studly case. |
||
| 89 | * |
||
| 90 | * @return string |
||
| 91 | */ |
||
| 92 | 56 | public function getStudlyName() |
|
| 96 | |||
| 97 | /** |
||
| 98 | * Get name in snake case. |
||
| 99 | * |
||
| 100 | * @return string |
||
| 101 | */ |
||
| 102 | 4 | public function getSnakeName() |
|
| 103 | { |
||
| 104 | 4 | return Str::snake($this->name); |
|
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Get description. |
||
| 109 | * |
||
| 110 | * @return string |
||
| 111 | */ |
||
| 112 | 1 | public function getDescription() |
|
| 116 | |||
| 117 | /** |
||
| 118 | * Get alias. |
||
| 119 | * |
||
| 120 | * @return string |
||
| 121 | */ |
||
| 122 | 3 | public function getAlias() |
|
| 126 | |||
| 127 | /** |
||
| 128 | * Get priority. |
||
| 129 | * |
||
| 130 | * @return string |
||
| 131 | */ |
||
| 132 | public function getPriority() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Get module requirements. |
||
| 139 | * |
||
| 140 | * @return array |
||
| 141 | */ |
||
| 142 | 2 | public function getRequires() |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Get path. |
||
| 149 | * |
||
| 150 | * @return string |
||
| 151 | */ |
||
| 152 | 79 | public function getPath() |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Set path. |
||
| 159 | * |
||
| 160 | * @param string $path |
||
| 161 | * |
||
| 162 | * @return $this |
||
| 163 | */ |
||
| 164 | public function setPath($path) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Bootstrap the application events. |
||
| 173 | */ |
||
| 174 | 1 | public function boot() |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Register module's translation. |
||
| 185 | * |
||
| 186 | * @return void |
||
| 187 | */ |
||
| 188 | 1 | protected function registerTranslation() |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Get json contents from the cache, setting as needed. |
||
| 201 | * |
||
| 202 | * @param $file |
||
| 203 | * |
||
| 204 | * @return Json |
||
| 205 | */ |
||
| 206 | 24 | public function json($file = null) |
|
| 216 | |||
| 217 | /** |
||
| 218 | * Get a specific data from json file by given the key. |
||
| 219 | * |
||
| 220 | * @param $key |
||
| 221 | * @param null $default |
||
| 222 | * |
||
| 223 | * @return mixed |
||
| 224 | */ |
||
| 225 | 18 | public function get($key, $default = null) |
|
| 229 | |||
| 230 | /** |
||
| 231 | * Get a specific data from composer.json file by given the key. |
||
| 232 | * |
||
| 233 | * @param $key |
||
| 234 | * @param null $default |
||
| 235 | * |
||
| 236 | * @return mixed |
||
| 237 | */ |
||
| 238 | 1 | public function getComposerAttr($key, $default = null) |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Register the module. |
||
| 245 | */ |
||
| 246 | public function register() |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Register the module event. |
||
| 259 | * |
||
| 260 | * @param string $event |
||
| 261 | */ |
||
| 262 | 5 | protected function fireEvent($event) |
|
| 266 | |||
| 267 | /** |
||
| 268 | * Register the aliases from this module. |
||
| 269 | */ |
||
| 270 | protected function registerAliases() |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Register the service providers from this module. |
||
| 280 | */ |
||
| 281 | 2 | protected function registerProviders() |
|
| 282 | { |
||
| 283 | 2 | (new ProviderRepository($this->app, new Filesystem, $this->getCachedServicesPath())) |
|
| 284 | 2 | ->load($this->get('providers', [])); |
|
| 285 | 2 | } |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Get the path to the cached *_module.php file. |
||
| 289 | * |
||
| 290 | * @return string |
||
| 291 | */ |
||
| 292 | 3 | public function getCachedServicesPath() |
|
| 293 | { |
||
| 294 | 3 | return Str::replaceLast('services.php', $this->getSnakeName().'_module.php', $this->app->getCachedServicesPath()); |
|
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Register the files from this module. |
||
| 299 | */ |
||
| 300 | protected function registerFiles() |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Handle call __toString. |
||
| 309 | * |
||
| 310 | * @return string |
||
| 311 | */ |
||
| 312 | 2 | public function __toString() |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Determine whether the given status same with the current module status. |
||
| 319 | * |
||
| 320 | * @param $status |
||
| 321 | * |
||
| 322 | * @return bool |
||
| 323 | */ |
||
| 324 | 9 | public function isStatus($status) |
|
| 328 | |||
| 329 | /** |
||
| 330 | * Determine whether the current module activated. |
||
| 331 | * |
||
| 332 | * @return bool |
||
| 333 | */ |
||
| 334 | 1 | public function enabled() |
|
| 338 | |||
| 339 | /** |
||
| 340 | * Alternate for "enabled" method. |
||
| 341 | * |
||
| 342 | * @return bool |
||
| 343 | */ |
||
| 344 | 5 | public function active() |
|
| 348 | |||
| 349 | /** |
||
| 350 | * Determine whether the current module not activated. |
||
| 351 | * |
||
| 352 | * @return bool |
||
| 353 | */ |
||
| 354 | 1 | public function notActive() |
|
| 358 | |||
| 359 | /** |
||
| 360 | * Alias for "notActive" method. |
||
| 361 | * |
||
| 362 | * @return bool |
||
| 363 | */ |
||
| 364 | 1 | public function disabled() |
|
| 368 | |||
| 369 | /** |
||
| 370 | * Set active state for current module. |
||
| 371 | * |
||
| 372 | * @param $active |
||
| 373 | * |
||
| 374 | * @return bool |
||
| 375 | */ |
||
| 376 | 4 | public function setActive($active) |
|
| 380 | |||
| 381 | /** |
||
| 382 | * Disable the current module. |
||
| 383 | */ |
||
| 384 | 2 | public function disable() |
|
| 392 | |||
| 393 | /** |
||
| 394 | * Enable the current module. |
||
| 395 | */ |
||
| 396 | 2 | public function enable() |
|
| 404 | |||
| 405 | /** |
||
| 406 | * Delete the current module. |
||
| 407 | * |
||
| 408 | * @return bool |
||
| 409 | */ |
||
| 410 | 2 | public function delete() |
|
| 414 | |||
| 415 | /** |
||
| 416 | * Get extra path. |
||
| 417 | * |
||
| 418 | * @param $path |
||
| 419 | * |
||
| 420 | * @return string |
||
| 421 | */ |
||
| 422 | 3 | public function getExtraPath($path) |
|
| 426 | |||
| 427 | /** |
||
| 428 | * Handle call to __get method. |
||
| 429 | * |
||
| 430 | * @param $key |
||
| 431 | * |
||
| 432 | * @return mixed |
||
| 433 | */ |
||
| 434 | public function __get($key) |
||
| 438 | } |
||
| 439 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: