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 | 136 | 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 | */ |
||
78 | 1 | public function addPath($path) |
|
82 | |||
83 | /** |
||
84 | * Get all additional paths. |
||
85 | * |
||
86 | * @return array |
||
87 | */ |
||
88 | 1 | public function getPaths() |
|
92 | |||
93 | /** |
||
94 | * Get scanned modules paths. |
||
95 | * |
||
96 | * @return array |
||
97 | */ |
||
98 | 136 | public function getScanPaths() |
|
110 | |||
111 | /** |
||
112 | * Get & scan all modules. |
||
113 | * |
||
114 | * @return array |
||
115 | */ |
||
116 | abstract public function scan(); |
||
117 | |||
118 | /** |
||
119 | * Get all modules. |
||
120 | * |
||
121 | * @return array |
||
122 | */ |
||
123 | 136 | public function all() |
|
131 | |||
132 | /** |
||
133 | * Format the cached data as array of modules. |
||
134 | * |
||
135 | * @param array $cached |
||
136 | * |
||
137 | * @return array |
||
138 | */ |
||
139 | abstract protected function formatCached($cached); |
||
140 | |||
141 | /** |
||
142 | * Get cached modules. |
||
143 | * |
||
144 | * @return array |
||
145 | */ |
||
146 | public function getCached() |
||
152 | |||
153 | /** |
||
154 | * Get all modules as collection instance. |
||
155 | * |
||
156 | * @return Collection |
||
157 | */ |
||
158 | 1 | public function toCollection() |
|
162 | |||
163 | /** |
||
164 | * Get modules by status. |
||
165 | * |
||
166 | * @param $status |
||
167 | * |
||
168 | * @return array |
||
169 | */ |
||
170 | 136 | public function getByStatus($status) |
|
182 | |||
183 | /** |
||
184 | * Determine whether the given module exist. |
||
185 | * |
||
186 | * @param $name |
||
187 | * |
||
188 | * @return bool |
||
189 | */ |
||
190 | 56 | public function has($name) |
|
194 | |||
195 | /** |
||
196 | * Get list of enabled modules. |
||
197 | * |
||
198 | * @return array |
||
199 | */ |
||
200 | 136 | public function enabled() |
|
204 | |||
205 | /** |
||
206 | * Get list of disabled modules. |
||
207 | * |
||
208 | * @return array |
||
209 | */ |
||
210 | 1 | public function disabled() |
|
214 | |||
215 | /** |
||
216 | * Get count from all modules. |
||
217 | * |
||
218 | * @return int |
||
219 | */ |
||
220 | 1 | public function count() |
|
224 | |||
225 | /** |
||
226 | * Get all ordered modules. |
||
227 | * |
||
228 | * @param string $direction |
||
229 | * |
||
230 | * @return array |
||
231 | */ |
||
232 | 136 | public function getOrdered($direction = 'asc') |
|
250 | |||
251 | /** |
||
252 | * Get a module path. |
||
253 | * |
||
254 | * @return string |
||
255 | */ |
||
256 | 136 | public function getPath() |
|
260 | |||
261 | /** |
||
262 | * Register the modules. |
||
263 | */ |
||
264 | 136 | public function register() |
|
270 | |||
271 | /** |
||
272 | * Boot the modules. |
||
273 | */ |
||
274 | 136 | public function boot() |
|
280 | |||
281 | /** |
||
282 | * Find a specific module. |
||
283 | * @param $name |
||
284 | * @return mixed|void |
||
285 | */ |
||
286 | 65 | public function find($name) |
|
296 | |||
297 | /** |
||
298 | * Find a specific module by its alias. |
||
299 | * @param $alias |
||
300 | * @return mixed|void |
||
301 | */ |
||
302 | 2 | public function findByAlias($alias) |
|
312 | |||
313 | /** |
||
314 | * Find all modules that are required by a module. If the module cannot be found, throw an exception. |
||
315 | * |
||
316 | * @param $name |
||
317 | * @return array |
||
318 | * @throws ModuleNotFoundException |
||
319 | */ |
||
320 | 1 | public function findRequirements($name) |
|
332 | |||
333 | /** |
||
334 | * Alternative for "find" method. |
||
335 | * @param $name |
||
336 | * @return mixed|void |
||
337 | */ |
||
338 | 1 | public function get($name) |
|
342 | |||
343 | /** |
||
344 | * Find a specific module, if there return that, otherwise throw exception. |
||
345 | * |
||
346 | * @param $name |
||
347 | * |
||
348 | * @return Module |
||
349 | * |
||
350 | * @throws ModuleNotFoundException |
||
351 | */ |
||
352 | 63 | public function findOrFail($name) |
|
362 | |||
363 | /** |
||
364 | * Get all modules as laravel collection instance. |
||
365 | * |
||
366 | * @param $status |
||
367 | * |
||
368 | * @return Collection |
||
369 | */ |
||
370 | 1 | public function collections($status = 1) |
|
374 | |||
375 | /** |
||
376 | * Get module path for a specific module. |
||
377 | * |
||
378 | * @param $module |
||
379 | * |
||
380 | * @return string |
||
381 | */ |
||
382 | 56 | public function getModulePath($module) |
|
390 | |||
391 | /** |
||
392 | * Get asset path for a specific module. |
||
393 | * |
||
394 | * @param $module |
||
395 | * |
||
396 | * @return string |
||
397 | */ |
||
398 | 2 | public function assetPath($module) |
|
402 | |||
403 | /** |
||
404 | * Get a specific config data from a configuration file. |
||
405 | * |
||
406 | * @param $key |
||
407 | * |
||
408 | * @param null $default |
||
409 | * @return mixed |
||
410 | */ |
||
411 | 136 | public function config($key, $default = null) |
|
415 | |||
416 | /** |
||
417 | * Get storage path for module used. |
||
418 | * |
||
419 | * @return string |
||
420 | */ |
||
421 | 2 | public function getUsedStoragePath() |
|
435 | |||
436 | /** |
||
437 | * Set module used for cli session. |
||
438 | * |
||
439 | * @param $name |
||
440 | * |
||
441 | * @throws ModuleNotFoundException |
||
442 | */ |
||
443 | 1 | public function setUsed($name) |
|
449 | |||
450 | /** |
||
451 | * Get module used for cli session. |
||
452 | * |
||
453 | * @return string |
||
454 | */ |
||
455 | 1 | public function getUsedNow() |
|
459 | |||
460 | /** |
||
461 | * Get used now. |
||
462 | * |
||
463 | * @return string |
||
464 | */ |
||
465 | 1 | public function getUsed() |
|
469 | |||
470 | /** |
||
471 | * Get laravel filesystem instance. |
||
472 | * |
||
473 | * @return \Illuminate\Filesystem\Filesystem |
||
474 | */ |
||
475 | 4 | public function getFiles() |
|
479 | |||
480 | /** |
||
481 | * Get module assets path. |
||
482 | * |
||
483 | * @return string |
||
484 | */ |
||
485 | 2 | public function getAssetsPath() |
|
489 | |||
490 | /** |
||
491 | * Get asset url from a specific module. |
||
492 | * |
||
493 | * @param string $asset |
||
494 | * |
||
495 | * @return string |
||
496 | */ |
||
497 | 2 | public function asset($asset) |
|
510 | |||
511 | /** |
||
512 | * Determine whether the given module is activated. |
||
513 | * |
||
514 | * @param string $name |
||
515 | * |
||
516 | * @return bool |
||
517 | */ |
||
518 | 4 | public function active($name) |
|
522 | |||
523 | /** |
||
524 | * Determine whether the given module is not activated. |
||
525 | * |
||
526 | * @param string $name |
||
527 | * |
||
528 | * @return bool |
||
529 | */ |
||
530 | 2 | public function notActive($name) |
|
534 | |||
535 | /** |
||
536 | * Enabling a specific module. |
||
537 | * |
||
538 | * @param string $name |
||
539 | * |
||
540 | * @return bool |
||
541 | */ |
||
542 | 1 | public function enable($name) |
|
546 | |||
547 | /** |
||
548 | * Disabling a specific module. |
||
549 | * |
||
550 | * @param string $name |
||
551 | * |
||
552 | * @return bool |
||
553 | */ |
||
554 | 1 | public function disable($name) |
|
558 | |||
559 | /** |
||
560 | * Delete a specific module. |
||
561 | * |
||
562 | * @param string $name |
||
563 | * |
||
564 | * @return bool |
||
565 | */ |
||
566 | 2 | public function delete($name) |
|
570 | |||
571 | /** |
||
572 | * Update dependencies for the specified module. |
||
573 | * |
||
574 | * @param string $module |
||
575 | */ |
||
576 | public function update($module) |
||
580 | |||
581 | /** |
||
582 | * Install the specified module. |
||
583 | * |
||
584 | * @param string $name |
||
585 | * @param string $version |
||
586 | * @param string $type |
||
587 | * @param bool $subtree |
||
588 | * |
||
589 | * @return \Symfony\Component\Process\Process |
||
590 | */ |
||
591 | public function install($name, $version = 'dev-master', $type = 'composer', $subtree = false) |
||
597 | |||
598 | /** |
||
599 | * Get stub path. |
||
600 | * |
||
601 | * @return string |
||
602 | */ |
||
603 | 3 | public function getStubPath() |
|
615 | |||
616 | /** |
||
617 | * Set stub path. |
||
618 | * |
||
619 | * @param string $stubPath |
||
620 | * |
||
621 | * @return $this |
||
622 | */ |
||
623 | 1 | public function setStubPath($stubPath) |
|
629 | } |
||
630 |
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..