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 | 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 | 118 | 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 | 118 | public function getScanPaths() |
|
| 110 | |||
| 111 | /** |
||
| 112 | * Get & scan all modules. |
||
| 113 | * |
||
| 114 | * @return array |
||
| 115 | */ |
||
| 116 | 118 | public function scan() |
|
| 136 | |||
| 137 | /** |
||
| 138 | * Get all modules. |
||
| 139 | * |
||
| 140 | * @return array |
||
| 141 | */ |
||
| 142 | 118 | public function all() |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Format the cached data as array of modules. |
||
| 153 | * |
||
| 154 | * @param array $cached |
||
| 155 | * |
||
| 156 | * @return array |
||
| 157 | */ |
||
| 158 | protected function formatCached($cached) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Get cached modules. |
||
| 173 | * |
||
| 174 | * @return array |
||
| 175 | */ |
||
| 176 | public function getCached() |
||
| 177 | { |
||
| 178 | return $this->app['cache']->remember($this->config('cache.key'), $this->config('cache.lifetime'), function () { |
||
| 179 | return $this->toCollection()->toArray(); |
||
| 180 | }); |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Get all modules as collection instance. |
||
| 185 | * |
||
| 186 | * @return Collection |
||
| 187 | */ |
||
| 188 | 1 | public function toCollection() |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Get modules by status. |
||
| 195 | * |
||
| 196 | * @param $status |
||
| 197 | * |
||
| 198 | * @return array |
||
| 199 | */ |
||
| 200 | 118 | public function getByStatus($status) |
|
| 212 | |||
| 213 | /** |
||
| 214 | * Determine whether the given module exist. |
||
| 215 | * |
||
| 216 | * @param $name |
||
| 217 | * |
||
| 218 | * @return bool |
||
| 219 | */ |
||
| 220 | 56 | public function has($name) |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Get list of enabled modules. |
||
| 227 | * |
||
| 228 | * @return array |
||
| 229 | */ |
||
| 230 | 118 | public function enabled() |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Get list of disabled modules. |
||
| 237 | * |
||
| 238 | * @return array |
||
| 239 | */ |
||
| 240 | 1 | public function disabled() |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Get count from all modules. |
||
| 247 | * |
||
| 248 | * @return int |
||
| 249 | */ |
||
| 250 | 1 | public function count() |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Get all ordered modules. |
||
| 257 | * |
||
| 258 | * @param string $direction |
||
| 259 | * |
||
| 260 | * @return array |
||
| 261 | */ |
||
| 262 | 118 | public function getOrdered($direction = 'asc') |
|
| 263 | { |
||
| 264 | 118 | $modules = $this->enabled(); |
|
| 265 | |||
| 266 | 118 | uasort($modules, function (Module $a, Module $b) use ($direction) { |
|
| 267 | if ($a->order == $b->order) { |
||
| 268 | return 0; |
||
| 269 | } |
||
| 270 | |||
| 271 | if ($direction == 'desc') { |
||
| 272 | return $a->order < $b->order ? 1 : -1; |
||
| 273 | } |
||
| 274 | |||
| 275 | return $a->order > $b->order ? 1 : -1; |
||
| 276 | 118 | }); |
|
| 277 | |||
| 278 | 118 | return $modules; |
|
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Get a module path. |
||
| 283 | * |
||
| 284 | * @return string |
||
| 285 | */ |
||
| 286 | 118 | public function getPath() |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Register the modules. |
||
| 293 | */ |
||
| 294 | 118 | public function register() |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Boot the modules. |
||
| 303 | */ |
||
| 304 | 118 | public function boot() |
|
| 310 | |||
| 311 | /** |
||
| 312 | * Find a specific module. |
||
| 313 | * @param $name |
||
| 314 | * @return mixed|void |
||
| 315 | */ |
||
| 316 | 65 | public function find($name) |
|
| 326 | |||
| 327 | /** |
||
| 328 | * Find a specific module by its alias. |
||
| 329 | * @param $alias |
||
| 330 | * @return mixed|void |
||
| 331 | */ |
||
| 332 | 2 | public function findByAlias($alias) |
|
| 342 | |||
| 343 | /** |
||
| 344 | * Find all modules that are required by a module. If the module cannot be found, throw an exception. |
||
| 345 | * |
||
| 346 | * @param $name |
||
| 347 | * @return array |
||
| 348 | * @throws ModuleNotFoundException |
||
| 349 | */ |
||
| 350 | 1 | public function findRequirements($name) |
|
| 362 | |||
| 363 | /** |
||
| 364 | * Alternative for "find" method. |
||
| 365 | * @param $name |
||
| 366 | * @return mixed|void |
||
| 367 | */ |
||
| 368 | 1 | public function get($name) |
|
| 372 | |||
| 373 | /** |
||
| 374 | * Find a specific module, if there return that, otherwise throw exception. |
||
| 375 | * |
||
| 376 | * @param $name |
||
| 377 | * |
||
| 378 | * @return Module |
||
| 379 | * |
||
| 380 | * @throws ModuleNotFoundException |
||
| 381 | */ |
||
| 382 | 63 | public function findOrFail($name) |
|
| 392 | |||
| 393 | /** |
||
| 394 | * Get all modules as laravel collection instance. |
||
| 395 | * |
||
| 396 | * @param $status |
||
| 397 | * |
||
| 398 | * @return Collection |
||
| 399 | */ |
||
| 400 | 1 | public function collections($status = 1) |
|
| 404 | |||
| 405 | /** |
||
| 406 | * Get module path for a specific module. |
||
| 407 | * |
||
| 408 | * @param $module |
||
| 409 | * |
||
| 410 | * @return string |
||
| 411 | */ |
||
| 412 | 56 | public function getModulePath($module) |
|
| 420 | |||
| 421 | /** |
||
| 422 | * Get asset path for a specific module. |
||
| 423 | * |
||
| 424 | * @param $module |
||
| 425 | * |
||
| 426 | * @return string |
||
| 427 | */ |
||
| 428 | 2 | public function assetPath($module) |
|
| 432 | |||
| 433 | /** |
||
| 434 | * Get a specific config data from a configuration file. |
||
| 435 | * |
||
| 436 | * @param $key |
||
| 437 | * |
||
| 438 | * @param null $default |
||
| 439 | * @return mixed |
||
| 440 | */ |
||
| 441 | 118 | public function config($key, $default = null) |
|
| 445 | |||
| 446 | /** |
||
| 447 | * Get storage path for module used. |
||
| 448 | * |
||
| 449 | * @return string |
||
| 450 | */ |
||
| 451 | 2 | public function getUsedStoragePath() |
|
| 465 | |||
| 466 | /** |
||
| 467 | * Set module used for cli session. |
||
| 468 | * |
||
| 469 | * @param $name |
||
| 470 | * |
||
| 471 | * @throws ModuleNotFoundException |
||
| 472 | */ |
||
| 473 | 1 | public function setUsed($name) |
|
| 479 | |||
| 480 | /** |
||
| 481 | * Get module used for cli session. |
||
| 482 | * |
||
| 483 | * @return string |
||
| 484 | */ |
||
| 485 | 1 | public function getUsedNow() |
|
| 489 | |||
| 490 | /** |
||
| 491 | * Get used now. |
||
| 492 | * |
||
| 493 | * @return string |
||
| 494 | */ |
||
| 495 | 1 | public function getUsed() |
|
| 499 | |||
| 500 | /** |
||
| 501 | * Get laravel filesystem instance. |
||
| 502 | * |
||
| 503 | * @return \Illuminate\Filesystem\Filesystem |
||
| 504 | */ |
||
| 505 | 4 | public function getFiles() |
|
| 509 | |||
| 510 | /** |
||
| 511 | * Get module assets path. |
||
| 512 | * |
||
| 513 | * @return string |
||
| 514 | */ |
||
| 515 | 2 | public function getAssetsPath() |
|
| 519 | |||
| 520 | /** |
||
| 521 | * Get asset url from a specific module. |
||
| 522 | * |
||
| 523 | * @param string $asset |
||
| 524 | * |
||
| 525 | * @return string |
||
| 526 | */ |
||
| 527 | 2 | public function asset($asset) |
|
| 540 | |||
| 541 | /** |
||
| 542 | * Determine whether the given module is activated. |
||
| 543 | * |
||
| 544 | * @param string $name |
||
| 545 | * |
||
| 546 | * @return bool |
||
| 547 | */ |
||
| 548 | 4 | public function active($name) |
|
| 552 | |||
| 553 | /** |
||
| 554 | * Determine whether the given module is not activated. |
||
| 555 | * |
||
| 556 | * @param string $name |
||
| 557 | * |
||
| 558 | * @return bool |
||
| 559 | */ |
||
| 560 | 2 | public function notActive($name) |
|
| 564 | |||
| 565 | /** |
||
| 566 | * Enabling a specific module. |
||
| 567 | * |
||
| 568 | * @param string $name |
||
| 569 | * |
||
| 570 | * @return bool |
||
| 571 | */ |
||
| 572 | 1 | public function enable($name) |
|
| 576 | |||
| 577 | /** |
||
| 578 | * Disabling a specific module. |
||
| 579 | * |
||
| 580 | * @param string $name |
||
| 581 | * |
||
| 582 | * @return bool |
||
| 583 | */ |
||
| 584 | 1 | public function disable($name) |
|
| 588 | |||
| 589 | /** |
||
| 590 | * Delete a specific module. |
||
| 591 | * |
||
| 592 | * @param string $name |
||
| 593 | * |
||
| 594 | * @return bool |
||
| 595 | */ |
||
| 596 | 2 | public function delete($name) |
|
| 600 | |||
| 601 | /** |
||
| 602 | * Update dependencies for the specified module. |
||
| 603 | * |
||
| 604 | * @param string $module |
||
| 605 | */ |
||
| 606 | public function update($module) |
||
| 610 | |||
| 611 | /** |
||
| 612 | * Install the specified module. |
||
| 613 | * |
||
| 614 | * @param string $name |
||
| 615 | * @param string $version |
||
| 616 | * @param string $type |
||
| 617 | * @param bool $subtree |
||
| 618 | * |
||
| 619 | * @return \Symfony\Component\Process\Process |
||
| 620 | */ |
||
| 621 | public function install($name, $version = 'dev-master', $type = 'composer', $subtree = false) |
||
| 627 | |||
| 628 | /** |
||
| 629 | * Get stub path. |
||
| 630 | * |
||
| 631 | * @return string |
||
| 632 | */ |
||
| 633 | 3 | public function getStubPath() |
|
| 645 | |||
| 646 | /** |
||
| 647 | * Set stub path. |
||
| 648 | * |
||
| 649 | * @param string $stubPath |
||
| 650 | * |
||
| 651 | * @return $this |
||
| 652 | */ |
||
| 653 | 1 | public function setStubPath($stubPath) |
|
| 659 | } |
||
| 660 |
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..