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 |
||
13 | class Repository implements RepositoryInterface, Countable |
||
14 | { |
||
15 | /** |
||
16 | * Application instance. |
||
17 | * |
||
18 | * @var Application |
||
19 | */ |
||
20 | protected $app; |
||
21 | |||
22 | /** |
||
23 | * The module path. |
||
24 | * |
||
25 | * @var string|null |
||
26 | */ |
||
27 | protected $path; |
||
28 | |||
29 | /** |
||
30 | * The scanned paths. |
||
31 | * |
||
32 | * @var array |
||
33 | */ |
||
34 | protected $paths = []; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $stubPath; |
||
40 | |||
41 | /** |
||
42 | * The constructor. |
||
43 | * |
||
44 | * @param Application $app |
||
45 | * @param string|null $path |
||
46 | */ |
||
47 | 55 | public function __construct(Application $app, $path = null) |
|
52 | |||
53 | /** |
||
54 | * Add other module location. |
||
55 | * |
||
56 | * @param string $path |
||
57 | * |
||
58 | * @return $this |
||
59 | */ |
||
60 | 10 | public function addLocation($path) |
|
66 | |||
67 | /** |
||
68 | * Alternative method for "addPath". |
||
69 | * |
||
70 | * @param string $path |
||
71 | * |
||
72 | * @return $this |
||
73 | */ |
||
74 | 1 | public function addPath($path) |
|
78 | |||
79 | /** |
||
80 | * Get all additional paths. |
||
81 | * |
||
82 | * @return array |
||
83 | */ |
||
84 | 1 | public function getPaths() |
|
88 | |||
89 | /** |
||
90 | * Get scanned modules paths. |
||
91 | * |
||
92 | * @return array |
||
93 | */ |
||
94 | 55 | public function getScanPaths() |
|
106 | |||
107 | /** |
||
108 | * Get & scan all modules. |
||
109 | * |
||
110 | * @return array |
||
111 | */ |
||
112 | 55 | public function scan() |
|
134 | |||
135 | /** |
||
136 | * Get all modules. |
||
137 | * |
||
138 | * @return array |
||
139 | */ |
||
140 | 55 | public function all() |
|
148 | |||
149 | /** |
||
150 | * Format the cached data as array of modules. |
||
151 | * |
||
152 | * @param array $cached |
||
153 | * |
||
154 | * @return array |
||
155 | */ |
||
156 | protected function formatCached($cached) |
||
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 | 55 | public function getByStatus($status) |
|
212 | |||
213 | /** |
||
214 | * Determine whether the given module exist. |
||
215 | * |
||
216 | * @param $name |
||
217 | * |
||
218 | * @return bool |
||
219 | */ |
||
220 | 16 | public function has($name) |
|
224 | |||
225 | /** |
||
226 | * Get list of enabled modules. |
||
227 | * |
||
228 | * @return array |
||
229 | */ |
||
230 | 55 | 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 | 55 | public function getOrdered($direction = 'asc') |
|
280 | |||
281 | /** |
||
282 | * Get a module path. |
||
283 | * |
||
284 | * @return string |
||
285 | */ |
||
286 | 55 | public function getPath() |
|
290 | |||
291 | /** |
||
292 | * Register the modules. |
||
293 | */ |
||
294 | 55 | public function register() |
|
300 | |||
301 | /** |
||
302 | * Boot the modules. |
||
303 | */ |
||
304 | 55 | public function boot() |
|
310 | |||
311 | /** |
||
312 | * Find a specific module. |
||
313 | * |
||
314 | * @param $name |
||
315 | */ |
||
316 | 21 | public function find($name) |
|
326 | |||
327 | /** |
||
328 | * Alternative for "find" method. |
||
329 | * |
||
330 | * @param $name |
||
331 | */ |
||
332 | 1 | public function get($name) |
|
336 | |||
337 | /** |
||
338 | * Find a specific module, if there return that, otherwise throw exception. |
||
339 | * |
||
340 | * @param $name |
||
341 | * |
||
342 | * @return Module |
||
343 | * |
||
344 | * @throws ModuleNotFoundException |
||
345 | */ |
||
346 | 20 | public function findOrFail($name) |
|
354 | |||
355 | /** |
||
356 | * Get all modules as laravel collection instance. |
||
357 | * |
||
358 | * @return Collection |
||
359 | */ |
||
360 | 1 | public function collections() |
|
364 | |||
365 | /** |
||
366 | * Get module path for a specific module. |
||
367 | * |
||
368 | * @param $module |
||
369 | * |
||
370 | * @return string |
||
371 | */ |
||
372 | 16 | public function getModulePath($module) |
|
380 | |||
381 | /** |
||
382 | * Get asset path for a specific module. |
||
383 | * |
||
384 | * @param $module |
||
385 | * |
||
386 | * @return string |
||
387 | */ |
||
388 | 1 | public function assetPath($module) |
|
392 | |||
393 | /** |
||
394 | * Get a specific config data from a configuration file. |
||
395 | * |
||
396 | * @param $key |
||
397 | * |
||
398 | * @return mixed |
||
399 | */ |
||
400 | 55 | public function config($key) |
|
404 | |||
405 | /** |
||
406 | * Get storage path for module used. |
||
407 | * |
||
408 | * @return string |
||
409 | */ |
||
410 | 2 | public function getUsedStoragePath() |
|
418 | |||
419 | /** |
||
420 | * Set module used for cli session. |
||
421 | * |
||
422 | * @param $name |
||
423 | * |
||
424 | * @throws ModuleNotFoundException |
||
425 | */ |
||
426 | 1 | public function setUsed($name) |
|
432 | |||
433 | /** |
||
434 | * Get module used for cli session. |
||
435 | * |
||
436 | * @return string |
||
437 | */ |
||
438 | 1 | public function getUsedNow() |
|
442 | |||
443 | /** |
||
444 | * Get used now. |
||
445 | * |
||
446 | * @return string |
||
447 | */ |
||
448 | 1 | public function getUsed() |
|
452 | |||
453 | /** |
||
454 | * Get laravel filesystem instance. |
||
455 | * |
||
456 | * @return \Illuminate\Filesystem\Filesystem |
||
457 | */ |
||
458 | 1 | public function getFiles() |
|
462 | |||
463 | /** |
||
464 | * Get module assets path. |
||
465 | * |
||
466 | * @return string |
||
467 | */ |
||
468 | 2 | public function getAssetsPath() |
|
472 | |||
473 | /** |
||
474 | * Get asset url from a specific module. |
||
475 | * |
||
476 | * @param string $asset |
||
477 | * @param bool $secure |
||
478 | * |
||
479 | * @return string |
||
480 | */ |
||
481 | 1 | public function asset($asset) |
|
491 | |||
492 | /** |
||
493 | * Determine whether the given module is activated. |
||
494 | * |
||
495 | * @param string $name |
||
496 | * |
||
497 | * @return bool |
||
498 | */ |
||
499 | 2 | public function active($name) |
|
503 | |||
504 | /** |
||
505 | * Determine whether the given module is not activated. |
||
506 | * |
||
507 | * @param string $name |
||
508 | * |
||
509 | * @return bool |
||
510 | */ |
||
511 | 1 | public function notActive($name) |
|
515 | |||
516 | /** |
||
517 | * Enabling a specific module. |
||
518 | * |
||
519 | * @param string $name |
||
520 | * |
||
521 | * @return bool |
||
522 | */ |
||
523 | public function enable($name) |
||
527 | |||
528 | /** |
||
529 | * Disabling a specific module. |
||
530 | * |
||
531 | * @param string $name |
||
532 | * |
||
533 | * @return bool |
||
534 | */ |
||
535 | public function disable($name) |
||
539 | |||
540 | /** |
||
541 | * Delete a specific module. |
||
542 | * |
||
543 | * @param string $name |
||
544 | * |
||
545 | * @return bool |
||
546 | */ |
||
547 | public function delete($name) |
||
551 | |||
552 | /** |
||
553 | * Update dependencies for the specified module. |
||
554 | * |
||
555 | * @param string $module |
||
556 | */ |
||
557 | public function update($module) |
||
561 | |||
562 | /** |
||
563 | * Install the specified module. |
||
564 | * |
||
565 | * @param string $name |
||
566 | * @param string $version |
||
567 | * @param string $type |
||
568 | * @param bool $subtree |
||
569 | * |
||
570 | * @return \Symfony\Component\Process\Process |
||
571 | */ |
||
572 | public function install($name, $version = 'dev-master', $type = 'composer', $subtree = false) |
||
578 | |||
579 | /** |
||
580 | * Get stub path. |
||
581 | * |
||
582 | * @return string |
||
583 | */ |
||
584 | 3 | public function getStubPath() |
|
596 | |||
597 | /** |
||
598 | * Set stub path. |
||
599 | * |
||
600 | * @param string $stubPath |
||
601 | * |
||
602 | * @return $this |
||
603 | */ |
||
604 | 1 | public function setStubPath($stubPath) |
|
610 | } |
||
611 |
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.