Complex classes like Repository 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 Repository, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | abstract class Repository implements RepositoryInterface, Countable |
||
| 16 | { |
||
| 17 | use Macroable; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Application instance. |
||
| 21 | * |
||
| 22 | * @var Illuminate\Contracts\Foundation\Application|Laravel\Lumen\Application |
||
| 23 | */ |
||
| 24 | protected $app; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * The module path. |
||
| 28 | * |
||
| 29 | * @var string|null |
||
| 30 | */ |
||
| 31 | protected $path; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * The scanned paths. |
||
| 35 | * |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | protected $paths = []; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | protected $stubPath; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The constructor. |
||
| 47 | * |
||
| 48 | * @param Container $app |
||
| 49 | * @param string|null $path |
||
| 50 | */ |
||
| 51 | 161 | public function __construct(Container $app, $path = null) |
|
| 56 | |||
| 57 | /** |
||
| 58 | * Add other module location. |
||
| 59 | * |
||
| 60 | * @param string $path |
||
| 61 | * |
||
| 62 | * @return $this |
||
| 63 | */ |
||
| 64 | 15 | public function addLocation($path) |
|
| 70 | |||
| 71 | /** |
||
| 72 | * Alternative method for "addPath". |
||
| 73 | * |
||
| 74 | * @param string $path |
||
| 75 | * |
||
| 76 | * @return $this |
||
| 77 | * @deprecated |
||
| 78 | */ |
||
| 79 | 1 | public function addPath($path) |
|
| 83 | |||
| 84 | /** |
||
| 85 | * Get all additional paths. |
||
| 86 | * |
||
| 87 | * @return array |
||
| 88 | */ |
||
| 89 | 1 | public function getPaths() : array |
|
| 93 | |||
| 94 | /** |
||
| 95 | * Get scanned modules paths. |
||
| 96 | * |
||
| 97 | * @return array |
||
| 98 | */ |
||
| 99 | 161 | public function getScanPaths() : array |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Get & scan all modules. |
||
| 117 | * |
||
| 118 | * @return array |
||
| 119 | */ |
||
| 120 | abstract public function scan(); |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Get all modules. |
||
| 124 | * |
||
| 125 | * @return array |
||
| 126 | */ |
||
| 127 | 161 | public function all() : array |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Format the cached data as array of modules. |
||
| 138 | * |
||
| 139 | * @param array $cached |
||
| 140 | * |
||
| 141 | * @return array |
||
| 142 | */ |
||
| 143 | abstract protected function formatCached($cached); |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Get cached modules. |
||
| 147 | * |
||
| 148 | * @return array |
||
| 149 | */ |
||
| 150 | public function getCached() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Get all modules as collection instance. |
||
| 159 | * |
||
| 160 | * @return Collection |
||
| 161 | */ |
||
| 162 | 1 | public function toCollection() : Collection |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Get modules by status. |
||
| 169 | * |
||
| 170 | * @param $status |
||
| 171 | * |
||
| 172 | * @return array |
||
| 173 | */ |
||
| 174 | 161 | public function getByStatus($status) : array |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Determine whether the given module exist. |
||
| 189 | * |
||
| 190 | * @param $name |
||
| 191 | * |
||
| 192 | * @return bool |
||
| 193 | */ |
||
| 194 | 75 | public function has($name) : bool |
|
| 198 | |||
| 199 | /** |
||
| 200 | * Get list of enabled modules. |
||
| 201 | * |
||
| 202 | * @return array |
||
| 203 | */ |
||
| 204 | 161 | public function enabled() : array |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Get list of disabled modules. |
||
| 211 | * |
||
| 212 | * @return array |
||
| 213 | */ |
||
| 214 | 1 | public function disabled() : array |
|
| 218 | |||
| 219 | /** |
||
| 220 | * Get count from all modules. |
||
| 221 | * |
||
| 222 | * @return int |
||
| 223 | */ |
||
| 224 | 1 | public function count() : int |
|
| 228 | |||
| 229 | /** |
||
| 230 | * Get all ordered modules. |
||
| 231 | * |
||
| 232 | * @param string $direction |
||
| 233 | * |
||
| 234 | * @return array |
||
| 235 | */ |
||
| 236 | 161 | public function getOrdered($direction = 'asc') : array |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Get a module path. |
||
| 257 | * |
||
| 258 | * @return string |
||
| 259 | */ |
||
| 260 | 161 | public function getPath() : string |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Register the modules. |
||
| 267 | */ |
||
| 268 | 161 | public function register() |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Boot the modules. |
||
| 277 | */ |
||
| 278 | 161 | public function boot() |
|
| 284 | |||
| 285 | /** |
||
| 286 | * Find a specific module. |
||
| 287 | * @param $name |
||
| 288 | * @return mixed|void |
||
| 289 | */ |
||
| 290 | 84 | public function find($name) |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Find a specific module by its alias. |
||
| 303 | * @param $alias |
||
| 304 | * @return mixed|void |
||
| 305 | */ |
||
| 306 | 2 | public function findByAlias($alias) |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Find all modules that are required by a module. If the module cannot be found, throw an exception. |
||
| 319 | * |
||
| 320 | * @param $name |
||
| 321 | * @return array |
||
| 322 | * @throws ModuleNotFoundException |
||
| 323 | */ |
||
| 324 | 1 | public function findRequirements($name) |
|
| 336 | |||
| 337 | /** |
||
| 338 | * Alternative for "find" method. |
||
| 339 | * @param $name |
||
| 340 | * @return mixed|void |
||
| 341 | * @deprecated |
||
| 342 | */ |
||
| 343 | 1 | public function get($name) |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Find a specific module, if there return that, otherwise throw exception. |
||
| 350 | * |
||
| 351 | * @param $name |
||
| 352 | * |
||
| 353 | * @return Module |
||
| 354 | * |
||
| 355 | * @throws ModuleNotFoundException |
||
| 356 | */ |
||
| 357 | 82 | public function findOrFail($name) |
|
| 367 | |||
| 368 | /** |
||
| 369 | * Get all modules as laravel collection instance. |
||
| 370 | * |
||
| 371 | * @param $status |
||
| 372 | * |
||
| 373 | * @return Collection |
||
| 374 | */ |
||
| 375 | 1 | public function collections($status = 1) : Collection |
|
| 379 | |||
| 380 | /** |
||
| 381 | * Get module path for a specific module. |
||
| 382 | * |
||
| 383 | * @param $module |
||
| 384 | * |
||
| 385 | * @return string |
||
| 386 | */ |
||
| 387 | 75 | public function getModulePath($module) |
|
| 395 | |||
| 396 | /** |
||
| 397 | * Get asset path for a specific module. |
||
| 398 | * |
||
| 399 | * @param $module |
||
| 400 | * |
||
| 401 | * @return string |
||
| 402 | */ |
||
| 403 | 2 | public function assetPath($module) : string |
|
| 407 | |||
| 408 | /** |
||
| 409 | * Get a specific config data from a configuration file. |
||
| 410 | * |
||
| 411 | * @param $key |
||
| 412 | * |
||
| 413 | * @param null $default |
||
| 414 | * @return mixed |
||
| 415 | */ |
||
| 416 | 161 | public function config($key, $default = null) |
|
| 420 | |||
| 421 | /** |
||
| 422 | * Get storage path for module used. |
||
| 423 | * |
||
| 424 | * @return string |
||
| 425 | */ |
||
| 426 | 2 | public function getUsedStoragePath() : string |
|
| 440 | |||
| 441 | /** |
||
| 442 | * Set module used for cli session. |
||
| 443 | * |
||
| 444 | * @param $name |
||
| 445 | * |
||
| 446 | * @throws ModuleNotFoundException |
||
| 447 | */ |
||
| 448 | 1 | public function setUsed($name) |
|
| 454 | |||
| 455 | /** |
||
| 456 | * Forget the module used for cli session. |
||
| 457 | */ |
||
| 458 | public function forgetUsed() |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Get module used for cli session. |
||
| 467 | * @return string |
||
| 468 | * @throws \Nwidart\Modules\Exceptions\ModuleNotFoundException |
||
| 469 | */ |
||
| 470 | 1 | public function getUsedNow() : string |
|
| 474 | |||
| 475 | /** |
||
| 476 | * Get used now. |
||
| 477 | * |
||
| 478 | * @return string |
||
| 479 | * @deprecated |
||
| 480 | */ |
||
| 481 | 1 | public function getUsed() |
|
| 485 | |||
| 486 | /** |
||
| 487 | * Get laravel filesystem instance. |
||
| 488 | * |
||
| 489 | * @return \Illuminate\Filesystem\Filesystem |
||
| 490 | */ |
||
| 491 | 4 | public function getFiles() |
|
| 495 | |||
| 496 | /** |
||
| 497 | * Get module assets path. |
||
| 498 | * |
||
| 499 | * @return string |
||
| 500 | */ |
||
| 501 | 2 | public function getAssetsPath() : string |
|
| 505 | |||
| 506 | /** |
||
| 507 | * Get asset url from a specific module. |
||
| 508 | * @param string $asset |
||
| 509 | * @return string |
||
| 510 | * @throws InvalidAssetPath |
||
| 511 | */ |
||
| 512 | 2 | public function asset($asset) : string |
|
| 525 | |||
| 526 | /** |
||
| 527 | * Determine whether the given module is activated. |
||
| 528 | * |
||
| 529 | * @param string $name |
||
| 530 | * |
||
| 531 | * @return bool |
||
| 532 | */ |
||
| 533 | 4 | public function active($name) : bool |
|
| 537 | |||
| 538 | /** |
||
| 539 | * Determine whether the given module is not activated. |
||
| 540 | * |
||
| 541 | * @param string $name |
||
| 542 | * |
||
| 543 | * @return bool |
||
| 544 | */ |
||
| 545 | 2 | public function notActive($name) : bool |
|
| 549 | |||
| 550 | /** |
||
| 551 | * Enabling a specific module. |
||
| 552 | * @param string $name |
||
| 553 | * @return void |
||
| 554 | * @throws \Nwidart\Modules\Exceptions\ModuleNotFoundException |
||
| 555 | */ |
||
| 556 | 1 | public function enable($name) |
|
| 560 | |||
| 561 | /** |
||
| 562 | * Disabling a specific module. |
||
| 563 | * @param string $name |
||
| 564 | * @return void |
||
| 565 | * @throws \Nwidart\Modules\Exceptions\ModuleNotFoundException |
||
| 566 | */ |
||
| 567 | 1 | public function disable($name) |
|
| 571 | |||
| 572 | /** |
||
| 573 | * Delete a specific module. |
||
| 574 | * @param string $name |
||
| 575 | * @return bool |
||
| 576 | * @throws \Nwidart\Modules\Exceptions\ModuleNotFoundException |
||
| 577 | */ |
||
| 578 | 2 | public function delete($name) : bool |
|
| 582 | |||
| 583 | /** |
||
| 584 | * Update dependencies for the specified module. |
||
| 585 | * |
||
| 586 | * @param string $module |
||
| 587 | */ |
||
| 588 | public function update($module) |
||
| 592 | |||
| 593 | /** |
||
| 594 | * Install the specified module. |
||
| 595 | * |
||
| 596 | * @param string $name |
||
| 597 | * @param string $version |
||
| 598 | * @param string $type |
||
| 599 | * @param bool $subtree |
||
| 600 | * |
||
| 601 | * @return \Symfony\Component\Process\Process |
||
| 602 | */ |
||
| 603 | public function install($name, $version = 'dev-master', $type = 'composer', $subtree = false) |
||
| 609 | |||
| 610 | /** |
||
| 611 | * Get stub path. |
||
| 612 | * |
||
| 613 | * @return string|null |
||
| 614 | */ |
||
| 615 | 3 | public function getStubPath() |
|
| 627 | |||
| 628 | /** |
||
| 629 | * Set stub path. |
||
| 630 | * |
||
| 631 | * @param string $stubPath |
||
| 632 | * |
||
| 633 | * @return $this |
||
| 634 | */ |
||
| 635 | 1 | public function setStubPath($stubPath) |
|
| 641 | } |
||
| 642 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..