Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like FileRepository 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 FileRepository, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | abstract class FileRepository implements RepositoryInterface, Countable |
||
| 20 | { |
||
| 21 | use Macroable; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Application instance. |
||
| 25 | * |
||
| 26 | * @var \Illuminate\Contracts\Foundation\Application|\Laravel\Lumen\Application |
||
| 27 | */ |
||
| 28 | protected $app; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * The module path. |
||
| 32 | * |
||
| 33 | * @var string|null |
||
| 34 | */ |
||
| 35 | protected $path; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * The scanned paths. |
||
| 39 | * |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | protected $paths = []; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | protected $stubPath; |
||
| 48 | /** |
||
| 49 | * @var UrlGenerator |
||
| 50 | */ |
||
| 51 | private $url; |
||
| 52 | /** |
||
| 53 | * @var ConfigRepository |
||
| 54 | */ |
||
| 55 | private $config; |
||
| 56 | /** |
||
| 57 | * @var Filesystem |
||
| 58 | */ |
||
| 59 | private $files; |
||
| 60 | /** |
||
| 61 | * @var CacheManager |
||
| 62 | */ |
||
| 63 | private $cache; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * The constructor. |
||
| 67 | * @param Container $app |
||
| 68 | * @param string|null $path |
||
| 69 | */ |
||
| 70 | 190 | View Code Duplication | public function __construct(Container $app, $path = null) |
| 79 | |||
| 80 | /** |
||
| 81 | * Add other module location. |
||
| 82 | * |
||
| 83 | * @param string $path |
||
| 84 | * |
||
| 85 | * @return $this |
||
| 86 | */ |
||
| 87 | 15 | public function addLocation($path) |
|
| 93 | |||
| 94 | /** |
||
| 95 | * Get all additional paths. |
||
| 96 | * |
||
| 97 | * @return array |
||
| 98 | */ |
||
| 99 | 1 | public function getPaths() : array |
|
| 103 | |||
| 104 | /** |
||
| 105 | * Get scanned modules paths. |
||
| 106 | * |
||
| 107 | * @return array |
||
| 108 | */ |
||
| 109 | 190 | public function getScanPaths() : array |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Creates a new Module instance |
||
| 128 | * |
||
| 129 | * @param Container $app |
||
| 130 | * @param string $args |
||
| 131 | * @param string $path |
||
| 132 | * @return \Nwidart\Modules\Module |
||
| 133 | */ |
||
| 134 | abstract protected function createModule(...$args); |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Get & scan all modules. |
||
| 138 | * |
||
| 139 | * @return array |
||
| 140 | */ |
||
| 141 | 190 | public function scan() |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Get all modules. |
||
| 164 | * |
||
| 165 | * @return array |
||
| 166 | */ |
||
| 167 | 190 | public function all() : array |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Format the cached data as array of modules. |
||
| 178 | * |
||
| 179 | * @param array $cached |
||
| 180 | * |
||
| 181 | * @return array |
||
| 182 | */ |
||
| 183 | protected function formatCached($cached) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Get cached modules. |
||
| 198 | * |
||
| 199 | * @return array |
||
| 200 | */ |
||
| 201 | public function getCached() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Get all modules as collection instance. |
||
| 210 | * |
||
| 211 | * @return Collection |
||
| 212 | */ |
||
| 213 | 1 | public function toCollection() : Collection |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Get modules by status. |
||
| 220 | * |
||
| 221 | * @param $status |
||
| 222 | * |
||
| 223 | * @return array |
||
| 224 | */ |
||
| 225 | 190 | public function getByStatus($status) : array |
|
| 237 | |||
| 238 | /** |
||
| 239 | * Determine whether the given module exist. |
||
| 240 | * |
||
| 241 | * @param $name |
||
| 242 | * |
||
| 243 | * @return bool |
||
| 244 | */ |
||
| 245 | 99 | public function has($name) : bool |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Get list of enabled modules. |
||
| 252 | * |
||
| 253 | * @return array |
||
| 254 | */ |
||
| 255 | 190 | public function allEnabled() : array |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Get list of disabled modules. |
||
| 262 | * |
||
| 263 | * @return array |
||
| 264 | */ |
||
| 265 | 1 | public function allDisabled() : array |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Get count from all modules. |
||
| 272 | * |
||
| 273 | * @return int |
||
| 274 | */ |
||
| 275 | 1 | public function count() : int |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Get all ordered modules. |
||
| 282 | * |
||
| 283 | * @param string $direction |
||
| 284 | * |
||
| 285 | * @return array |
||
| 286 | */ |
||
| 287 | 190 | public function getOrdered($direction = 'asc') : array |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Get a module path. |
||
| 308 | * |
||
| 309 | * @return string |
||
| 310 | */ |
||
| 311 | 190 | public function getPath() : string |
|
| 315 | |||
| 316 | /** |
||
| 317 | * Register the modules. |
||
| 318 | */ |
||
| 319 | 190 | public function register() |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Boot the modules. |
||
| 328 | */ |
||
| 329 | 190 | public function boot() |
|
| 335 | |||
| 336 | /** |
||
| 337 | * Find a specific module. |
||
| 338 | * @param $name |
||
| 339 | * @return mixed|void |
||
| 340 | */ |
||
| 341 | 108 | public function find($name) |
|
| 351 | |||
| 352 | /** |
||
| 353 | * Find a specific module by its alias. |
||
| 354 | * @param $alias |
||
| 355 | * @return mixed|void |
||
| 356 | */ |
||
| 357 | 2 | public function findByAlias($alias) |
|
| 367 | |||
| 368 | /** |
||
| 369 | * Find all modules that are required by a module. If the module cannot be found, throw an exception. |
||
| 370 | * |
||
| 371 | * @param $name |
||
| 372 | * @return array |
||
| 373 | * @throws ModuleNotFoundException |
||
| 374 | */ |
||
| 375 | 1 | public function findRequirements($name) |
|
| 387 | |||
| 388 | /** |
||
| 389 | * Find a specific module, if there return that, otherwise throw exception. |
||
| 390 | * |
||
| 391 | * @param $name |
||
| 392 | * |
||
| 393 | * @return Module |
||
| 394 | * |
||
| 395 | * @throws ModuleNotFoundException |
||
| 396 | */ |
||
| 397 | 106 | public function findOrFail($name) |
|
| 407 | |||
| 408 | /** |
||
| 409 | * Get all modules as laravel collection instance. |
||
| 410 | * |
||
| 411 | * @param $status |
||
| 412 | * |
||
| 413 | * @return Collection |
||
| 414 | */ |
||
| 415 | 1 | public function collections($status = 1) : Collection |
|
| 419 | |||
| 420 | /** |
||
| 421 | * Get module path for a specific module. |
||
| 422 | * |
||
| 423 | * @param $module |
||
| 424 | * |
||
| 425 | * @return string |
||
| 426 | */ |
||
| 427 | 99 | public function getModulePath($module) |
|
| 435 | |||
| 436 | /** |
||
| 437 | * Get asset path for a specific module. |
||
| 438 | * |
||
| 439 | * @param $module |
||
| 440 | * |
||
| 441 | * @return string |
||
| 442 | */ |
||
| 443 | 2 | public function assetPath($module) : string |
|
| 447 | |||
| 448 | /** |
||
| 449 | * Get a specific config data from a configuration file. |
||
| 450 | * |
||
| 451 | * @param $key |
||
| 452 | * |
||
| 453 | * @param null $default |
||
| 454 | * @return mixed |
||
| 455 | */ |
||
| 456 | 190 | public function config($key, $default = null) |
|
| 460 | |||
| 461 | /** |
||
| 462 | * Get storage path for module used. |
||
| 463 | * |
||
| 464 | * @return string |
||
| 465 | */ |
||
| 466 | 2 | public function getUsedStoragePath() : string |
|
| 480 | |||
| 481 | /** |
||
| 482 | * Set module used for cli session. |
||
| 483 | * |
||
| 484 | * @param $name |
||
| 485 | * |
||
| 486 | * @throws ModuleNotFoundException |
||
| 487 | */ |
||
| 488 | 1 | public function setUsed($name) |
|
| 494 | |||
| 495 | /** |
||
| 496 | * Forget the module used for cli session. |
||
| 497 | */ |
||
| 498 | public function forgetUsed() |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Get module used for cli session. |
||
| 507 | * @return string |
||
| 508 | * @throws \Nwidart\Modules\Exceptions\ModuleNotFoundException |
||
| 509 | */ |
||
| 510 | 1 | public function getUsedNow() : string |
|
| 514 | |||
| 515 | /** |
||
| 516 | * Get laravel filesystem instance. |
||
| 517 | * |
||
| 518 | * @return Filesystem |
||
| 519 | */ |
||
| 520 | 190 | public function getFiles(): Filesystem |
|
| 524 | |||
| 525 | /** |
||
| 526 | * Get module assets path. |
||
| 527 | * |
||
| 528 | * @return string |
||
| 529 | */ |
||
| 530 | 2 | public function getAssetsPath() : string |
|
| 534 | |||
| 535 | /** |
||
| 536 | * Get asset url from a specific module. |
||
| 537 | * @param string $asset |
||
| 538 | * @return string |
||
| 539 | * @throws InvalidAssetPath |
||
| 540 | */ |
||
| 541 | 2 | public function asset($asset) : string |
|
| 554 | |||
| 555 | /** |
||
| 556 | * Determine whether the given module is activated. |
||
| 557 | * @param string $name |
||
| 558 | * @return bool |
||
| 559 | * @throws ModuleNotFoundException |
||
| 560 | */ |
||
| 561 | 4 | public function enabled($name) : bool |
|
| 565 | |||
| 566 | /** |
||
| 567 | * Determine whether the given module is not activated. |
||
| 568 | * @param string $name |
||
| 569 | * @return bool |
||
| 570 | * @throws ModuleNotFoundException |
||
| 571 | */ |
||
| 572 | 2 | public function disabled($name) : bool |
|
| 576 | |||
| 577 | /** |
||
| 578 | * Enabling a specific module. |
||
| 579 | * @param string $name |
||
| 580 | * @return void |
||
| 581 | * @throws \Nwidart\Modules\Exceptions\ModuleNotFoundException |
||
| 582 | */ |
||
| 583 | 1 | public function enable($name) |
|
| 587 | |||
| 588 | /** |
||
| 589 | * Disabling a specific module. |
||
| 590 | * @param string $name |
||
| 591 | * @return void |
||
| 592 | * @throws \Nwidart\Modules\Exceptions\ModuleNotFoundException |
||
| 593 | */ |
||
| 594 | 1 | public function disable($name) |
|
| 598 | |||
| 599 | /** |
||
| 600 | * Delete a specific module. |
||
| 601 | * @param string $name |
||
| 602 | * @return bool |
||
| 603 | * @throws \Nwidart\Modules\Exceptions\ModuleNotFoundException |
||
| 604 | */ |
||
| 605 | 2 | public function delete($name) : bool |
|
| 609 | |||
| 610 | /** |
||
| 611 | * Update dependencies for the specified module. |
||
| 612 | * |
||
| 613 | * @param string $module |
||
| 614 | */ |
||
| 615 | public function update($module) |
||
| 619 | |||
| 620 | /** |
||
| 621 | * Install the specified module. |
||
| 622 | * |
||
| 623 | * @param string $name |
||
| 624 | * @param string $version |
||
| 625 | * @param string $type |
||
| 626 | * @param bool $subtree |
||
| 627 | * |
||
| 628 | * @return \Symfony\Component\Process\Process |
||
| 629 | */ |
||
| 630 | public function install($name, $version = 'dev-master', $type = 'composer', $subtree = false) |
||
| 636 | |||
| 637 | /** |
||
| 638 | * Get stub path. |
||
| 639 | * |
||
| 640 | * @return string|null |
||
| 641 | */ |
||
| 642 | 3 | public function getStubPath() |
|
| 654 | |||
| 655 | /** |
||
| 656 | * Set stub path. |
||
| 657 | * |
||
| 658 | * @param string $stubPath |
||
| 659 | * |
||
| 660 | * @return $this |
||
| 661 | */ |
||
| 662 | 1 | public function setStubPath($stubPath) |
|
| 668 | } |
||
| 669 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..