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 |
||
15 | abstract class Repository implements RepositoryInterface, Countable |
||
16 | { |
||
17 | use Macroable; |
||
18 | |||
19 | /** |
||
20 | * Application instance. |
||
21 | * |
||
22 | * @var Illuminate\Contracts\Foundation\Application|Laravel\Lumen\Application |
||
23 | */ |
||
24 | protected $app; |
||
25 | |||
26 | /** |
||
27 | * The module path. |
||
28 | * |
||
29 | * @var string|null |
||
30 | */ |
||
31 | protected $path; |
||
32 | |||
33 | /** |
||
34 | * The scanned paths. |
||
35 | * |
||
36 | * @var array |
||
37 | */ |
||
38 | protected $paths = []; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $stubPath; |
||
44 | |||
45 | /** |
||
46 | * The constructor. |
||
47 | * |
||
48 | * @param Container $app |
||
49 | * @param string|null $path |
||
50 | */ |
||
51 | 179 | public function __construct(Container $app, $path = null) |
|
56 | |||
57 | /** |
||
58 | * Add other module location. |
||
59 | * |
||
60 | * @param string $path |
||
61 | * |
||
62 | * @return $this |
||
63 | */ |
||
64 | 15 | public function addLocation($path) |
|
70 | |||
71 | /** |
||
72 | * Alternative method for "addPath". |
||
73 | * |
||
74 | * @param string $path |
||
75 | * |
||
76 | * @return $this |
||
77 | * @deprecated |
||
78 | */ |
||
79 | 1 | public function addPath($path) |
|
83 | |||
84 | /** |
||
85 | * Get all additional paths. |
||
86 | * |
||
87 | * @return array |
||
88 | */ |
||
89 | 1 | public function getPaths() : array |
|
93 | |||
94 | /** |
||
95 | * Get scanned modules paths. |
||
96 | * |
||
97 | * @return array |
||
98 | */ |
||
99 | 179 | public function getScanPaths() : array |
|
115 | |||
116 | /** |
||
117 | * Creates a new Module instance |
||
118 | * |
||
119 | * @param Container $app |
||
120 | * @param $name |
||
121 | * @param $path |
||
122 | * @return \Nwidart\Modules\Module |
||
123 | */ |
||
124 | abstract protected function createModule(...$args); |
||
125 | |||
126 | /** |
||
127 | * Get & scan all modules. |
||
128 | * |
||
129 | * @return array |
||
130 | */ |
||
131 | 179 | public function scan() |
|
151 | |||
152 | /** |
||
153 | * Get all modules. |
||
154 | * |
||
155 | * @return array |
||
156 | */ |
||
157 | 179 | public function all() : array |
|
165 | |||
166 | /** |
||
167 | * Format the cached data as array of modules. |
||
168 | * |
||
169 | * @param array $cached |
||
170 | * |
||
171 | * @return array |
||
172 | */ |
||
173 | protected function formatCached($cached) |
||
185 | |||
186 | /** |
||
187 | * Get cached modules. |
||
188 | * |
||
189 | * @return array |
||
190 | */ |
||
191 | public function getCached() |
||
197 | |||
198 | /** |
||
199 | * Get all modules as collection instance. |
||
200 | * |
||
201 | * @return Collection |
||
202 | */ |
||
203 | 1 | public function toCollection() : Collection |
|
207 | |||
208 | /** |
||
209 | * Get modules by status. |
||
210 | * |
||
211 | * @param $status |
||
212 | * |
||
213 | * @return array |
||
214 | */ |
||
215 | 179 | public function getByStatus($status) : array |
|
227 | |||
228 | /** |
||
229 | * Determine whether the given module exist. |
||
230 | * |
||
231 | * @param $name |
||
232 | * |
||
233 | * @return bool |
||
234 | */ |
||
235 | 91 | public function has($name) : bool |
|
239 | |||
240 | /** |
||
241 | * Get list of enabled modules. |
||
242 | * |
||
243 | * @return array |
||
244 | */ |
||
245 | 179 | public function allEnabled() : array |
|
246 | { |
||
247 | 179 | return $this->getByStatus(1); |
|
248 | } |
||
249 | |||
250 | /** |
||
251 | * Get list of disabled modules. |
||
252 | * |
||
253 | * @return array |
||
254 | */ |
||
255 | 1 | public function allDisabled() : array |
|
256 | { |
||
257 | 1 | return $this->getByStatus(0); |
|
258 | } |
||
259 | |||
260 | /** |
||
261 | * Get count from all modules. |
||
262 | * |
||
263 | * @return int |
||
264 | */ |
||
265 | 1 | public function count() : int |
|
269 | |||
270 | /** |
||
271 | * Get all ordered modules. |
||
272 | * |
||
273 | * @param string $direction |
||
274 | * |
||
275 | * @return array |
||
276 | */ |
||
277 | 179 | public function getOrdered($direction = 'asc') : array |
|
295 | |||
296 | /** |
||
297 | * Get a module path. |
||
298 | * |
||
299 | * @return string |
||
300 | */ |
||
301 | 179 | public function getPath() : string |
|
305 | |||
306 | /** |
||
307 | * Register the modules. |
||
308 | */ |
||
309 | 179 | public function register() |
|
315 | |||
316 | /** |
||
317 | * Boot the modules. |
||
318 | */ |
||
319 | 179 | public function boot() |
|
325 | |||
326 | /** |
||
327 | * Find a specific module. |
||
328 | * @param $name |
||
329 | * @return mixed|void |
||
330 | */ |
||
331 | 100 | public function find($name) |
|
341 | |||
342 | /** |
||
343 | * Find a specific module by its alias. |
||
344 | * @param $alias |
||
345 | * @return mixed|void |
||
346 | */ |
||
347 | 2 | public function findByAlias($alias) |
|
357 | |||
358 | /** |
||
359 | * Find all modules that are required by a module. If the module cannot be found, throw an exception. |
||
360 | * |
||
361 | * @param $name |
||
362 | * @return array |
||
363 | * @throws ModuleNotFoundException |
||
364 | */ |
||
365 | 1 | public function findRequirements($name) |
|
377 | |||
378 | /** |
||
379 | * Alternative for "find" method. |
||
380 | * @param $name |
||
381 | * @return mixed|void |
||
382 | * @deprecated |
||
383 | */ |
||
384 | 1 | public function get($name) |
|
388 | |||
389 | /** |
||
390 | * Find a specific module, if there return that, otherwise throw exception. |
||
391 | * |
||
392 | * @param $name |
||
393 | * |
||
394 | * @return Module |
||
395 | * |
||
396 | * @throws ModuleNotFoundException |
||
397 | */ |
||
398 | 98 | public function findOrFail($name) |
|
408 | |||
409 | /** |
||
410 | * Get all modules as laravel collection instance. |
||
411 | * |
||
412 | * @param $status |
||
413 | * |
||
414 | * @return Collection |
||
415 | */ |
||
416 | 1 | public function collections($status = 1) : Collection |
|
420 | |||
421 | /** |
||
422 | * Get module path for a specific module. |
||
423 | * |
||
424 | * @param $module |
||
425 | * |
||
426 | * @return string |
||
427 | */ |
||
428 | 91 | public function getModulePath($module) |
|
436 | |||
437 | /** |
||
438 | * Get asset path for a specific module. |
||
439 | * |
||
440 | * @param $module |
||
441 | * |
||
442 | * @return string |
||
443 | */ |
||
444 | 2 | public function assetPath($module) : string |
|
448 | |||
449 | /** |
||
450 | * Get a specific config data from a configuration file. |
||
451 | * |
||
452 | * @param $key |
||
453 | * |
||
454 | * @param null $default |
||
455 | * @return mixed |
||
456 | */ |
||
457 | 179 | public function config($key, $default = null) |
|
461 | |||
462 | /** |
||
463 | * Get storage path for module used. |
||
464 | * |
||
465 | * @return string |
||
466 | */ |
||
467 | 2 | public function getUsedStoragePath() : string |
|
481 | |||
482 | /** |
||
483 | * Set module used for cli session. |
||
484 | * |
||
485 | * @param $name |
||
486 | * |
||
487 | * @throws ModuleNotFoundException |
||
488 | */ |
||
489 | 1 | public function setUsed($name) |
|
495 | |||
496 | /** |
||
497 | * Forget the module used for cli session. |
||
498 | */ |
||
499 | public function forgetUsed() |
||
505 | |||
506 | /** |
||
507 | * Get module used for cli session. |
||
508 | * @return string |
||
509 | * @throws \Nwidart\Modules\Exceptions\ModuleNotFoundException |
||
510 | */ |
||
511 | 1 | public function getUsedNow() : string |
|
515 | |||
516 | /** |
||
517 | * Get used now. |
||
518 | * |
||
519 | * @return string |
||
520 | * @deprecated |
||
521 | */ |
||
522 | 1 | public function getUsed() |
|
526 | |||
527 | /** |
||
528 | * Get laravel filesystem instance. |
||
529 | * |
||
530 | * @return \Illuminate\Filesystem\Filesystem |
||
531 | */ |
||
532 | 4 | public function getFiles() |
|
536 | |||
537 | /** |
||
538 | * Get module assets path. |
||
539 | * |
||
540 | * @return string |
||
541 | */ |
||
542 | 2 | public function getAssetsPath() : string |
|
546 | |||
547 | /** |
||
548 | * Get asset url from a specific module. |
||
549 | * @param string $asset |
||
550 | * @return string |
||
551 | * @throws InvalidAssetPath |
||
552 | */ |
||
553 | 2 | public function asset($asset) : string |
|
566 | |||
567 | /** |
||
568 | * Determine whether the given module is activated. |
||
569 | * @param string $name |
||
570 | * @return bool |
||
571 | * @throws ModuleNotFoundException |
||
572 | */ |
||
573 | 4 | public function enabled($name) : bool |
|
577 | |||
578 | /** |
||
579 | * Determine whether the given module is not activated. |
||
580 | * @param string $name |
||
581 | * @return bool |
||
582 | * @throws ModuleNotFoundException |
||
583 | */ |
||
584 | 2 | public function disabled($name) : bool |
|
588 | |||
589 | /** |
||
590 | * Enabling a specific module. |
||
591 | * @param string $name |
||
592 | * @return void |
||
593 | * @throws \Nwidart\Modules\Exceptions\ModuleNotFoundException |
||
594 | */ |
||
595 | 1 | public function enable($name) |
|
599 | |||
600 | /** |
||
601 | * Disabling a specific module. |
||
602 | * @param string $name |
||
603 | * @return void |
||
604 | * @throws \Nwidart\Modules\Exceptions\ModuleNotFoundException |
||
605 | */ |
||
606 | 1 | public function disable($name) |
|
610 | |||
611 | /** |
||
612 | * Delete a specific module. |
||
613 | * @param string $name |
||
614 | * @return bool |
||
615 | * @throws \Nwidart\Modules\Exceptions\ModuleNotFoundException |
||
616 | */ |
||
617 | 2 | public function delete($name) : bool |
|
621 | |||
622 | /** |
||
623 | * Update dependencies for the specified module. |
||
624 | * |
||
625 | * @param string $module |
||
626 | */ |
||
627 | public function update($module) |
||
631 | |||
632 | /** |
||
633 | * Install the specified module. |
||
634 | * |
||
635 | * @param string $name |
||
636 | * @param string $version |
||
637 | * @param string $type |
||
638 | * @param bool $subtree |
||
639 | * |
||
640 | * @return \Symfony\Component\Process\Process |
||
641 | */ |
||
642 | public function install($name, $version = 'dev-master', $type = 'composer', $subtree = false) |
||
648 | |||
649 | /** |
||
650 | * Get stub path. |
||
651 | * |
||
652 | * @return string|null |
||
653 | */ |
||
654 | 3 | public function getStubPath() |
|
666 | |||
667 | /** |
||
668 | * Set stub path. |
||
669 | * |
||
670 | * @param string $stubPath |
||
671 | * |
||
672 | * @return $this |
||
673 | */ |
||
674 | 1 | public function setStubPath($stubPath) |
|
680 | } |
||
681 |
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..