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 |
||
15 | abstract class FileRepository 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 | 181 | 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 | * Get all additional paths. |
||
73 | * |
||
74 | * @return array |
||
75 | */ |
||
76 | 1 | public function getPaths() : array |
|
80 | |||
81 | /** |
||
82 | * Get scanned modules paths. |
||
83 | * |
||
84 | * @return array |
||
85 | */ |
||
86 | 181 | public function getScanPaths() : array |
|
102 | |||
103 | /** |
||
104 | * Creates a new Module instance |
||
105 | * |
||
106 | * @param Container $app |
||
107 | * @param $name |
||
108 | * @param $path |
||
109 | * @return \Nwidart\Modules\Module |
||
110 | */ |
||
111 | abstract protected function createModule(...$args); |
||
112 | |||
113 | /** |
||
114 | * Get & scan all modules. |
||
115 | * |
||
116 | * @return array |
||
117 | */ |
||
118 | 181 | public function scan() |
|
138 | |||
139 | /** |
||
140 | * Get all modules. |
||
141 | * |
||
142 | * @return array |
||
143 | */ |
||
144 | 181 | public function all() : array |
|
152 | |||
153 | /** |
||
154 | * Format the cached data as array of modules. |
||
155 | * |
||
156 | * @param array $cached |
||
157 | * |
||
158 | * @return array |
||
159 | */ |
||
160 | protected function formatCached($cached) |
||
172 | |||
173 | /** |
||
174 | * Get cached modules. |
||
175 | * |
||
176 | * @return array |
||
177 | */ |
||
178 | public function getCached() |
||
184 | |||
185 | /** |
||
186 | * Get all modules as collection instance. |
||
187 | * |
||
188 | * @return Collection |
||
189 | */ |
||
190 | 1 | public function toCollection() : Collection |
|
194 | |||
195 | /** |
||
196 | * Get modules by status. |
||
197 | * |
||
198 | * @param $status |
||
199 | * |
||
200 | * @return array |
||
201 | */ |
||
202 | 181 | public function getByStatus($status) : array |
|
214 | |||
215 | /** |
||
216 | * Determine whether the given module exist. |
||
217 | * |
||
218 | * @param $name |
||
219 | * |
||
220 | * @return bool |
||
221 | */ |
||
222 | 92 | public function has($name) : bool |
|
226 | |||
227 | /** |
||
228 | * Get list of enabled modules. |
||
229 | * |
||
230 | * @return array |
||
231 | */ |
||
232 | 181 | public function allEnabled() : array |
|
236 | |||
237 | /** |
||
238 | * Get list of disabled modules. |
||
239 | * |
||
240 | * @return array |
||
241 | */ |
||
242 | 1 | public function allDisabled() : array |
|
246 | |||
247 | /** |
||
248 | * Get count from all modules. |
||
249 | * |
||
250 | * @return int |
||
251 | */ |
||
252 | 1 | public function count() : int |
|
256 | |||
257 | /** |
||
258 | * Get all ordered modules. |
||
259 | * |
||
260 | * @param string $direction |
||
261 | * |
||
262 | * @return array |
||
263 | */ |
||
264 | 181 | public function getOrdered($direction = 'asc') : array |
|
282 | |||
283 | /** |
||
284 | * Get a module path. |
||
285 | * |
||
286 | * @return string |
||
287 | */ |
||
288 | 181 | public function getPath() : string |
|
292 | |||
293 | /** |
||
294 | * Register the modules. |
||
295 | */ |
||
296 | 181 | public function register() |
|
302 | |||
303 | /** |
||
304 | * Boot the modules. |
||
305 | */ |
||
306 | 181 | public function boot() |
|
312 | |||
313 | /** |
||
314 | * Find a specific module. |
||
315 | * @param $name |
||
316 | * @return mixed|void |
||
317 | */ |
||
318 | 101 | public function find($name) |
|
328 | |||
329 | /** |
||
330 | * Find a specific module by its alias. |
||
331 | * @param $alias |
||
332 | * @return mixed|void |
||
333 | */ |
||
334 | 2 | public function findByAlias($alias) |
|
344 | |||
345 | /** |
||
346 | * Find all modules that are required by a module. If the module cannot be found, throw an exception. |
||
347 | * |
||
348 | * @param $name |
||
349 | * @return array |
||
350 | * @throws ModuleNotFoundException |
||
351 | */ |
||
352 | 1 | public function findRequirements($name) |
|
364 | |||
365 | /** |
||
366 | * Find a specific module, if there return that, otherwise throw exception. |
||
367 | * |
||
368 | * @param $name |
||
369 | * |
||
370 | * @return Module |
||
371 | * |
||
372 | * @throws ModuleNotFoundException |
||
373 | */ |
||
374 | 99 | public function findOrFail($name) |
|
384 | |||
385 | /** |
||
386 | * Get all modules as laravel collection instance. |
||
387 | * |
||
388 | * @param $status |
||
389 | * |
||
390 | * @return Collection |
||
391 | */ |
||
392 | 1 | public function collections($status = 1) : Collection |
|
396 | |||
397 | /** |
||
398 | * Get module path for a specific module. |
||
399 | * |
||
400 | * @param $module |
||
401 | * |
||
402 | * @return string |
||
403 | */ |
||
404 | 92 | public function getModulePath($module) |
|
412 | |||
413 | /** |
||
414 | * Get asset path for a specific module. |
||
415 | * |
||
416 | * @param $module |
||
417 | * |
||
418 | * @return string |
||
419 | */ |
||
420 | 2 | public function assetPath($module) : string |
|
424 | |||
425 | /** |
||
426 | * Get a specific config data from a configuration file. |
||
427 | * |
||
428 | * @param $key |
||
429 | * |
||
430 | * @param null $default |
||
431 | * @return mixed |
||
432 | */ |
||
433 | 181 | public function config($key, $default = null) |
|
437 | |||
438 | /** |
||
439 | * Get storage path for module used. |
||
440 | * |
||
441 | * @return string |
||
442 | */ |
||
443 | 2 | public function getUsedStoragePath() : string |
|
457 | |||
458 | /** |
||
459 | * Set module used for cli session. |
||
460 | * |
||
461 | * @param $name |
||
462 | * |
||
463 | * @throws ModuleNotFoundException |
||
464 | */ |
||
465 | 1 | public function setUsed($name) |
|
471 | |||
472 | /** |
||
473 | * Forget the module used for cli session. |
||
474 | */ |
||
475 | public function forgetUsed() |
||
481 | |||
482 | /** |
||
483 | * Get module used for cli session. |
||
484 | * @return string |
||
485 | * @throws \Nwidart\Modules\Exceptions\ModuleNotFoundException |
||
486 | */ |
||
487 | 1 | public function getUsedNow() : string |
|
491 | |||
492 | /** |
||
493 | * Get laravel filesystem instance. |
||
494 | * |
||
495 | * @return \Illuminate\Filesystem\Filesystem |
||
496 | */ |
||
497 | 4 | public function getFiles() |
|
501 | |||
502 | /** |
||
503 | * Get module assets path. |
||
504 | * |
||
505 | * @return string |
||
506 | */ |
||
507 | 2 | public function getAssetsPath() : string |
|
511 | |||
512 | /** |
||
513 | * Get asset url from a specific module. |
||
514 | * @param string $asset |
||
515 | * @return string |
||
516 | * @throws InvalidAssetPath |
||
517 | */ |
||
518 | 2 | public function asset($asset) : string |
|
531 | |||
532 | /** |
||
533 | * Determine whether the given module is activated. |
||
534 | * @param string $name |
||
535 | * @return bool |
||
536 | * @throws ModuleNotFoundException |
||
537 | */ |
||
538 | 4 | public function enabled($name) : bool |
|
542 | |||
543 | /** |
||
544 | * Determine whether the given module is not activated. |
||
545 | * @param string $name |
||
546 | * @return bool |
||
547 | * @throws ModuleNotFoundException |
||
548 | */ |
||
549 | 2 | public function disabled($name) : bool |
|
553 | |||
554 | /** |
||
555 | * Enabling a specific module. |
||
556 | * @param string $name |
||
557 | * @return void |
||
558 | * @throws \Nwidart\Modules\Exceptions\ModuleNotFoundException |
||
559 | */ |
||
560 | 1 | public function enable($name) |
|
564 | |||
565 | /** |
||
566 | * Disabling a specific module. |
||
567 | * @param string $name |
||
568 | * @return void |
||
569 | * @throws \Nwidart\Modules\Exceptions\ModuleNotFoundException |
||
570 | */ |
||
571 | 1 | public function disable($name) |
|
575 | |||
576 | /** |
||
577 | * Delete a specific module. |
||
578 | * @param string $name |
||
579 | * @return bool |
||
580 | * @throws \Nwidart\Modules\Exceptions\ModuleNotFoundException |
||
581 | */ |
||
582 | 2 | public function delete($name) : bool |
|
586 | |||
587 | /** |
||
588 | * Update dependencies for the specified module. |
||
589 | * |
||
590 | * @param string $module |
||
591 | */ |
||
592 | public function update($module) |
||
596 | |||
597 | /** |
||
598 | * Install the specified module. |
||
599 | * |
||
600 | * @param string $name |
||
601 | * @param string $version |
||
602 | * @param string $type |
||
603 | * @param bool $subtree |
||
604 | * |
||
605 | * @return \Symfony\Component\Process\Process |
||
606 | */ |
||
607 | public function install($name, $version = 'dev-master', $type = 'composer', $subtree = false) |
||
613 | |||
614 | /** |
||
615 | * Get stub path. |
||
616 | * |
||
617 | * @return string|null |
||
618 | */ |
||
619 | 3 | public function getStubPath() |
|
631 | |||
632 | /** |
||
633 | * Set stub path. |
||
634 | * |
||
635 | * @param string $stubPath |
||
636 | * |
||
637 | * @return $this |
||
638 | */ |
||
639 | 1 | public function setStubPath($stubPath) |
|
645 | } |
||
646 |
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..