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 | 217 | public function __construct(Container $app, $path = null) |
|
71 | { |
||
72 | 217 | $this->app = $app; |
|
|
|||
73 | 217 | $this->path = $path; |
|
74 | 217 | $this->url = $app['url']; |
|
75 | 217 | $this->config = $app['config']; |
|
76 | 217 | $this->files = $app['files']; |
|
77 | 217 | $this->cache = $app['cache']; |
|
78 | 217 | } |
|
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 | 217 | 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 | 217 | public function scan() |
|
161 | |||
162 | /** |
||
163 | * Get all modules. |
||
164 | * |
||
165 | * @return array |
||
166 | */ |
||
167 | 217 | 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 | 217 | public function getByStatus($status) : array |
|
238 | |||
239 | /** |
||
240 | * Determine whether the given module exist. |
||
241 | * |
||
242 | * @param $name |
||
243 | * |
||
244 | * @return bool |
||
245 | */ |
||
246 | 120 | public function has($name) : bool |
|
250 | |||
251 | /** |
||
252 | * Get list of enabled modules. |
||
253 | * |
||
254 | * @return array |
||
255 | */ |
||
256 | 217 | public function allEnabled() : array |
|
260 | |||
261 | /** |
||
262 | * Get list of disabled modules. |
||
263 | * |
||
264 | * @return array |
||
265 | */ |
||
266 | 1 | public function allDisabled() : array |
|
270 | |||
271 | /** |
||
272 | * Get count from all modules. |
||
273 | * |
||
274 | * @return int |
||
275 | */ |
||
276 | 1 | public function count() : int |
|
280 | |||
281 | /** |
||
282 | * Get all ordered modules. |
||
283 | * |
||
284 | * @param string $direction |
||
285 | * |
||
286 | * @return array |
||
287 | */ |
||
288 | 217 | public function getOrdered($direction = 'asc') : array |
|
306 | |||
307 | /** |
||
308 | * @inheritDoc |
||
309 | */ |
||
310 | 217 | public function getPath() : string |
|
314 | |||
315 | /** |
||
316 | * @inheritDoc |
||
317 | */ |
||
318 | 217 | public function register(): void |
|
324 | |||
325 | /** |
||
326 | * @inheritDoc |
||
327 | */ |
||
328 | 217 | public function boot(): void |
|
334 | |||
335 | /** |
||
336 | * @inheritDoc |
||
337 | */ |
||
338 | 129 | public function find(string $name) |
|
348 | |||
349 | /** |
||
350 | * @inheritDoc |
||
351 | */ |
||
352 | 2 | public function findByAlias(string $alias) |
|
362 | |||
363 | /** |
||
364 | * @inheritDoc |
||
365 | */ |
||
366 | 1 | public function findRequirements($name): array |
|
378 | |||
379 | /** |
||
380 | * Find a specific module, if there return that, otherwise throw exception. |
||
381 | * |
||
382 | * @param $name |
||
383 | * |
||
384 | * @return Module |
||
385 | * |
||
386 | * @throws ModuleNotFoundException |
||
387 | */ |
||
388 | 127 | public function findOrFail(string $name) |
|
398 | |||
399 | /** |
||
400 | * Get all modules as laravel collection instance. |
||
401 | * |
||
402 | * @param $status |
||
403 | * |
||
404 | * @return Collection |
||
405 | */ |
||
406 | 1 | public function collections($status = 1) : Collection |
|
410 | |||
411 | /** |
||
412 | * Get module path for a specific module. |
||
413 | * |
||
414 | * @param $module |
||
415 | * |
||
416 | * @return string |
||
417 | */ |
||
418 | 120 | public function getModulePath($module) |
|
426 | |||
427 | /** |
||
428 | * @inheritDoc |
||
429 | */ |
||
430 | 2 | public function assetPath(string $module) : string |
|
434 | |||
435 | /** |
||
436 | * @inheritDoc |
||
437 | */ |
||
438 | 217 | public function config(string $key, $default = null) |
|
442 | |||
443 | /** |
||
444 | * Get storage path for module used. |
||
445 | * |
||
446 | * @return string |
||
447 | */ |
||
448 | 2 | public function getUsedStoragePath() : string |
|
462 | |||
463 | /** |
||
464 | * Set module used for cli session. |
||
465 | * |
||
466 | * @param $name |
||
467 | * |
||
468 | * @throws ModuleNotFoundException |
||
469 | */ |
||
470 | 1 | public function setUsed($name) |
|
476 | |||
477 | /** |
||
478 | * Forget the module used for cli session. |
||
479 | */ |
||
480 | public function forgetUsed() |
||
486 | |||
487 | /** |
||
488 | * Get module used for cli session. |
||
489 | * @return string |
||
490 | * @throws \Nwidart\Modules\Exceptions\ModuleNotFoundException |
||
491 | */ |
||
492 | 1 | public function getUsedNow() : string |
|
496 | |||
497 | /** |
||
498 | * Get laravel filesystem instance. |
||
499 | * |
||
500 | * @return Filesystem |
||
501 | */ |
||
502 | 217 | public function getFiles(): Filesystem |
|
506 | |||
507 | /** |
||
508 | * Get module assets path. |
||
509 | * |
||
510 | * @return string |
||
511 | */ |
||
512 | 2 | public function getAssetsPath() : string |
|
516 | |||
517 | /** |
||
518 | * Get asset url from a specific module. |
||
519 | * @param string $asset |
||
520 | * @return string |
||
521 | * @throws InvalidAssetPath |
||
522 | */ |
||
523 | 2 | public function asset($asset) : string |
|
536 | |||
537 | /** |
||
538 | * @inheritDoc |
||
539 | */ |
||
540 | 6 | public function isEnabled(string $name) : bool |
|
544 | |||
545 | /** |
||
546 | * @inheritDoc |
||
547 | */ |
||
548 | 3 | public function isDisabled(string $name) : bool |
|
552 | |||
553 | /** |
||
554 | * Enabling a specific module. |
||
555 | * @param string $name |
||
556 | * @return void |
||
557 | * @throws \Nwidart\Modules\Exceptions\ModuleNotFoundException |
||
558 | */ |
||
559 | 2 | public function enable($name) |
|
563 | |||
564 | /** |
||
565 | * Disabling a specific module. |
||
566 | * @param string $name |
||
567 | * @return void |
||
568 | * @throws \Nwidart\Modules\Exceptions\ModuleNotFoundException |
||
569 | */ |
||
570 | 1 | public function disable($name) |
|
574 | |||
575 | /** |
||
576 | * @inheritDoc |
||
577 | */ |
||
578 | 100 | public function delete($name) : bool |
|
582 | |||
583 | /** |
||
584 | * Update dependencies for the specified module. |
||
585 | * |
||
586 | * @param string $module |
||
587 | */ |
||
588 | public function update($module) |
||
592 | |||
593 | /** |
||
594 | * Install the specified module. |
||
595 | * |
||
596 | * @param string $name |
||
597 | * @param string $version |
||
598 | * @param string $type |
||
599 | * @param bool $subtree |
||
600 | * |
||
601 | * @return \Symfony\Component\Process\Process |
||
602 | */ |
||
603 | public function install($name, $version = 'dev-master', $type = 'composer', $subtree = false) |
||
609 | |||
610 | /** |
||
611 | * Get stub path. |
||
612 | * |
||
613 | * @return string|null |
||
614 | */ |
||
615 | 3 | public function getStubPath() |
|
627 | |||
628 | /** |
||
629 | * Set stub path. |
||
630 | * |
||
631 | * @param string $stubPath |
||
632 | * |
||
633 | * @return $this |
||
634 | */ |
||
635 | 1 | public function setStubPath($stubPath) |
|
641 | } |
||
642 |
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..