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 | 139 | 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 | */ |
||
| 78 | 1 | public function addPath($path) |
|
| 82 | |||
| 83 | /** |
||
| 84 | * Get all additional paths. |
||
| 85 | * |
||
| 86 | * @return array |
||
| 87 | */ |
||
| 88 | 1 | public function getPaths() |
|
| 92 | |||
| 93 | /** |
||
| 94 | * Get scanned modules paths. |
||
| 95 | * |
||
| 96 | * @return array |
||
| 97 | */ |
||
| 98 | 139 | public function getScanPaths() |
|
| 110 | |||
| 111 | /** |
||
| 112 | * Get & scan all modules. |
||
| 113 | * |
||
| 114 | * @return array |
||
| 115 | */ |
||
| 116 | abstract public function scan(); |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Get all modules. |
||
| 120 | * |
||
| 121 | * @return array |
||
| 122 | */ |
||
| 123 | 139 | public function all() |
|
| 131 | |||
| 132 | /** |
||
| 133 | * Format the cached data as array of modules. |
||
| 134 | * |
||
| 135 | * @param array $cached |
||
| 136 | * |
||
| 137 | * @return array |
||
| 138 | */ |
||
| 139 | abstract protected function formatCached($cached); |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Get cached modules. |
||
| 143 | * |
||
| 144 | * @return array |
||
| 145 | */ |
||
| 146 | public function getCached() |
||
| 147 | { |
||
| 148 | return $this->app['cache']->remember($this->config('cache.key'), $this->config('cache.lifetime'), function () { |
||
| 149 | return $this->toCollection()->toArray(); |
||
| 150 | }); |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Get all modules as collection instance. |
||
| 155 | * |
||
| 156 | * @return Collection |
||
| 157 | */ |
||
| 158 | 1 | public function toCollection() |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Get modules by status. |
||
| 165 | * |
||
| 166 | * @param $status |
||
| 167 | * |
||
| 168 | * @return array |
||
| 169 | */ |
||
| 170 | 139 | public function getByStatus($status) |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Determine whether the given module exist. |
||
| 185 | * |
||
| 186 | * @param $name |
||
| 187 | * |
||
| 188 | * @return bool |
||
| 189 | */ |
||
| 190 | 57 | public function has($name) |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Get list of enabled modules. |
||
| 197 | * |
||
| 198 | * @return array |
||
| 199 | */ |
||
| 200 | 139 | public function enabled() |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Get list of disabled modules. |
||
| 207 | * |
||
| 208 | * @return array |
||
| 209 | */ |
||
| 210 | 1 | public function disabled() |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Get count from all modules. |
||
| 217 | * |
||
| 218 | * @return int |
||
| 219 | */ |
||
| 220 | 1 | public function count() |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Get all ordered modules. |
||
| 227 | * |
||
| 228 | * @param string $direction |
||
| 229 | * |
||
| 230 | * @return array |
||
| 231 | */ |
||
| 232 | 139 | public function getOrdered($direction = 'asc') |
|
| 233 | { |
||
| 234 | 139 | $modules = $this->enabled(); |
|
| 235 | |||
| 236 | 139 | uasort($modules, function (Module $a, Module $b) use ($direction) { |
|
| 237 | if ($a->order == $b->order) { |
||
| 238 | return 0; |
||
| 239 | } |
||
| 240 | |||
| 241 | if ($direction == 'desc') { |
||
| 242 | return $a->order < $b->order ? 1 : -1; |
||
| 243 | } |
||
| 244 | |||
| 245 | return $a->order > $b->order ? 1 : -1; |
||
| 246 | 139 | }); |
|
| 247 | |||
| 248 | 139 | return $modules; |
|
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Get a module path. |
||
| 253 | * |
||
| 254 | * @return string |
||
| 255 | */ |
||
| 256 | 139 | public function getPath() |
|
| 260 | |||
| 261 | /** |
||
| 262 | * Register the modules. |
||
| 263 | */ |
||
| 264 | 139 | public function register() |
|
| 270 | |||
| 271 | /** |
||
| 272 | * Boot the modules. |
||
| 273 | */ |
||
| 274 | 139 | public function boot() |
|
| 280 | |||
| 281 | /** |
||
| 282 | * Find a specific module. |
||
| 283 | * @param $name |
||
| 284 | * @return mixed|void |
||
| 285 | */ |
||
| 286 | 66 | public function find($name) |
|
| 296 | |||
| 297 | /** |
||
| 298 | * Find a specific module by its alias. |
||
| 299 | * @param $alias |
||
| 300 | * @return mixed|void |
||
| 301 | */ |
||
| 302 | 2 | public function findByAlias($alias) |
|
| 312 | |||
| 313 | /** |
||
| 314 | * Find all modules that are required by a module. If the module cannot be found, throw an exception. |
||
| 315 | * |
||
| 316 | * @param $name |
||
| 317 | * @return array |
||
| 318 | * @throws ModuleNotFoundException |
||
| 319 | */ |
||
| 320 | 1 | public function findRequirements($name) |
|
| 332 | |||
| 333 | /** |
||
| 334 | * Alternative for "find" method. |
||
| 335 | * @param $name |
||
| 336 | * @return mixed|void |
||
| 337 | */ |
||
| 338 | 1 | public function get($name) |
|
| 342 | |||
| 343 | /** |
||
| 344 | * Find a specific module, if there return that, otherwise throw exception. |
||
| 345 | * |
||
| 346 | * @param $name |
||
| 347 | * |
||
| 348 | * @return Module |
||
| 349 | * |
||
| 350 | * @throws ModuleNotFoundException |
||
| 351 | */ |
||
| 352 | 64 | public function findOrFail($name) |
|
| 362 | |||
| 363 | /** |
||
| 364 | * Get all modules as laravel collection instance. |
||
| 365 | * |
||
| 366 | * @param $status |
||
| 367 | * |
||
| 368 | * @return Collection |
||
| 369 | */ |
||
| 370 | 1 | public function collections($status = 1) |
|
| 374 | |||
| 375 | /** |
||
| 376 | * Get module path for a specific module. |
||
| 377 | * |
||
| 378 | * @param $module |
||
| 379 | * |
||
| 380 | * @return string |
||
| 381 | */ |
||
| 382 | 57 | public function getModulePath($module) |
|
| 390 | |||
| 391 | /** |
||
| 392 | * Get asset path for a specific module. |
||
| 393 | * |
||
| 394 | * @param $module |
||
| 395 | * |
||
| 396 | * @return string |
||
| 397 | */ |
||
| 398 | 2 | public function assetPath($module) |
|
| 402 | |||
| 403 | /** |
||
| 404 | * Get a specific config data from a configuration file. |
||
| 405 | * |
||
| 406 | * @param $key |
||
| 407 | * |
||
| 408 | * @param null $default |
||
| 409 | * @return mixed |
||
| 410 | */ |
||
| 411 | 139 | public function config($key, $default = null) |
|
| 415 | |||
| 416 | /** |
||
| 417 | * Get storage path for module used. |
||
| 418 | * |
||
| 419 | * @return string |
||
| 420 | */ |
||
| 421 | 2 | public function getUsedStoragePath() |
|
| 435 | |||
| 436 | /** |
||
| 437 | * Set module used for cli session. |
||
| 438 | * |
||
| 439 | * @param $name |
||
| 440 | * |
||
| 441 | * @throws ModuleNotFoundException |
||
| 442 | */ |
||
| 443 | 1 | public function setUsed($name) |
|
| 449 | |||
| 450 | /** |
||
| 451 | * Forget the module used for cli session. |
||
| 452 | */ |
||
| 453 | public function forgetUsed() |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Get module used for cli session. |
||
| 462 | * |
||
| 463 | * @return string |
||
| 464 | */ |
||
| 465 | 1 | public function getUsedNow() |
|
| 469 | |||
| 470 | /** |
||
| 471 | * Get used now. |
||
| 472 | * |
||
| 473 | * @return string |
||
| 474 | */ |
||
| 475 | 1 | public function getUsed() |
|
| 479 | |||
| 480 | /** |
||
| 481 | * Get laravel filesystem instance. |
||
| 482 | * |
||
| 483 | * @return \Illuminate\Filesystem\Filesystem |
||
| 484 | */ |
||
| 485 | 4 | public function getFiles() |
|
| 489 | |||
| 490 | /** |
||
| 491 | * Get module assets path. |
||
| 492 | * |
||
| 493 | * @return string |
||
| 494 | */ |
||
| 495 | 2 | public function getAssetsPath() |
|
| 499 | |||
| 500 | /** |
||
| 501 | * Get asset url from a specific module. |
||
| 502 | * |
||
| 503 | * @param string $asset |
||
| 504 | * |
||
| 505 | * @return string |
||
| 506 | */ |
||
| 507 | 2 | public function asset($asset) |
|
| 520 | |||
| 521 | /** |
||
| 522 | * Determine whether the given module is activated. |
||
| 523 | * |
||
| 524 | * @param string $name |
||
| 525 | * |
||
| 526 | * @return bool |
||
| 527 | */ |
||
| 528 | 4 | public function active($name) |
|
| 532 | |||
| 533 | /** |
||
| 534 | * Determine whether the given module is not activated. |
||
| 535 | * |
||
| 536 | * @param string $name |
||
| 537 | * |
||
| 538 | * @return bool |
||
| 539 | */ |
||
| 540 | 2 | public function notActive($name) |
|
| 544 | |||
| 545 | /** |
||
| 546 | * Enabling a specific module. |
||
| 547 | * |
||
| 548 | * @param string $name |
||
| 549 | * |
||
| 550 | * @return bool |
||
| 551 | */ |
||
| 552 | 1 | public function enable($name) |
|
| 556 | |||
| 557 | /** |
||
| 558 | * Disabling a specific module. |
||
| 559 | * |
||
| 560 | * @param string $name |
||
| 561 | * |
||
| 562 | * @return bool |
||
| 563 | */ |
||
| 564 | 1 | public function disable($name) |
|
| 568 | |||
| 569 | /** |
||
| 570 | * Delete a specific module. |
||
| 571 | * |
||
| 572 | * @param string $name |
||
| 573 | * |
||
| 574 | * @return bool |
||
| 575 | */ |
||
| 576 | 2 | public function delete($name) |
|
| 580 | |||
| 581 | /** |
||
| 582 | * Update dependencies for the specified module. |
||
| 583 | * |
||
| 584 | * @param string $module |
||
| 585 | */ |
||
| 586 | public function update($module) |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Install the specified module. |
||
| 593 | * |
||
| 594 | * @param string $name |
||
| 595 | * @param string $version |
||
| 596 | * @param string $type |
||
| 597 | * @param bool $subtree |
||
| 598 | * |
||
| 599 | * @return \Symfony\Component\Process\Process |
||
| 600 | */ |
||
| 601 | public function install($name, $version = 'dev-master', $type = 'composer', $subtree = false) |
||
| 607 | |||
| 608 | /** |
||
| 609 | * Get stub path. |
||
| 610 | * |
||
| 611 | * @return string |
||
| 612 | */ |
||
| 613 | 3 | public function getStubPath() |
|
| 625 | |||
| 626 | /** |
||
| 627 | * Set stub path. |
||
| 628 | * |
||
| 629 | * @param string $stubPath |
||
| 630 | * |
||
| 631 | * @return $this |
||
| 632 | */ |
||
| 633 | 1 | public function setStubPath($stubPath) |
|
| 639 | } |
||
| 640 |
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..