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 | 69 | 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 | 69 | public function getScanPaths() |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Get & scan all modules. |
||
| 109 | * |
||
| 110 | * @return array |
||
| 111 | */ |
||
| 112 | 69 | public function scan() |
|
| 113 | { |
||
| 114 | 69 | $paths = $this->getScanPaths(); |
|
| 115 | |||
| 116 | 69 | $modules = []; |
|
| 117 | |||
| 118 | 69 | foreach ($paths as $key => $path) { |
|
| 119 | 69 | $manifests = $this->app['files']->glob("{$path}/module.json"); |
|
| 120 | |||
| 121 | 69 | is_array($manifests) || $manifests = []; |
|
| 122 | |||
| 123 | 69 | foreach ($manifests as $manifest) { |
|
| 124 | 37 | $name = Json::make($manifest)->get('name'); |
|
| 125 | |||
| 126 | 37 | $modules[$name] = new Module($this->app, $name, dirname($manifest)); |
|
| 127 | 69 | } |
|
| 128 | 69 | } |
|
| 129 | |||
| 130 | 69 | return $modules; |
|
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Get all modules. |
||
| 135 | * |
||
| 136 | * @return array |
||
| 137 | */ |
||
| 138 | 69 | public function all() |
|
| 146 | |||
| 147 | /** |
||
| 148 | * Format the cached data as array of modules. |
||
| 149 | * |
||
| 150 | * @param array $cached |
||
| 151 | * |
||
| 152 | * @return array |
||
| 153 | */ |
||
| 154 | protected function formatCached($cached) |
||
| 155 | { |
||
| 156 | $modules = []; |
||
| 157 | |||
| 158 | foreach ($cached as $name => $module) { |
||
| 159 | $path = $this->config('paths.modules') . '/' . $name; |
||
| 160 | |||
| 161 | $modules[$name] = new Module($this->app, $name, $path); |
||
| 162 | } |
||
| 163 | |||
| 164 | return $modules; |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Get cached modules. |
||
| 169 | * |
||
| 170 | * @return array |
||
| 171 | */ |
||
| 172 | public function getCached() |
||
| 173 | { |
||
| 174 | return $this->app['cache']->remember($this->config('cache.key'), $this->config('cache.lifetime'), function () { |
||
| 175 | return $this->toCollection()->toArray(); |
||
| 176 | }); |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Get all modules as collection instance. |
||
| 181 | * |
||
| 182 | * @return Collection |
||
| 183 | */ |
||
| 184 | 1 | public function toCollection() |
|
| 188 | |||
| 189 | /** |
||
| 190 | * Get modules by status. |
||
| 191 | * |
||
| 192 | * @param $status |
||
| 193 | * |
||
| 194 | * @return array |
||
| 195 | */ |
||
| 196 | 69 | public function getByStatus($status) |
|
| 197 | { |
||
| 198 | 69 | $modules = []; |
|
| 199 | |||
| 200 | 69 | foreach ($this->all() as $name => $module) { |
|
| 201 | 3 | if ($module->isStatus($status)) { |
|
| 202 | 2 | $modules[$name] = $module; |
|
| 203 | 2 | } |
|
| 204 | 69 | } |
|
| 205 | |||
| 206 | 69 | return $modules; |
|
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Determine whether the given module exist. |
||
| 211 | * |
||
| 212 | * @param $name |
||
| 213 | * |
||
| 214 | * @return bool |
||
| 215 | */ |
||
| 216 | 27 | public function has($name) |
|
| 220 | |||
| 221 | /** |
||
| 222 | * Get list of enabled modules. |
||
| 223 | * |
||
| 224 | * @return array |
||
| 225 | */ |
||
| 226 | 69 | public function enabled() |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Get list of disabled modules. |
||
| 233 | * |
||
| 234 | * @return array |
||
| 235 | */ |
||
| 236 | 1 | public function disabled() |
|
| 240 | |||
| 241 | /** |
||
| 242 | * Get count from all modules. |
||
| 243 | * |
||
| 244 | * @return int |
||
| 245 | */ |
||
| 246 | 1 | public function count() |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Get all ordered modules. |
||
| 253 | * |
||
| 254 | * @param string $direction |
||
| 255 | * |
||
| 256 | * @return array |
||
| 257 | */ |
||
| 258 | 69 | public function getOrdered($direction = 'asc') |
|
| 259 | { |
||
| 260 | 69 | $modules = $this->enabled(); |
|
| 261 | |||
| 262 | 69 | uasort($modules, function (Module $a, Module $b) use ($direction) { |
|
| 263 | if ($a->order == $b->order) { |
||
|
|
|||
| 264 | return 0; |
||
| 265 | } |
||
| 266 | |||
| 267 | if ($direction == 'desc') { |
||
| 268 | return $a->order < $b->order ? 1 : -1; |
||
| 269 | } |
||
| 270 | |||
| 271 | return $a->order > $b->order ? 1 : -1; |
||
| 272 | 69 | }); |
|
| 273 | |||
| 274 | 69 | return $modules; |
|
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Get a module path. |
||
| 279 | * |
||
| 280 | * @return string |
||
| 281 | */ |
||
| 282 | 69 | public function getPath() |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Register the modules. |
||
| 289 | */ |
||
| 290 | 69 | public function register() |
|
| 291 | { |
||
| 292 | 69 | foreach ($this->getOrdered() as $module) { |
|
| 293 | $module->register(); |
||
| 294 | 69 | } |
|
| 295 | 69 | } |
|
| 296 | |||
| 297 | /** |
||
| 298 | * Boot the modules. |
||
| 299 | */ |
||
| 300 | 69 | public function boot() |
|
| 301 | { |
||
| 302 | 69 | foreach ($this->getOrdered() as $module) { |
|
| 303 | $module->boot(); |
||
| 304 | 69 | } |
|
| 305 | 69 | } |
|
| 306 | |||
| 307 | /** |
||
| 308 | * Find a specific module. |
||
| 309 | * @param $name |
||
| 310 | * @return mixed|void |
||
| 311 | */ |
||
| 312 | 34 | public function find($name) |
|
| 313 | { |
||
| 314 | 34 | foreach ($this->all() as $module) { |
|
| 315 | 33 | if ($module->getLowerName() === strtolower($name)) { |
|
| 316 | 33 | return $module; |
|
| 317 | } |
||
| 318 | 28 | } |
|
| 319 | |||
| 320 | 28 | return; |
|
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Alternative for "find" method. |
||
| 325 | * @param $name |
||
| 326 | * @return mixed|void |
||
| 327 | */ |
||
| 328 | 1 | public function get($name) |
|
| 332 | |||
| 333 | /** |
||
| 334 | * Find a specific module, if there return that, otherwise throw exception. |
||
| 335 | * |
||
| 336 | * @param $name |
||
| 337 | * |
||
| 338 | * @return Module |
||
| 339 | * |
||
| 340 | * @throws ModuleNotFoundException |
||
| 341 | */ |
||
| 342 | 33 | public function findOrFail($name) |
|
| 352 | |||
| 353 | /** |
||
| 354 | * Get all modules as laravel collection instance. |
||
| 355 | * |
||
| 356 | * @return Collection |
||
| 357 | */ |
||
| 358 | 1 | public function collections() |
|
| 359 | { |
||
| 360 | 1 | return new Collection($this->enabled()); |
|
| 361 | 1 | } |
|
| 362 | |||
| 363 | /** |
||
| 364 | * Get module path for a specific module. |
||
| 365 | * |
||
| 366 | * @param $module |
||
| 367 | * |
||
| 368 | * @return string |
||
| 369 | */ |
||
| 370 | 27 | public function getModulePath($module) |
|
| 378 | |||
| 379 | /** |
||
| 380 | * Get asset path for a specific module. |
||
| 381 | * |
||
| 382 | * @param $module |
||
| 383 | * |
||
| 384 | * @return string |
||
| 385 | */ |
||
| 386 | 2 | public function assetPath($module) |
|
| 390 | |||
| 391 | /** |
||
| 392 | * Get a specific config data from a configuration file. |
||
| 393 | * |
||
| 394 | * @param $key |
||
| 395 | * |
||
| 396 | * @return mixed |
||
| 397 | */ |
||
| 398 | 69 | public function config($key) |
|
| 402 | |||
| 403 | /** |
||
| 404 | * Get storage path for module used. |
||
| 405 | * |
||
| 406 | * @return string |
||
| 407 | */ |
||
| 408 | 2 | public function getUsedStoragePath() |
|
| 409 | { |
||
| 410 | 2 | if (!$this->app['files']->exists($path = storage_path('app/modules'))) { |
|
| 411 | 1 | $this->app['files']->makeDirectory($path, 0777, true); |
|
| 412 | 1 | } |
|
| 413 | |||
| 414 | 2 | return $path . '/modules.used'; |
|
| 415 | } |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Set module used for cli session. |
||
| 419 | * |
||
| 420 | * @param $name |
||
| 421 | * |
||
| 422 | * @throws ModuleNotFoundException |
||
| 423 | */ |
||
| 424 | 1 | public function setUsed($name) |
|
| 430 | |||
| 431 | /** |
||
| 432 | * Get module used for cli session. |
||
| 433 | * |
||
| 434 | * @return string |
||
| 435 | */ |
||
| 436 | 1 | public function getUsedNow() |
|
| 440 | |||
| 441 | /** |
||
| 442 | * Get used now. |
||
| 443 | * |
||
| 444 | * @return string |
||
| 445 | */ |
||
| 446 | 1 | public function getUsed() |
|
| 450 | |||
| 451 | /** |
||
| 452 | * Get laravel filesystem instance. |
||
| 453 | * |
||
| 454 | * @return \Illuminate\Filesystem\Filesystem |
||
| 455 | */ |
||
| 456 | 4 | public function getFiles() |
|
| 460 | |||
| 461 | /** |
||
| 462 | * Get module assets path. |
||
| 463 | * |
||
| 464 | * @return string |
||
| 465 | */ |
||
| 466 | 2 | public function getAssetsPath() |
|
| 470 | |||
| 471 | /** |
||
| 472 | * Get asset url from a specific module. |
||
| 473 | * |
||
| 474 | * @param string $asset |
||
| 475 | * |
||
| 476 | * @return string |
||
| 477 | */ |
||
| 478 | 1 | public function asset($asset) |
|
| 488 | |||
| 489 | /** |
||
| 490 | * Determine whether the given module is activated. |
||
| 491 | * |
||
| 492 | * @param string $name |
||
| 493 | * |
||
| 494 | * @return bool |
||
| 495 | */ |
||
| 496 | 4 | public function active($name) |
|
| 500 | |||
| 501 | /** |
||
| 502 | * Determine whether the given module is not activated. |
||
| 503 | * |
||
| 504 | * @param string $name |
||
| 505 | * |
||
| 506 | * @return bool |
||
| 507 | */ |
||
| 508 | 2 | public function notActive($name) |
|
| 512 | |||
| 513 | /** |
||
| 514 | * Enabling a specific module. |
||
| 515 | * |
||
| 516 | * @param string $name |
||
| 517 | * |
||
| 518 | * @return bool |
||
| 519 | */ |
||
| 520 | 1 | public function enable($name) |
|
| 524 | |||
| 525 | /** |
||
| 526 | * Disabling a specific module. |
||
| 527 | * |
||
| 528 | * @param string $name |
||
| 529 | * |
||
| 530 | * @return bool |
||
| 531 | */ |
||
| 532 | 1 | public function disable($name) |
|
| 536 | |||
| 537 | /** |
||
| 538 | * Delete a specific module. |
||
| 539 | * |
||
| 540 | * @param string $name |
||
| 541 | * |
||
| 542 | * @return bool |
||
| 543 | */ |
||
| 544 | 1 | public function delete($name) |
|
| 548 | |||
| 549 | /** |
||
| 550 | * Update dependencies for the specified module. |
||
| 551 | * |
||
| 552 | * @param string $module |
||
| 553 | */ |
||
| 554 | public function update($module) |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Install the specified module. |
||
| 561 | * |
||
| 562 | * @param string $name |
||
| 563 | * @param string $version |
||
| 564 | * @param string $type |
||
| 565 | * @param bool $subtree |
||
| 566 | * |
||
| 567 | * @return \Symfony\Component\Process\Process |
||
| 568 | */ |
||
| 569 | public function install($name, $version = 'dev-master', $type = 'composer', $subtree = false) |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Get stub path. |
||
| 578 | * |
||
| 579 | * @return string |
||
| 580 | */ |
||
| 581 | 3 | public function getStubPath() |
|
| 593 | |||
| 594 | /** |
||
| 595 | * Set stub path. |
||
| 596 | * |
||
| 597 | * @param string $stubPath |
||
| 598 | * |
||
| 599 | * @return $this |
||
| 600 | */ |
||
| 601 | 1 | public function setStubPath($stubPath) |
|
| 607 | } |
||
| 608 |
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.