Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Module 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 Module, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | abstract class Module |
||
15 | { |
||
16 | use Macroable; |
||
17 | |||
18 | /** |
||
19 | * The laravel|lumen application instance. |
||
20 | * |
||
21 | * @var \Illuminate\Contracts\Foundation\Application|\Laravel\Lumen\Application |
||
22 | */ |
||
23 | protected $app; |
||
24 | |||
25 | /** |
||
26 | * The module name. |
||
27 | * |
||
28 | * @var |
||
29 | */ |
||
30 | protected $name; |
||
31 | |||
32 | /** |
||
33 | * The module path. |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | protected $path; |
||
38 | |||
39 | /** |
||
40 | * @var array of cached Json objects, keyed by filename |
||
41 | */ |
||
42 | protected $moduleJson = []; |
||
43 | /** |
||
44 | * @var CacheManager |
||
45 | */ |
||
46 | private $cache; |
||
47 | /** |
||
48 | * @var Filesystem |
||
49 | */ |
||
50 | private $files; |
||
51 | /** |
||
52 | * @var Translator |
||
53 | */ |
||
54 | private $translator; |
||
55 | |||
56 | /** |
||
57 | * The constructor. |
||
58 | * @param Container $app |
||
59 | * @param $name |
||
60 | * @param $path |
||
61 | */ |
||
62 | 177 | View Code Duplication | public function __construct(Container $app, string $name, $path) |
72 | |||
73 | /** |
||
74 | * Get name. |
||
75 | * |
||
76 | * @return string |
||
77 | */ |
||
78 | 26 | public function getName() |
|
82 | |||
83 | /** |
||
84 | * Get name in lower case. |
||
85 | * |
||
86 | * @return string |
||
87 | */ |
||
88 | 134 | public function getLowerName() |
|
92 | |||
93 | /** |
||
94 | * Get name in studly case. |
||
95 | * |
||
96 | * @return string |
||
97 | */ |
||
98 | 120 | public function getStudlyName() |
|
102 | |||
103 | /** |
||
104 | * Get name in snake case. |
||
105 | * |
||
106 | * @return string |
||
107 | */ |
||
108 | 6 | public function getSnakeName() |
|
112 | |||
113 | /** |
||
114 | * Get description. |
||
115 | * |
||
116 | * @return string |
||
117 | */ |
||
118 | 2 | public function getDescription() |
|
122 | |||
123 | /** |
||
124 | * Get alias. |
||
125 | * |
||
126 | * @return string |
||
127 | */ |
||
128 | 4 | public function getAlias() |
|
132 | |||
133 | /** |
||
134 | * Get priority. |
||
135 | * |
||
136 | * @return string |
||
137 | */ |
||
138 | public function getPriority() |
||
142 | |||
143 | /** |
||
144 | * Get module requirements. |
||
145 | * |
||
146 | * @return array |
||
147 | */ |
||
148 | 3 | public function getRequires() |
|
152 | |||
153 | /** |
||
154 | * Get path. |
||
155 | * |
||
156 | * @return string |
||
157 | */ |
||
158 | 140 | public function getPath() |
|
162 | |||
163 | /** |
||
164 | * Set path. |
||
165 | * |
||
166 | * @param string $path |
||
167 | * |
||
168 | * @return $this |
||
169 | */ |
||
170 | public function setPath($path) |
||
176 | |||
177 | /** |
||
178 | * Bootstrap the application events. |
||
179 | */ |
||
180 | 2 | public function boot() |
|
192 | |||
193 | /** |
||
194 | * Register module's translation. |
||
195 | * |
||
196 | * @return void |
||
197 | */ |
||
198 | 2 | protected function registerTranslation() |
|
208 | |||
209 | /** |
||
210 | * Get json contents from the cache, setting as needed. |
||
211 | * |
||
212 | * @param string $file |
||
213 | * |
||
214 | * @return Json |
||
215 | */ |
||
216 | 20 | public function json($file = null) : Json |
|
226 | |||
227 | /** |
||
228 | * Get a specific data from json file by given the key. |
||
229 | * |
||
230 | * @param string $key |
||
231 | * @param null $default |
||
232 | * |
||
233 | * @return mixed |
||
234 | */ |
||
235 | 13 | public function get(string $key, $default = null) |
|
239 | |||
240 | /** |
||
241 | * Get a specific data from composer.json file by given the key. |
||
242 | * |
||
243 | * @param $key |
||
244 | * @param null $default |
||
245 | * |
||
246 | * @return mixed |
||
247 | */ |
||
248 | 2 | public function getComposerAttr($key, $default = null) |
|
252 | |||
253 | /** |
||
254 | * Register the module. |
||
255 | */ |
||
256 | public function register() |
||
268 | |||
269 | /** |
||
270 | * Register the module event. |
||
271 | * |
||
272 | * @param string $event |
||
273 | */ |
||
274 | 10 | protected function fireEvent($event) |
|
278 | /** |
||
279 | * Register the aliases from this module. |
||
280 | */ |
||
281 | abstract public function registerAliases(); |
||
282 | |||
283 | /** |
||
284 | * Register the service providers from this module. |
||
285 | */ |
||
286 | abstract public function registerProviders(); |
||
287 | |||
288 | /** |
||
289 | * Get the path to the cached *_module.php file. |
||
290 | * |
||
291 | * @return string |
||
292 | */ |
||
293 | abstract public function getCachedServicesPath(); |
||
294 | |||
295 | /** |
||
296 | * Register the files from this module. |
||
297 | */ |
||
298 | protected function registerFiles() |
||
304 | |||
305 | /** |
||
306 | * Handle call __toString. |
||
307 | * |
||
308 | * @return string |
||
309 | */ |
||
310 | 3 | public function __toString() |
|
314 | |||
315 | /** |
||
316 | * Determine whether the given status same with the current module status. |
||
317 | * |
||
318 | * @param bool $status |
||
319 | * |
||
320 | * @return bool |
||
321 | */ |
||
322 | 5 | public function isStatus(bool $status) : bool |
|
326 | |||
327 | /** |
||
328 | * Determine whether the current module activated. |
||
329 | * |
||
330 | * @return bool |
||
331 | */ |
||
332 | 8 | public function enabled() : bool |
|
336 | |||
337 | /** |
||
338 | * Determine whether the current module not disabled. |
||
339 | * |
||
340 | * @return bool |
||
341 | */ |
||
342 | 2 | public function disabled() : bool |
|
346 | |||
347 | /** |
||
348 | * Set active state for current module. |
||
349 | * |
||
350 | * @param bool $active |
||
351 | * |
||
352 | * @return bool |
||
353 | */ |
||
354 | public function setActive(bool $active) |
||
358 | |||
359 | /** |
||
360 | * Disable the current module. |
||
361 | */ |
||
362 | 4 | public function disable() |
|
371 | |||
372 | /** |
||
373 | * Enable the current module. |
||
374 | */ |
||
375 | 4 | public function enable() |
|
384 | |||
385 | /** |
||
386 | * Delete the current module. |
||
387 | * |
||
388 | * @return bool |
||
389 | */ |
||
390 | 2 | public function delete() |
|
395 | |||
396 | /** |
||
397 | * Get extra path. |
||
398 | * |
||
399 | * @param string $path |
||
400 | * |
||
401 | * @return string |
||
402 | */ |
||
403 | 3 | public function getExtraPath(string $path) : string |
|
407 | |||
408 | /** |
||
409 | * Handle call to __get method. |
||
410 | * |
||
411 | * @param $key |
||
412 | * |
||
413 | * @return mixed |
||
414 | */ |
||
415 | public function __get($key) |
||
419 | |||
420 | /** |
||
421 | * Check if can load files of module on boot method. |
||
422 | * |
||
423 | * @return bool |
||
424 | */ |
||
425 | 2 | protected function isLoadFilesOnBoot() |
|
431 | |||
432 | 8 | private function flushCache(): void |
|
438 | |||
439 | /** |
||
440 | * Register a translation file namespace. |
||
441 | * |
||
442 | * @param string $path |
||
443 | * @param string $namespace |
||
444 | * @return void |
||
445 | */ |
||
446 | 2 | private function loadTranslationsFrom(string $path, string $namespace): void |
|
450 | } |
||
451 |
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..