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 |
||
| 14 | class Repository implements RepositoryInterface, Countable |
||
| 15 | { |
||
| 16 | use Macroable; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Application instance. |
||
| 20 | * |
||
| 21 | * @var Application |
||
| 22 | */ |
||
| 23 | protected $app; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * The module path. |
||
| 27 | * |
||
| 28 | * @var string|null |
||
| 29 | */ |
||
| 30 | protected $path; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * The scanned paths. |
||
| 34 | * |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | protected $paths = []; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | protected $stubPath; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * The constructor. |
||
| 46 | * |
||
| 47 | * @param Application $app |
||
| 48 | * @param string|null $path |
||
| 49 | */ |
||
| 50 | 91 | public function __construct(Application $app, $path = null) |
|
| 55 | |||
| 56 | /** |
||
| 57 | * Add other module location. |
||
| 58 | * |
||
| 59 | * @param string $path |
||
| 60 | * |
||
| 61 | * @return $this |
||
| 62 | */ |
||
| 63 | 14 | public function addLocation($path) |
|
| 69 | |||
| 70 | /** |
||
| 71 | * Alternative method for "addPath". |
||
| 72 | * |
||
| 73 | * @param string $path |
||
| 74 | * |
||
| 75 | * @return $this |
||
| 76 | */ |
||
| 77 | 1 | public function addPath($path) |
|
| 81 | |||
| 82 | /** |
||
| 83 | * Get all additional paths. |
||
| 84 | * |
||
| 85 | * @return array |
||
| 86 | */ |
||
| 87 | 1 | public function getPaths() |
|
| 91 | |||
| 92 | /** |
||
| 93 | * Get scanned modules paths. |
||
| 94 | * |
||
| 95 | * @return array |
||
| 96 | */ |
||
| 97 | 91 | public function getScanPaths() |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Get & scan all modules. |
||
| 112 | * |
||
| 113 | * @return array |
||
| 114 | */ |
||
| 115 | 91 | public function scan() |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Get all modules. |
||
| 138 | * |
||
| 139 | * @return array |
||
| 140 | */ |
||
| 141 | 91 | public function all() |
|
| 149 | |||
| 150 | /** |
||
| 151 | * Format the cached data as array of modules. |
||
| 152 | * |
||
| 153 | * @param array $cached |
||
| 154 | * |
||
| 155 | * @return array |
||
| 156 | */ |
||
| 157 | protected function formatCached($cached) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Get cached modules. |
||
| 172 | * |
||
| 173 | * @return array |
||
| 174 | */ |
||
| 175 | public function getCached() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Get all modules as collection instance. |
||
| 184 | * |
||
| 185 | * @return Collection |
||
| 186 | */ |
||
| 187 | 1 | public function toCollection() |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Get modules by status. |
||
| 194 | * |
||
| 195 | * @param $status |
||
| 196 | * |
||
| 197 | * @return array |
||
| 198 | */ |
||
| 199 | 91 | public function getByStatus($status) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Determine whether the given module exist. |
||
| 214 | * |
||
| 215 | * @param $name |
||
| 216 | * |
||
| 217 | * @return bool |
||
| 218 | */ |
||
| 219 | 43 | public function has($name) |
|
| 223 | |||
| 224 | /** |
||
| 225 | * Get list of enabled modules. |
||
| 226 | * |
||
| 227 | * @return array |
||
| 228 | */ |
||
| 229 | 91 | public function enabled() |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Get list of disabled modules. |
||
| 236 | * |
||
| 237 | * @return array |
||
| 238 | */ |
||
| 239 | 1 | public function disabled() |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Get count from all modules. |
||
| 246 | * |
||
| 247 | * @return int |
||
| 248 | */ |
||
| 249 | 1 | public function count() |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Get all ordered modules. |
||
| 256 | * |
||
| 257 | * @param string $direction |
||
| 258 | * |
||
| 259 | * @return array |
||
| 260 | */ |
||
| 261 | 91 | public function getOrdered($direction = 'asc') |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Get a module path. |
||
| 282 | * |
||
| 283 | * @return string |
||
| 284 | */ |
||
| 285 | 91 | public function getPath() |
|
| 289 | |||
| 290 | /** |
||
| 291 | * Register the modules. |
||
| 292 | */ |
||
| 293 | 91 | public function register() |
|
| 299 | |||
| 300 | /** |
||
| 301 | * Boot the modules. |
||
| 302 | */ |
||
| 303 | 91 | public function boot() |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Find a specific module. |
||
| 312 | * @param $name |
||
| 313 | * @return mixed|void |
||
| 314 | */ |
||
| 315 | 51 | public function find($name) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Find a specific module by its alias. |
||
| 328 | * @param $alias |
||
| 329 | * @return mixed|void |
||
| 330 | */ |
||
| 331 | 2 | public function findByAlias($alias) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Find all modules that are required by a module. If the module cannot be found, throw an exception. |
||
| 344 | * |
||
| 345 | * @param $name |
||
| 346 | * @return array |
||
| 347 | * @throws ModuleNotFoundException |
||
| 348 | */ |
||
| 349 | 1 | public function findRequirements($name) |
|
| 361 | |||
| 362 | /** |
||
| 363 | * Alternative for "find" method. |
||
| 364 | * @param $name |
||
| 365 | * @return mixed|void |
||
| 366 | */ |
||
| 367 | 1 | public function get($name) |
|
| 371 | |||
| 372 | /** |
||
| 373 | * Find a specific module, if there return that, otherwise throw exception. |
||
| 374 | * |
||
| 375 | * @param $name |
||
| 376 | * |
||
| 377 | * @return Module |
||
| 378 | * |
||
| 379 | * @throws ModuleNotFoundException |
||
| 380 | */ |
||
| 381 | 50 | public function findOrFail($name) |
|
| 391 | |||
| 392 | /** |
||
| 393 | * Get all modules as laravel collection instance. |
||
| 394 | * |
||
| 395 | * @param $status |
||
| 396 | * |
||
| 397 | * @return Collection |
||
| 398 | */ |
||
| 399 | public function collections($status) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Get module path for a specific module. |
||
| 406 | * |
||
| 407 | * @param $module |
||
| 408 | * |
||
| 409 | * @return string |
||
| 410 | */ |
||
| 411 | 43 | public function getModulePath($module) |
|
| 419 | |||
| 420 | /** |
||
| 421 | * Get asset path for a specific module. |
||
| 422 | * |
||
| 423 | * @param $module |
||
| 424 | * |
||
| 425 | * @return string |
||
| 426 | */ |
||
| 427 | 2 | public function assetPath($module) |
|
| 431 | |||
| 432 | /** |
||
| 433 | * Get a specific config data from a configuration file. |
||
| 434 | * |
||
| 435 | * @param $key |
||
| 436 | * |
||
| 437 | * @param null $default |
||
| 438 | * @return mixed |
||
| 439 | */ |
||
| 440 | 91 | public function config($key, $default = null) |
|
| 444 | |||
| 445 | /** |
||
| 446 | * Get storage path for module used. |
||
| 447 | * |
||
| 448 | * @return string |
||
| 449 | */ |
||
| 450 | 2 | public function getUsedStoragePath() |
|
| 458 | |||
| 459 | /** |
||
| 460 | * Set module used for cli session. |
||
| 461 | * |
||
| 462 | * @param $name |
||
| 463 | * |
||
| 464 | * @throws ModuleNotFoundException |
||
| 465 | */ |
||
| 466 | 1 | public function setUsed($name) |
|
| 472 | |||
| 473 | /** |
||
| 474 | * Get module used for cli session. |
||
| 475 | * |
||
| 476 | * @return string |
||
| 477 | */ |
||
| 478 | 1 | public function getUsedNow() |
|
| 482 | |||
| 483 | /** |
||
| 484 | * Get used now. |
||
| 485 | * |
||
| 486 | * @return string |
||
| 487 | */ |
||
| 488 | 1 | public function getUsed() |
|
| 492 | |||
| 493 | /** |
||
| 494 | * Get laravel filesystem instance. |
||
| 495 | * |
||
| 496 | * @return \Illuminate\Filesystem\Filesystem |
||
| 497 | */ |
||
| 498 | 4 | public function getFiles() |
|
| 502 | |||
| 503 | /** |
||
| 504 | * Get module assets path. |
||
| 505 | * |
||
| 506 | * @return string |
||
| 507 | */ |
||
| 508 | 2 | public function getAssetsPath() |
|
| 512 | |||
| 513 | /** |
||
| 514 | * Get asset url from a specific module. |
||
| 515 | * |
||
| 516 | * @param string $asset |
||
| 517 | * |
||
| 518 | * @return string |
||
| 519 | */ |
||
| 520 | 1 | public function asset($asset) |
|
| 530 | |||
| 531 | /** |
||
| 532 | * Determine whether the given module is activated. |
||
| 533 | * |
||
| 534 | * @param string $name |
||
| 535 | * |
||
| 536 | * @return bool |
||
| 537 | */ |
||
| 538 | 4 | public function active($name) |
|
| 542 | |||
| 543 | /** |
||
| 544 | * Determine whether the given module is not activated. |
||
| 545 | * |
||
| 546 | * @param string $name |
||
| 547 | * |
||
| 548 | * @return bool |
||
| 549 | */ |
||
| 550 | 2 | public function notActive($name) |
|
| 554 | |||
| 555 | /** |
||
| 556 | * Enabling a specific module. |
||
| 557 | * |
||
| 558 | * @param string $name |
||
| 559 | * |
||
| 560 | * @return bool |
||
| 561 | */ |
||
| 562 | 1 | public function enable($name) |
|
| 566 | |||
| 567 | /** |
||
| 568 | * Disabling a specific module. |
||
| 569 | * |
||
| 570 | * @param string $name |
||
| 571 | * |
||
| 572 | * @return bool |
||
| 573 | */ |
||
| 574 | 1 | public function disable($name) |
|
| 578 | |||
| 579 | /** |
||
| 580 | * Delete a specific module. |
||
| 581 | * |
||
| 582 | * @param string $name |
||
| 583 | * |
||
| 584 | * @return bool |
||
| 585 | */ |
||
| 586 | 1 | public function delete($name) |
|
| 590 | |||
| 591 | /** |
||
| 592 | * Update dependencies for the specified module. |
||
| 593 | * |
||
| 594 | * @param string $module |
||
| 595 | */ |
||
| 596 | public function update($module) |
||
| 600 | |||
| 601 | /** |
||
| 602 | * Install the specified module. |
||
| 603 | * |
||
| 604 | * @param string $name |
||
| 605 | * @param string $version |
||
| 606 | * @param string $type |
||
| 607 | * @param bool $subtree |
||
| 608 | * |
||
| 609 | * @return \Symfony\Component\Process\Process |
||
| 610 | */ |
||
| 611 | public function install($name, $version = 'dev-master', $type = 'composer', $subtree = false) |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Get stub path. |
||
| 620 | * |
||
| 621 | * @return string |
||
| 622 | */ |
||
| 623 | 3 | public function getStubPath() |
|
| 635 | |||
| 636 | /** |
||
| 637 | * Set stub path. |
||
| 638 | * |
||
| 639 | * @param string $stubPath |
||
| 640 | * |
||
| 641 | * @return $this |
||
| 642 | */ |
||
| 643 | 1 | public function setStubPath($stubPath) |
|
| 649 | } |
||
| 650 |
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.