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 |
||
| 13 | class Repository implements RepositoryInterface, Countable |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * Application instance. |
||
| 17 | * |
||
| 18 | * @var Application |
||
| 19 | */ |
||
| 20 | protected $app; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * The module path. |
||
| 24 | * |
||
| 25 | * @var string|null |
||
| 26 | */ |
||
| 27 | protected $path; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The scanned paths. |
||
| 31 | * |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | protected $paths = []; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | protected $stubPath; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The constructor. |
||
| 43 | * |
||
| 44 | * @param Application $app |
||
| 45 | * @param string|null $path |
||
| 46 | */ |
||
| 47 | 63 | public function __construct(Application $app, $path = null) |
|
| 52 | |||
| 53 | /** |
||
| 54 | * Add other module location. |
||
| 55 | * |
||
| 56 | * @param string $path |
||
| 57 | * |
||
| 58 | * @return $this |
||
| 59 | */ |
||
| 60 | 12 | public function addLocation($path) |
|
| 66 | |||
| 67 | /** |
||
| 68 | * Alternative method for "addPath". |
||
| 69 | * |
||
| 70 | * @param string $path |
||
| 71 | * |
||
| 72 | * @return $this |
||
| 73 | */ |
||
| 74 | 1 | public function addPath($path) |
|
| 78 | |||
| 79 | /** |
||
| 80 | * Get all additional paths. |
||
| 81 | * |
||
| 82 | * @return array |
||
| 83 | */ |
||
| 84 | 1 | public function getPaths() |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Get scanned modules paths. |
||
| 91 | * |
||
| 92 | * @return array |
||
| 93 | */ |
||
| 94 | 63 | public function getScanPaths() |
|
| 95 | { |
||
| 96 | 63 | $paths = $this->paths; |
|
| 97 | |||
| 98 | 63 | $paths[] = $this->getPath().'/*'; |
|
| 99 | |||
| 100 | 63 | if ($this->config('scan.enabled')) { |
|
| 101 | $paths = array_merge($paths, $this->config('scan.paths')); |
||
| 102 | } |
||
| 103 | |||
| 104 | 63 | return $paths; |
|
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Get & scan all modules. |
||
| 109 | * |
||
| 110 | * @return array |
||
| 111 | */ |
||
| 112 | 63 | public function scan() |
|
| 113 | { |
||
| 114 | 63 | $paths = $this->getScanPaths(); |
|
| 115 | |||
| 116 | 63 | $modules = []; |
|
| 117 | |||
| 118 | 63 | foreach ($paths as $key => $path) { |
|
| 119 | 63 | $manifests = $this->app['files']->glob("{$path}/module.json"); |
|
| 120 | |||
| 121 | 63 | is_array($manifests) || $manifests = []; |
|
| 122 | |||
| 123 | 63 | foreach ($manifests as $manifest) { |
|
| 124 | 31 | $name = Json::make($manifest)->get('name'); |
|
| 125 | |||
| 126 | 31 | $lowerName = strtolower($name); |
|
| 127 | |||
| 128 | 63 | $modules[$name] = new Module($this->app, $lowerName, dirname($manifest)); |
|
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 132 | 63 | return $modules; |
|
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Get all modules. |
||
| 137 | * |
||
| 138 | * @return array |
||
| 139 | */ |
||
| 140 | 63 | public function all() |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Format the cached data as array of modules. |
||
| 151 | * |
||
| 152 | * @param array $cached |
||
| 153 | * |
||
| 154 | * @return array |
||
| 155 | */ |
||
| 156 | protected function formatCached($cached) |
||
| 157 | { |
||
| 158 | $modules = []; |
||
| 159 | |||
| 160 | foreach ($cached as $name => $module) { |
||
| 161 | $path = $this->config('paths.modules').'/'.$name; |
||
| 162 | |||
| 163 | $lowerName = strtolower($name); |
||
| 164 | |||
| 165 | $modules[$name] = new Module($this->app, $lowerName, $path); |
||
| 166 | } |
||
| 167 | |||
| 168 | return $modules; |
||
| 169 | } |
||
| 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 | 63 | public function getByStatus($status) |
|
| 201 | { |
||
| 202 | 63 | $modules = []; |
|
| 203 | |||
| 204 | 63 | foreach ($this->all() as $name => $module) { |
|
| 205 | 3 | if ($module->isStatus($status)) { |
|
| 206 | 3 | $modules[$name] = $module; |
|
| 207 | } |
||
| 208 | } |
||
| 209 | |||
| 210 | 63 | return $modules; |
|
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Determine whether the given module exist. |
||
| 215 | * |
||
| 216 | * @param $name |
||
| 217 | * |
||
| 218 | * @return bool |
||
| 219 | */ |
||
| 220 | 21 | public function has($name) |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Get list of enabled modules. |
||
| 227 | * |
||
| 228 | * @return array |
||
| 229 | */ |
||
| 230 | 63 | 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 | 63 | public function getOrdered($direction = 'asc') |
|
| 263 | { |
||
| 264 | 63 | $modules = $this->enabled(); |
|
| 265 | |||
| 266 | 63 | 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 | 63 | }); |
|
| 277 | |||
| 278 | 63 | return $modules; |
|
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Get a module path. |
||
| 283 | * |
||
| 284 | * @return string |
||
| 285 | */ |
||
| 286 | 63 | public function getPath() |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Register the modules. |
||
| 293 | */ |
||
| 294 | 63 | public function register() |
|
| 295 | { |
||
| 296 | 63 | foreach ($this->getOrdered() as $module) { |
|
| 297 | $module->register(); |
||
| 298 | } |
||
| 299 | 63 | } |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Boot the modules. |
||
| 303 | */ |
||
| 304 | 63 | public function boot() |
|
| 305 | { |
||
| 306 | 63 | foreach ($this->getOrdered() as $module) { |
|
| 307 | $module->boot(); |
||
| 308 | } |
||
| 309 | 63 | } |
|
| 310 | |||
| 311 | /** |
||
| 312 | * Find a specific module. |
||
| 313 | * @param $name |
||
| 314 | * @return mixed|void |
||
| 315 | */ |
||
| 316 | 28 | public function find($name) |
|
| 317 | { |
||
| 318 | 28 | foreach ($this->all() as $module) { |
|
| 319 | 27 | if ($module->getLowerName() === strtolower($name)) { |
|
| 320 | 27 | return $module; |
|
| 321 | } |
||
| 322 | } |
||
| 323 | |||
| 324 | 22 | return; |
|
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Alternative for "find" method. |
||
| 329 | * @param $name |
||
| 330 | * @return mixed|void |
||
| 331 | */ |
||
| 332 | 1 | public function get($name) |
|
| 336 | |||
| 337 | /** |
||
| 338 | * Find a specific module, if there return that, otherwise throw exception. |
||
| 339 | * |
||
| 340 | * @param $name |
||
| 341 | * |
||
| 342 | * @return Module |
||
| 343 | * |
||
| 344 | * @throws ModuleNotFoundException |
||
| 345 | */ |
||
| 346 | 27 | public function findOrFail($name) |
|
| 356 | |||
| 357 | /** |
||
| 358 | * Get all modules as laravel collection instance. |
||
| 359 | * |
||
| 360 | * @return Collection |
||
| 361 | */ |
||
| 362 | 1 | public function collections() |
|
| 366 | |||
| 367 | /** |
||
| 368 | * Get module path for a specific module. |
||
| 369 | * |
||
| 370 | * @param $module |
||
| 371 | * |
||
| 372 | * @return string |
||
| 373 | */ |
||
| 374 | 21 | public function getModulePath($module) |
|
| 382 | |||
| 383 | /** |
||
| 384 | * Get asset path for a specific module. |
||
| 385 | * |
||
| 386 | * @param $module |
||
| 387 | * |
||
| 388 | * @return string |
||
| 389 | */ |
||
| 390 | 2 | public function assetPath($module) |
|
| 394 | |||
| 395 | /** |
||
| 396 | * Get a specific config data from a configuration file. |
||
| 397 | * |
||
| 398 | * @param $key |
||
| 399 | * |
||
| 400 | * @return mixed |
||
| 401 | */ |
||
| 402 | 63 | public function config($key) |
|
| 406 | |||
| 407 | /** |
||
| 408 | * Get storage path for module used. |
||
| 409 | * |
||
| 410 | * @return string |
||
| 411 | */ |
||
| 412 | 2 | public function getUsedStoragePath() |
|
| 420 | |||
| 421 | /** |
||
| 422 | * Set module used for cli session. |
||
| 423 | * |
||
| 424 | * @param $name |
||
| 425 | * |
||
| 426 | * @throws ModuleNotFoundException |
||
| 427 | */ |
||
| 428 | 1 | public function setUsed($name) |
|
| 434 | |||
| 435 | /** |
||
| 436 | * Get module used for cli session. |
||
| 437 | * |
||
| 438 | * @return string |
||
| 439 | */ |
||
| 440 | 1 | public function getUsedNow() |
|
| 444 | |||
| 445 | /** |
||
| 446 | * Get used now. |
||
| 447 | * |
||
| 448 | * @return string |
||
| 449 | */ |
||
| 450 | 1 | public function getUsed() |
|
| 454 | |||
| 455 | /** |
||
| 456 | * Get laravel filesystem instance. |
||
| 457 | * |
||
| 458 | * @return \Illuminate\Filesystem\Filesystem |
||
| 459 | */ |
||
| 460 | 4 | public function getFiles() |
|
| 464 | |||
| 465 | /** |
||
| 466 | * Get module assets path. |
||
| 467 | * |
||
| 468 | * @return string |
||
| 469 | */ |
||
| 470 | 2 | public function getAssetsPath() |
|
| 474 | |||
| 475 | /** |
||
| 476 | * Get asset url from a specific module. |
||
| 477 | * |
||
| 478 | * @param string $asset |
||
| 479 | * |
||
| 480 | * @return string |
||
| 481 | */ |
||
| 482 | 1 | public function asset($asset) |
|
| 492 | |||
| 493 | /** |
||
| 494 | * Determine whether the given module is activated. |
||
| 495 | * |
||
| 496 | * @param string $name |
||
| 497 | * |
||
| 498 | * @return bool |
||
| 499 | */ |
||
| 500 | 4 | public function active($name) |
|
| 504 | |||
| 505 | /** |
||
| 506 | * Determine whether the given module is not activated. |
||
| 507 | * |
||
| 508 | * @param string $name |
||
| 509 | * |
||
| 510 | * @return bool |
||
| 511 | */ |
||
| 512 | 2 | public function notActive($name) |
|
| 516 | |||
| 517 | /** |
||
| 518 | * Enabling a specific module. |
||
| 519 | * |
||
| 520 | * @param string $name |
||
| 521 | * |
||
| 522 | * @return bool |
||
| 523 | */ |
||
| 524 | 1 | public function enable($name) |
|
| 528 | |||
| 529 | /** |
||
| 530 | * Disabling a specific module. |
||
| 531 | * |
||
| 532 | * @param string $name |
||
| 533 | * |
||
| 534 | * @return bool |
||
| 535 | */ |
||
| 536 | 1 | public function disable($name) |
|
| 540 | |||
| 541 | /** |
||
| 542 | * Delete a specific module. |
||
| 543 | * |
||
| 544 | * @param string $name |
||
| 545 | * |
||
| 546 | * @return bool |
||
| 547 | */ |
||
| 548 | 1 | public function delete($name) |
|
| 552 | |||
| 553 | /** |
||
| 554 | * Update dependencies for the specified module. |
||
| 555 | * |
||
| 556 | * @param string $module |
||
| 557 | */ |
||
| 558 | public function update($module) |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Install the specified module. |
||
| 565 | * |
||
| 566 | * @param string $name |
||
| 567 | * @param string $version |
||
| 568 | * @param string $type |
||
| 569 | * @param bool $subtree |
||
| 570 | * |
||
| 571 | * @return \Symfony\Component\Process\Process |
||
| 572 | */ |
||
| 573 | public function install($name, $version = 'dev-master', $type = 'composer', $subtree = false) |
||
| 579 | |||
| 580 | /** |
||
| 581 | * Get stub path. |
||
| 582 | * |
||
| 583 | * @return string |
||
| 584 | */ |
||
| 585 | 3 | public function getStubPath() |
|
| 597 | |||
| 598 | /** |
||
| 599 | * Set stub path. |
||
| 600 | * |
||
| 601 | * @param string $stubPath |
||
| 602 | * |
||
| 603 | * @return $this |
||
| 604 | */ |
||
| 605 | 1 | public function setStubPath($stubPath) |
|
| 611 | } |
||
| 612 |
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.