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 | class Repository implements RepositoryInterface, Countable |
||
16 | { |
||
17 | use Macroable; |
||
18 | |||
19 | /** |
||
20 | * Application instance. |
||
21 | * |
||
22 | * @var 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 Application $app |
||
49 | * @param string|null $path |
||
50 | */ |
||
51 | 111 | public function __construct(Application $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 | 111 | public function getScanPaths() |
|
99 | { |
||
100 | 111 | $paths = $this->paths; |
|
101 | |||
102 | 111 | $paths[] = $this->getPath() . '/*'; |
|
103 | |||
104 | 111 | if ($this->config('scan.enabled')) { |
|
105 | $paths = array_merge($paths, $this->config('scan.paths')); |
||
106 | } |
||
107 | |||
108 | 111 | return $paths; |
|
109 | } |
||
110 | |||
111 | /** |
||
112 | * Get & scan all modules. |
||
113 | * |
||
114 | * @return array |
||
115 | */ |
||
116 | 111 | public function scan() |
|
117 | { |
||
118 | 111 | $paths = $this->getScanPaths(); |
|
119 | |||
120 | 111 | $modules = []; |
|
121 | |||
122 | 111 | foreach ($paths as $key => $path) { |
|
123 | 111 | $manifests = $this->app['files']->glob("{$path}/module.json"); |
|
124 | |||
125 | 111 | is_array($manifests) || $manifests = []; |
|
126 | |||
127 | 111 | foreach ($manifests as $manifest) { |
|
128 | 68 | $name = Json::make($manifest)->get('name'); |
|
129 | |||
130 | 68 | $modules[$name] = new Module($this->app, $name, dirname($manifest)); |
|
131 | 111 | } |
|
132 | 111 | } |
|
133 | |||
134 | 111 | return $modules; |
|
135 | } |
||
136 | |||
137 | /** |
||
138 | * Get all modules. |
||
139 | * |
||
140 | * @return array |
||
141 | */ |
||
142 | 111 | public function all() |
|
150 | |||
151 | /** |
||
152 | * Format the cached data as array of modules. |
||
153 | * |
||
154 | * @param array $cached |
||
155 | * |
||
156 | * @return array |
||
157 | */ |
||
158 | protected function formatCached($cached) |
||
159 | { |
||
160 | $modules = []; |
||
161 | |||
162 | foreach ($cached as $name => $module) { |
||
163 | $path = $this->config('paths.modules') . '/' . $name; |
||
164 | |||
165 | $modules[$name] = new Module($this->app, $name, $path); |
||
166 | } |
||
167 | |||
168 | return $modules; |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * Get cached modules. |
||
173 | * |
||
174 | * @return array |
||
175 | */ |
||
176 | public function getCached() |
||
182 | |||
183 | /** |
||
184 | * Get all modules as collection instance. |
||
185 | * |
||
186 | * @return Collection |
||
187 | */ |
||
188 | 1 | public function toCollection() |
|
192 | |||
193 | /** |
||
194 | * Get modules by status. |
||
195 | * |
||
196 | * @param $status |
||
197 | * |
||
198 | * @return array |
||
199 | */ |
||
200 | 111 | public function getByStatus($status) |
|
201 | { |
||
202 | 111 | $modules = []; |
|
203 | |||
204 | 111 | foreach ($this->all() as $name => $module) { |
|
205 | 3 | if ($module->isStatus($status)) { |
|
206 | 2 | $modules[$name] = $module; |
|
207 | 2 | } |
|
208 | 111 | } |
|
209 | |||
210 | 111 | return $modules; |
|
211 | } |
||
212 | |||
213 | /** |
||
214 | * Determine whether the given module exist. |
||
215 | * |
||
216 | * @param $name |
||
217 | * |
||
218 | * @return bool |
||
219 | */ |
||
220 | 55 | public function has($name) |
|
224 | |||
225 | /** |
||
226 | * Get list of enabled modules. |
||
227 | * |
||
228 | * @return array |
||
229 | */ |
||
230 | 111 | public function enabled() |
|
234 | |||
235 | /** |
||
236 | * Get list of disabled modules. |
||
237 | * |
||
238 | * @return array |
||
239 | */ |
||
240 | 1 | public function disabled() |
|
244 | |||
245 | /** |
||
246 | * Get count from all modules. |
||
247 | * |
||
248 | * @return int |
||
249 | */ |
||
250 | 1 | public function count() |
|
254 | |||
255 | /** |
||
256 | * Get all ordered modules. |
||
257 | * |
||
258 | * @param string $direction |
||
259 | * |
||
260 | * @return array |
||
261 | */ |
||
262 | 111 | public function getOrdered($direction = 'asc') |
|
280 | |||
281 | /** |
||
282 | * Get a module path. |
||
283 | * |
||
284 | * @return string |
||
285 | */ |
||
286 | 111 | public function getPath() |
|
290 | |||
291 | /** |
||
292 | * Register the modules. |
||
293 | */ |
||
294 | 111 | public function register() |
|
295 | { |
||
296 | 111 | foreach ($this->getOrdered() as $module) { |
|
297 | $module->register(); |
||
298 | 111 | } |
|
299 | 111 | } |
|
300 | |||
301 | /** |
||
302 | * Boot the modules. |
||
303 | */ |
||
304 | 111 | public function boot() |
|
305 | { |
||
306 | 111 | foreach ($this->getOrdered() as $module) { |
|
307 | $module->boot(); |
||
308 | 111 | } |
|
309 | 111 | } |
|
310 | |||
311 | /** |
||
312 | * Find a specific module. |
||
313 | * @param $name |
||
314 | * @return mixed|void |
||
315 | */ |
||
316 | 64 | public function find($name) |
|
317 | { |
||
318 | 64 | foreach ($this->all() as $module) { |
|
319 | 63 | if ($module->getLowerName() === strtolower($name)) { |
|
320 | 63 | return $module; |
|
321 | } |
||
322 | 56 | } |
|
323 | |||
324 | 56 | return; |
|
325 | } |
||
326 | |||
327 | /** |
||
328 | * Find a specific module by its alias. |
||
329 | * @param $alias |
||
330 | * @return mixed|void |
||
331 | */ |
||
332 | 2 | public function findByAlias($alias) |
|
333 | { |
||
334 | 2 | foreach ($this->all() as $module) { |
|
335 | 2 | if ($module->getAlias() === $alias) { |
|
336 | 2 | return $module; |
|
337 | } |
||
338 | 2 | } |
|
339 | |||
340 | return; |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * Find all modules that are required by a module. If the module cannot be found, throw an exception. |
||
345 | * |
||
346 | * @param $name |
||
347 | * @return array |
||
348 | * @throws ModuleNotFoundException |
||
349 | */ |
||
350 | 1 | public function findRequirements($name) |
|
351 | { |
||
352 | 1 | $requirements = []; |
|
353 | |||
354 | 1 | $module = $this->findOrFail($name); |
|
355 | |||
356 | 1 | foreach ($module->getRequires() as $requirementName) { |
|
357 | 1 | $requirements[] = $this->findByAlias($requirementName); |
|
358 | 1 | } |
|
359 | |||
360 | 1 | return $requirements; |
|
361 | } |
||
362 | |||
363 | /** |
||
364 | * Alternative for "find" method. |
||
365 | * @param $name |
||
366 | * @return mixed|void |
||
367 | */ |
||
368 | 1 | public function get($name) |
|
369 | 1 | { |
|
370 | 1 | return $this->find($name); |
|
371 | } |
||
372 | |||
373 | /** |
||
374 | * Find a specific module, if there return that, otherwise throw exception. |
||
375 | * |
||
376 | * @param $name |
||
377 | * |
||
378 | * @return Module |
||
379 | * |
||
380 | * @throws ModuleNotFoundException |
||
381 | */ |
||
382 | 62 | public function findOrFail($name) |
|
392 | |||
393 | /** |
||
394 | * Get all modules as laravel collection instance. |
||
395 | * |
||
396 | * @param $status |
||
397 | * |
||
398 | * @return Collection |
||
399 | */ |
||
400 | 1 | public function collections($status = 1) |
|
404 | |||
405 | /** |
||
406 | * Get module path for a specific module. |
||
407 | * |
||
408 | * @param $module |
||
409 | * |
||
410 | * @return string |
||
411 | */ |
||
412 | 55 | public function getModulePath($module) |
|
420 | |||
421 | /** |
||
422 | * Get asset path for a specific module. |
||
423 | * |
||
424 | * @param $module |
||
425 | * |
||
426 | * @return string |
||
427 | */ |
||
428 | 2 | public function assetPath($module) |
|
432 | |||
433 | /** |
||
434 | * Get a specific config data from a configuration file. |
||
435 | * |
||
436 | * @param $key |
||
437 | * |
||
438 | * @param null $default |
||
439 | * @return mixed |
||
440 | */ |
||
441 | 111 | public function config($key, $default = null) |
|
445 | |||
446 | /** |
||
447 | * Get storage path for module used. |
||
448 | * |
||
449 | * @return string |
||
450 | */ |
||
451 | 2 | public function getUsedStoragePath() |
|
452 | { |
||
453 | 2 | $directory = storage_path('app/modules'); |
|
454 | 2 | if ($this->app['files']->exists($directory) === false) { |
|
455 | 1 | $this->app['files']->makeDirectory($directory, 0777, true); |
|
456 | 1 | } |
|
457 | |||
458 | 2 | $path = storage_path('app/modules/modules.used'); |
|
459 | 2 | if (!$this->app['files']->exists($path)) { |
|
460 | 1 | $this->app['files']->put($path, ''); |
|
461 | 1 | } |
|
462 | |||
463 | 2 | return $path; |
|
464 | } |
||
465 | |||
466 | /** |
||
467 | * Set module used for cli session. |
||
468 | * |
||
469 | * @param $name |
||
470 | * |
||
471 | * @throws ModuleNotFoundException |
||
472 | */ |
||
473 | 1 | public function setUsed($name) |
|
479 | |||
480 | /** |
||
481 | * Get module used for cli session. |
||
482 | * |
||
483 | * @return string |
||
484 | */ |
||
485 | 1 | public function getUsedNow() |
|
489 | |||
490 | /** |
||
491 | * Get used now. |
||
492 | * |
||
493 | * @return string |
||
494 | */ |
||
495 | 1 | public function getUsed() |
|
499 | |||
500 | /** |
||
501 | * Get laravel filesystem instance. |
||
502 | * |
||
503 | * @return \Illuminate\Filesystem\Filesystem |
||
504 | */ |
||
505 | 4 | public function getFiles() |
|
509 | |||
510 | /** |
||
511 | * Get module assets path. |
||
512 | * |
||
513 | * @return string |
||
514 | */ |
||
515 | 2 | public function getAssetsPath() |
|
519 | |||
520 | /** |
||
521 | * Get asset url from a specific module. |
||
522 | * |
||
523 | * @param string $asset |
||
524 | * |
||
525 | * @return string |
||
526 | */ |
||
527 | 2 | public function asset($asset) |
|
540 | |||
541 | /** |
||
542 | * Determine whether the given module is activated. |
||
543 | * |
||
544 | * @param string $name |
||
545 | * |
||
546 | * @return bool |
||
547 | */ |
||
548 | 4 | public function active($name) |
|
552 | |||
553 | /** |
||
554 | * Determine whether the given module is not activated. |
||
555 | * |
||
556 | * @param string $name |
||
557 | * |
||
558 | * @return bool |
||
559 | */ |
||
560 | 2 | public function notActive($name) |
|
564 | |||
565 | /** |
||
566 | * Enabling a specific module. |
||
567 | * |
||
568 | * @param string $name |
||
569 | * |
||
570 | * @return bool |
||
571 | */ |
||
572 | 1 | public function enable($name) |
|
576 | |||
577 | /** |
||
578 | * Disabling a specific module. |
||
579 | * |
||
580 | * @param string $name |
||
581 | * |
||
582 | * @return bool |
||
583 | */ |
||
584 | 1 | public function disable($name) |
|
588 | |||
589 | /** |
||
590 | * Delete a specific module. |
||
591 | * |
||
592 | * @param string $name |
||
593 | * |
||
594 | * @return bool |
||
595 | */ |
||
596 | 2 | public function delete($name) |
|
600 | |||
601 | /** |
||
602 | * Update dependencies for the specified module. |
||
603 | * |
||
604 | * @param string $module |
||
605 | */ |
||
606 | public function update($module) |
||
610 | |||
611 | /** |
||
612 | * Install the specified module. |
||
613 | * |
||
614 | * @param string $name |
||
615 | * @param string $version |
||
616 | * @param string $type |
||
617 | * @param bool $subtree |
||
618 | * |
||
619 | * @return \Symfony\Component\Process\Process |
||
620 | */ |
||
621 | public function install($name, $version = 'dev-master', $type = 'composer', $subtree = false) |
||
627 | |||
628 | /** |
||
629 | * Get stub path. |
||
630 | * |
||
631 | * @return string |
||
632 | */ |
||
633 | 3 | public function getStubPath() |
|
645 | |||
646 | /** |
||
647 | * Set stub path. |
||
648 | * |
||
649 | * @param string $stubPath |
||
650 | * |
||
651 | * @return $this |
||
652 | */ |
||
653 | 1 | public function setStubPath($stubPath) |
|
659 | } |
||
660 |
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.