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 component 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 | public function __construct(Application $app, $path = null) |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Add other component location. |
||
| 55 | * |
||
| 56 | * @param string $path |
||
| 57 | * |
||
| 58 | * @return $this |
||
| 59 | */ |
||
| 60 | public function addLocation($path) |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Alternative method for "addPath". |
||
| 69 | * |
||
| 70 | * @param string $path |
||
| 71 | * |
||
| 72 | * @return $this |
||
| 73 | */ |
||
| 74 | public function addPath($path) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Get all additional paths. |
||
| 81 | * |
||
| 82 | * @return array |
||
| 83 | */ |
||
| 84 | public function getPaths() |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Get scanned components paths. |
||
| 91 | * |
||
| 92 | * @return array |
||
| 93 | */ |
||
| 94 | public function getScanPaths() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Get & scan all components. |
||
| 109 | * |
||
| 110 | * @return array |
||
| 111 | */ |
||
| 112 | public function scan() |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Get all components. |
||
| 135 | * |
||
| 136 | * @return array |
||
| 137 | */ |
||
| 138 | public function all() |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Format the cached data as array of components. |
||
| 149 | * |
||
| 150 | * @param array $cached |
||
| 151 | * |
||
| 152 | * @return array |
||
| 153 | */ |
||
| 154 | protected function formatCached($cached) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Get cached components. |
||
| 169 | * |
||
| 170 | * @return array |
||
| 171 | */ |
||
| 172 | public function getCached() |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Get all components as collection instance. |
||
| 181 | * |
||
| 182 | * @return Collection |
||
| 183 | */ |
||
| 184 | public function toCollection() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Get components by status. |
||
| 191 | * |
||
| 192 | * @param $status |
||
| 193 | * |
||
| 194 | * @return array |
||
| 195 | */ |
||
| 196 | public function getByStatus($status) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Determine whether the given component exist. |
||
| 211 | * |
||
| 212 | * @param $name |
||
| 213 | * |
||
| 214 | * @return bool |
||
| 215 | */ |
||
| 216 | public function has($name) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Get list of enabled components. |
||
| 223 | * |
||
| 224 | * @return array |
||
| 225 | */ |
||
| 226 | public function enabled() |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Get list of disabled components. |
||
| 233 | * |
||
| 234 | * @return array |
||
| 235 | */ |
||
| 236 | public function disabled() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Get count from all components. |
||
| 243 | * |
||
| 244 | * @return int |
||
| 245 | */ |
||
| 246 | public function count() |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Get all ordered components. |
||
| 253 | * |
||
| 254 | * @param string $direction |
||
| 255 | * |
||
| 256 | * @return array |
||
| 257 | */ |
||
| 258 | public function getOrdered($direction = 'asc') |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Get a component path. |
||
| 279 | * |
||
| 280 | * @return string |
||
| 281 | */ |
||
| 282 | public function getPath() |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Register the components. |
||
| 289 | */ |
||
| 290 | public function register() |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Boot the components. |
||
| 299 | */ |
||
| 300 | public function boot() |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Find a specific component. |
||
| 309 | * |
||
| 310 | * @param $name |
||
| 311 | * |
||
| 312 | * @return mixed|void |
||
| 313 | */ |
||
| 314 | public function find($name) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Alternative for "find" method. |
||
| 327 | * |
||
| 328 | * @param $name |
||
| 329 | * |
||
| 330 | * @return mixed|void |
||
| 331 | */ |
||
| 332 | public function get($name) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Find a specific component, if there return that, otherwise throw exception. |
||
| 339 | * |
||
| 340 | * @param $name |
||
| 341 | * |
||
| 342 | * @return Component |
||
| 343 | * |
||
| 344 | * @throws ComponentNotFoundException |
||
| 345 | */ |
||
| 346 | public function findOrFail($name) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Get all components as laravel collection instance. |
||
| 359 | * |
||
| 360 | * @return Collection |
||
| 361 | */ |
||
| 362 | public function collections() |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Get component path for a specific component. |
||
| 369 | * |
||
| 370 | * @param $component |
||
| 371 | * |
||
| 372 | * @return string |
||
| 373 | */ |
||
| 374 | public function getComponentPath($component) |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Get asset path for a specific component. |
||
| 385 | * |
||
| 386 | * @param $component |
||
| 387 | * |
||
| 388 | * @return string |
||
| 389 | */ |
||
| 390 | public function assetPath($component) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Get a specific config data from a configuration file. |
||
| 397 | * |
||
| 398 | * @param $key |
||
| 399 | * |
||
| 400 | * @param null $default |
||
| 401 | * |
||
| 402 | * @return mixed |
||
| 403 | */ |
||
| 404 | public function config($key, $default = null) |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Get storage path for component used. |
||
| 411 | * |
||
| 412 | * @return string |
||
| 413 | */ |
||
| 414 | public function getUsedStoragePath() |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Set component used for cli session. |
||
| 425 | * |
||
| 426 | * @param $name |
||
| 427 | * |
||
| 428 | * @throws ComponentNotFoundException |
||
| 429 | */ |
||
| 430 | public function setUsed($name) |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Get component used for cli session. |
||
| 439 | * |
||
| 440 | * @return string |
||
| 441 | */ |
||
| 442 | public function getUsedNow() |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Get used now. |
||
| 449 | * |
||
| 450 | * @return string |
||
| 451 | */ |
||
| 452 | public function getUsed() |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Get laravel filesystem instance. |
||
| 459 | * |
||
| 460 | * @return \Illuminate\Filesystem\Filesystem |
||
| 461 | */ |
||
| 462 | public function getFiles() |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Get component assets path. |
||
| 469 | * |
||
| 470 | * @return string |
||
| 471 | */ |
||
| 472 | public function getAssetsPath() |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Get asset url from a specific component. |
||
| 479 | * |
||
| 480 | * @param string $asset |
||
| 481 | * |
||
| 482 | * @return string |
||
| 483 | */ |
||
| 484 | public function asset($asset) |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Determine whether the given component is activated. |
||
| 497 | * |
||
| 498 | * @param string $name |
||
| 499 | * |
||
| 500 | * @return bool |
||
| 501 | */ |
||
| 502 | public function active($name) |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Determine whether the given component is not activated. |
||
| 509 | * |
||
| 510 | * @param string $name |
||
| 511 | * |
||
| 512 | * @return bool |
||
| 513 | */ |
||
| 514 | public function notActive($name) |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Enabling a specific component. |
||
| 521 | * |
||
| 522 | * @param string $name |
||
| 523 | * |
||
| 524 | * @return bool |
||
| 525 | */ |
||
| 526 | public function enable($name) |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Disabling a specific component. |
||
| 533 | * |
||
| 534 | * @param string $name |
||
| 535 | * |
||
| 536 | * @return bool |
||
| 537 | */ |
||
| 538 | public function disable($name) |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Delete a specific component. |
||
| 545 | * |
||
| 546 | * @param string $name |
||
| 547 | * |
||
| 548 | * @return bool |
||
| 549 | */ |
||
| 550 | public function delete($name) |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Update dependencies for the specified component. |
||
| 557 | * |
||
| 558 | * @param string $component |
||
| 559 | */ |
||
| 560 | public function update($component) |
||
| 564 | |||
| 565 | /** |
||
| 566 | * Install the specified component. |
||
| 567 | * |
||
| 568 | * @param string $name |
||
| 569 | * @param string $version |
||
| 570 | * @param string $type |
||
| 571 | * @param bool $subtree |
||
| 572 | * |
||
| 573 | * @return \Symfony\Component\Process\Process |
||
| 574 | */ |
||
| 575 | public function install($name, $version = 'dev-master', $type = 'composer', $subtree = false) |
||
| 581 | |||
| 582 | /** |
||
| 583 | * Get stub path. |
||
| 584 | * |
||
| 585 | * @return string |
||
| 586 | */ |
||
| 587 | public function getStubPath() |
||
| 599 | |||
| 600 | /** |
||
| 601 | * Set stub path. |
||
| 602 | * |
||
| 603 | * @param string $stubPath |
||
| 604 | * |
||
| 605 | * @return $this |
||
| 606 | */ |
||
| 607 | public function setStubPath($stubPath) |
||
| 613 | } |
||
| 614 |
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.