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() |
|
| 98 | { |
||
| 99 | 91 | $paths = $this->paths; |
|
| 100 | |||
| 101 | 91 | $paths[] = $this->getPath() . '/*'; |
|
| 102 | |||
| 103 | 91 | if ($this->config('scan.enabled')) { |
|
| 104 | $paths = array_merge($paths, $this->config('scan.paths')); |
||
| 105 | } |
||
| 106 | |||
| 107 | 91 | return $paths; |
|
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Get & scan all modules. |
||
| 112 | * |
||
| 113 | * @return array |
||
| 114 | */ |
||
| 115 | 91 | public function scan() |
|
| 116 | { |
||
| 117 | 91 | $paths = $this->getScanPaths(); |
|
| 118 | |||
| 119 | 91 | $modules = []; |
|
| 120 | |||
| 121 | 91 | foreach ($paths as $key => $path) { |
|
| 122 | 91 | $manifests = $this->app['files']->glob("{$path}/module.json"); |
|
| 123 | |||
| 124 | 91 | is_array($manifests) || $manifests = []; |
|
| 125 | |||
| 126 | 91 | foreach ($manifests as $manifest) { |
|
| 127 | 55 | $name = Json::make($manifest)->get('name'); |
|
| 128 | |||
| 129 | 55 | $modules[$name] = new Module($this->app, $name, dirname($manifest)); |
|
| 130 | 91 | } |
|
| 131 | 91 | } |
|
| 132 | |||
| 133 | 91 | return $modules; |
|
| 134 | } |
||
| 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) |
||
| 158 | { |
||
| 159 | $modules = []; |
||
| 160 | |||
| 161 | foreach ($cached as $name => $module) { |
||
| 162 | $path = $this->config('paths.modules') . '/' . $name; |
||
| 163 | |||
| 164 | $modules[$name] = new Module($this->app, $name, $path); |
||
| 165 | } |
||
| 166 | |||
| 167 | return $modules; |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Get cached modules. |
||
| 172 | * |
||
| 173 | * @return array |
||
| 174 | */ |
||
| 175 | public function getCached() |
||
| 176 | { |
||
| 177 | return $this->app['cache']->remember($this->config('cache.key'), $this->config('cache.lifetime'), function () { |
||
| 178 | return $this->toCollection()->toArray(); |
||
| 179 | }); |
||
| 180 | } |
||
| 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) |
|
| 200 | { |
||
| 201 | 91 | $modules = []; |
|
| 202 | |||
| 203 | 91 | foreach ($this->all() as $name => $module) { |
|
| 204 | 3 | if ($module->isStatus($status)) { |
|
| 205 | 2 | $modules[$name] = $module; |
|
| 206 | 2 | } |
|
| 207 | 91 | } |
|
| 208 | |||
| 209 | 91 | return $modules; |
|
| 210 | } |
||
| 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') |
|
| 262 | { |
||
| 263 | 91 | $modules = $this->enabled(); |
|
| 264 | |||
| 265 | 91 | uasort($modules, function (Module $a, Module $b) use ($direction) { |
|
| 266 | if ($a->order == $b->order) { |
||
|
|
|||
| 267 | return 0; |
||
| 268 | } |
||
| 269 | |||
| 270 | if ($direction == 'desc') { |
||
| 271 | return $a->order < $b->order ? 1 : -1; |
||
| 272 | } |
||
| 273 | |||
| 274 | return $a->order > $b->order ? 1 : -1; |
||
| 275 | 91 | }); |
|
| 276 | |||
| 277 | 91 | return $modules; |
|
| 278 | } |
||
| 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() |
|
| 294 | { |
||
| 295 | 91 | foreach ($this->getOrdered() as $module) { |
|
| 296 | $module->register(); |
||
| 297 | 91 | } |
|
| 298 | 91 | } |
|
| 299 | |||
| 300 | /** |
||
| 301 | * Boot the modules. |
||
| 302 | */ |
||
| 303 | 91 | public function boot() |
|
| 304 | { |
||
| 305 | 91 | foreach ($this->getOrdered() as $module) { |
|
| 306 | $module->boot(); |
||
| 307 | 91 | } |
|
| 308 | 91 | } |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Find a specific module. |
||
| 312 | * @param $name |
||
| 313 | * @return mixed|void |
||
| 314 | */ |
||
| 315 | 51 | public function find($name) |
|
| 316 | { |
||
| 317 | 51 | foreach ($this->all() as $module) { |
|
| 318 | 50 | if ($module->getLowerName() === strtolower($name)) { |
|
| 319 | 50 | return $module; |
|
| 320 | } |
||
| 321 | 44 | } |
|
| 322 | |||
| 323 | 44 | return; |
|
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Find a specific module by its alias. |
||
| 328 | * @param $alias |
||
| 329 | * @return mixed|void |
||
| 330 | */ |
||
| 331 | 2 | public function findByAlias($alias) |
|
| 332 | { |
||
| 333 | 2 | foreach ($this->all() as $module) { |
|
| 334 | 2 | if ($module->getAlias() === $alias) { |
|
| 335 | 2 | return $module; |
|
| 336 | } |
||
| 337 | 2 | } |
|
| 338 | |||
| 339 | return; |
||
| 340 | } |
||
| 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 | 2 | public function findRequirements($name) |
|
| 350 | { |
||
| 351 | 1 | $requirements = []; |
|
| 352 | |||
| 353 | 1 | $module = $this->findOrFail($name); |
|
| 354 | |||
| 355 | 2 | foreach ($module->getRequires() as $requirementName) { |
|
| 356 | 1 | $requirements[] = $this->findByAlias($requirementName); |
|
| 357 | 1 | } |
|
| 358 | |||
| 359 | 2 | return $requirements; |
|
| 360 | } |
||
| 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 | * @return Collection |
||
| 396 | */ |
||
| 397 | 1 | public function collections() |
|
| 401 | |||
| 402 | /** |
||
| 403 | * Get module path for a specific module. |
||
| 404 | * |
||
| 405 | * @param $module |
||
| 406 | * |
||
| 407 | * @return string |
||
| 408 | */ |
||
| 409 | 43 | public function getModulePath($module) |
|
| 417 | |||
| 418 | /** |
||
| 419 | * Get asset path for a specific module. |
||
| 420 | * |
||
| 421 | * @param $module |
||
| 422 | * |
||
| 423 | * @return string |
||
| 424 | */ |
||
| 425 | 2 | public function assetPath($module) |
|
| 429 | |||
| 430 | /** |
||
| 431 | * Get a specific config data from a configuration file. |
||
| 432 | * |
||
| 433 | * @param $key |
||
| 434 | * |
||
| 435 | * @param null $default |
||
| 436 | * @return mixed |
||
| 437 | */ |
||
| 438 | 91 | public function config($key, $default = null) |
|
| 442 | |||
| 443 | /** |
||
| 444 | * Get storage path for module used. |
||
| 445 | * |
||
| 446 | * @return string |
||
| 447 | */ |
||
| 448 | 2 | public function getUsedStoragePath() |
|
| 449 | { |
||
| 450 | 2 | if (!$this->app['files']->exists($path = storage_path('app/modules'))) { |
|
| 451 | 1 | $this->app['files']->makeDirectory($path, 0777, true); |
|
| 452 | 1 | } |
|
| 453 | |||
| 454 | 2 | return $path . '/modules.used'; |
|
| 455 | } |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Set module used for cli session. |
||
| 459 | * |
||
| 460 | * @param $name |
||
| 461 | * |
||
| 462 | * @throws ModuleNotFoundException |
||
| 463 | */ |
||
| 464 | 1 | public function setUsed($name) |
|
| 470 | |||
| 471 | /** |
||
| 472 | * Get module used for cli session. |
||
| 473 | * |
||
| 474 | * @return string |
||
| 475 | */ |
||
| 476 | 1 | public function getUsedNow() |
|
| 480 | |||
| 481 | /** |
||
| 482 | * Get used now. |
||
| 483 | * |
||
| 484 | * @return string |
||
| 485 | */ |
||
| 486 | 1 | public function getUsed() |
|
| 490 | |||
| 491 | /** |
||
| 492 | * Get laravel filesystem instance. |
||
| 493 | * |
||
| 494 | * @return \Illuminate\Filesystem\Filesystem |
||
| 495 | */ |
||
| 496 | 4 | public function getFiles() |
|
| 500 | |||
| 501 | /** |
||
| 502 | * Get module assets path. |
||
| 503 | * |
||
| 504 | * @return string |
||
| 505 | */ |
||
| 506 | 2 | public function getAssetsPath() |
|
| 510 | |||
| 511 | /** |
||
| 512 | * Get asset url from a specific module. |
||
| 513 | * |
||
| 514 | * @param string $asset |
||
| 515 | * |
||
| 516 | * @return string |
||
| 517 | */ |
||
| 518 | 1 | public function asset($asset) |
|
| 528 | |||
| 529 | /** |
||
| 530 | * Determine whether the given module is activated. |
||
| 531 | * |
||
| 532 | * @param string $name |
||
| 533 | * |
||
| 534 | * @return bool |
||
| 535 | */ |
||
| 536 | 4 | public function active($name) |
|
| 540 | |||
| 541 | /** |
||
| 542 | * Determine whether the given module is not activated. |
||
| 543 | * |
||
| 544 | * @param string $name |
||
| 545 | * |
||
| 546 | * @return bool |
||
| 547 | */ |
||
| 548 | 2 | public function notActive($name) |
|
| 552 | |||
| 553 | /** |
||
| 554 | * Enabling a specific module. |
||
| 555 | * |
||
| 556 | * @param string $name |
||
| 557 | * |
||
| 558 | * @return bool |
||
| 559 | */ |
||
| 560 | 1 | public function enable($name) |
|
| 564 | |||
| 565 | /** |
||
| 566 | * Disabling a specific module. |
||
| 567 | * |
||
| 568 | * @param string $name |
||
| 569 | * |
||
| 570 | * @return bool |
||
| 571 | */ |
||
| 572 | 1 | public function disable($name) |
|
| 576 | |||
| 577 | /** |
||
| 578 | * Delete a specific module. |
||
| 579 | * |
||
| 580 | * @param string $name |
||
| 581 | * |
||
| 582 | * @return bool |
||
| 583 | */ |
||
| 584 | 1 | public function delete($name) |
|
| 588 | |||
| 589 | /** |
||
| 590 | * Update dependencies for the specified module. |
||
| 591 | * |
||
| 592 | * @param string $module |
||
| 593 | */ |
||
| 594 | public function update($module) |
||
| 598 | |||
| 599 | /** |
||
| 600 | * Install the specified module. |
||
| 601 | * |
||
| 602 | * @param string $name |
||
| 603 | * @param string $version |
||
| 604 | * @param string $type |
||
| 605 | * @param bool $subtree |
||
| 606 | * |
||
| 607 | * @return \Symfony\Component\Process\Process |
||
| 608 | */ |
||
| 609 | public function install($name, $version = 'dev-master', $type = 'composer', $subtree = false) |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Get stub path. |
||
| 618 | * |
||
| 619 | * @return string |
||
| 620 | */ |
||
| 621 | 3 | public function getStubPath() |
|
| 633 | |||
| 634 | /** |
||
| 635 | * Set stub path. |
||
| 636 | * |
||
| 637 | * @param string $stubPath |
||
| 638 | * |
||
| 639 | * @return $this |
||
| 640 | */ |
||
| 641 | 1 | public function setStubPath($stubPath) |
|
| 647 | } |
||
| 648 |
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.