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 |
||
13 | abstract class Module |
||
14 | { |
||
15 | use Macroable; |
||
16 | |||
17 | /** |
||
18 | * The laravel|lumen application instance. |
||
19 | * |
||
20 | * @var \Illuminate\Contracts\Foundation\Application|\Laravel\Lumen\Application |
||
21 | */ |
||
22 | protected $app; |
||
23 | |||
24 | /** |
||
25 | * The module name. |
||
26 | * |
||
27 | * @var |
||
28 | */ |
||
29 | protected $name; |
||
30 | |||
31 | /** |
||
32 | * The module path. |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $path; |
||
37 | |||
38 | /** |
||
39 | * @var array of cached Json objects, keyed by filename |
||
40 | */ |
||
41 | protected $moduleJson = []; |
||
42 | /** |
||
43 | * @var CacheManager |
||
44 | */ |
||
45 | private $cache; |
||
46 | /** |
||
47 | * @var Filesystem |
||
48 | */ |
||
49 | private $files; |
||
50 | /** |
||
51 | * @var Translator |
||
52 | */ |
||
53 | private $translator; |
||
54 | |||
55 | /** |
||
56 | * The constructor. |
||
57 | * @param Container $app |
||
58 | * @param $name |
||
59 | * @param $path |
||
60 | */ |
||
61 | 153 | View Code Duplication | public function __construct(Container $app, string $name, $path) |
70 | |||
71 | /** |
||
72 | * Get name. |
||
73 | * |
||
74 | * @return string |
||
75 | */ |
||
76 | 2 | public function getName() |
|
80 | |||
81 | /** |
||
82 | * Get name in lower case. |
||
83 | * |
||
84 | * @return string |
||
85 | */ |
||
86 | 115 | public function getLowerName() |
|
90 | |||
91 | /** |
||
92 | * Get name in studly case. |
||
93 | * |
||
94 | * @return string |
||
95 | */ |
||
96 | 101 | public function getStudlyName() |
|
100 | |||
101 | /** |
||
102 | * Get name in snake case. |
||
103 | * |
||
104 | * @return string |
||
105 | */ |
||
106 | 6 | public function getSnakeName() |
|
110 | |||
111 | /** |
||
112 | * Get description. |
||
113 | * |
||
114 | * @return string |
||
115 | */ |
||
116 | 2 | public function getDescription() |
|
120 | |||
121 | /** |
||
122 | * Get alias. |
||
123 | * |
||
124 | * @return string |
||
125 | */ |
||
126 | 4 | public function getAlias() |
|
130 | |||
131 | /** |
||
132 | * Get priority. |
||
133 | * |
||
134 | * @return string |
||
135 | */ |
||
136 | public function getPriority() |
||
140 | |||
141 | /** |
||
142 | * Get module requirements. |
||
143 | * |
||
144 | * @return array |
||
145 | */ |
||
146 | 3 | public function getRequires() |
|
150 | |||
151 | /** |
||
152 | * Get path. |
||
153 | * |
||
154 | * @return string |
||
155 | */ |
||
156 | 136 | public function getPath() |
|
160 | |||
161 | /** |
||
162 | * Set path. |
||
163 | * |
||
164 | * @param string $path |
||
165 | * |
||
166 | * @return $this |
||
167 | */ |
||
168 | public function setPath($path) |
||
174 | |||
175 | /** |
||
176 | * Bootstrap the application events. |
||
177 | */ |
||
178 | 2 | public function boot() |
|
190 | |||
191 | /** |
||
192 | * Register module's translation. |
||
193 | * |
||
194 | * @return void |
||
195 | */ |
||
196 | 2 | protected function registerTranslation() |
|
206 | |||
207 | /** |
||
208 | * Get json contents from the cache, setting as needed. |
||
209 | * |
||
210 | * @param string $file |
||
211 | * |
||
212 | * @return Json |
||
213 | */ |
||
214 | 35 | public function json($file = null) : Json |
|
224 | |||
225 | /** |
||
226 | * Get a specific data from json file by given the key. |
||
227 | * |
||
228 | * @param string $key |
||
229 | * @param null $default |
||
230 | * |
||
231 | * @return mixed |
||
232 | */ |
||
233 | 24 | public function get(string $key, $default = null) |
|
237 | |||
238 | /** |
||
239 | * Get a specific data from composer.json file by given the key. |
||
240 | * |
||
241 | * @param $key |
||
242 | * @param null $default |
||
243 | * |
||
244 | * @return mixed |
||
245 | */ |
||
246 | 2 | public function getComposerAttr($key, $default = null) |
|
250 | |||
251 | /** |
||
252 | * Register the module. |
||
253 | */ |
||
254 | public function register() |
||
266 | |||
267 | /** |
||
268 | * Register the module event. |
||
269 | * |
||
270 | * @param string $event |
||
271 | */ |
||
272 | 8 | protected function fireEvent($event) |
|
276 | /** |
||
277 | * Register the aliases from this module. |
||
278 | */ |
||
279 | abstract public function registerAliases(); |
||
280 | |||
281 | /** |
||
282 | * Register the service providers from this module. |
||
283 | */ |
||
284 | abstract public function registerProviders(); |
||
285 | |||
286 | /** |
||
287 | * Get the path to the cached *_module.php file. |
||
288 | * |
||
289 | * @return string |
||
290 | */ |
||
291 | abstract public function getCachedServicesPath(); |
||
292 | |||
293 | /** |
||
294 | * Register the files from this module. |
||
295 | */ |
||
296 | protected function registerFiles() |
||
302 | |||
303 | /** |
||
304 | * Handle call __toString. |
||
305 | * |
||
306 | * @return string |
||
307 | */ |
||
308 | 3 | public function __toString() |
|
312 | |||
313 | /** |
||
314 | * Determine whether the given status same with the current module status. |
||
315 | * |
||
316 | * @param $status |
||
317 | * |
||
318 | * @return bool |
||
319 | */ |
||
320 | 11 | public function isStatus($status) : bool |
|
324 | |||
325 | /** |
||
326 | * Determine whether the current module activated. |
||
327 | * |
||
328 | * @return bool |
||
329 | */ |
||
330 | 6 | public function enabled() : bool |
|
334 | |||
335 | /** |
||
336 | * Determine whether the current module not disabled. |
||
337 | * |
||
338 | * @return bool |
||
339 | */ |
||
340 | 2 | public function disabled() : bool |
|
344 | |||
345 | /** |
||
346 | * Set active state for current module. |
||
347 | * |
||
348 | * @param $active |
||
349 | * |
||
350 | * @return bool |
||
351 | */ |
||
352 | 6 | public function setActive($active) |
|
356 | |||
357 | /** |
||
358 | * Disable the current module. |
||
359 | */ |
||
360 | 3 | public function disable() |
|
369 | |||
370 | /** |
||
371 | * Enable the current module. |
||
372 | */ |
||
373 | 3 | public function enable() |
|
382 | |||
383 | /** |
||
384 | * Delete the current module. |
||
385 | * |
||
386 | * @return bool |
||
387 | */ |
||
388 | 2 | public function delete() |
|
392 | |||
393 | /** |
||
394 | * Get extra path. |
||
395 | * |
||
396 | * @param string $path |
||
397 | * |
||
398 | * @return string |
||
399 | */ |
||
400 | 3 | public function getExtraPath(string $path) : string |
|
404 | |||
405 | /** |
||
406 | * Handle call to __get method. |
||
407 | * |
||
408 | * @param $key |
||
409 | * |
||
410 | * @return mixed |
||
411 | */ |
||
412 | public function __get($key) |
||
416 | |||
417 | /** |
||
418 | * Check if can load files of module on boot method. |
||
419 | * |
||
420 | * @return bool |
||
421 | */ |
||
422 | 2 | protected function isLoadFilesOnBoot() |
|
428 | |||
429 | 6 | private function flushCache(): void |
|
435 | |||
436 | /** |
||
437 | * Register a translation file namespace. |
||
438 | * |
||
439 | * @param string $path |
||
440 | * @param string $namespace |
||
441 | * @return void |
||
442 | */ |
||
443 | 2 | private function loadTranslationsFrom(string $path, string $namespace): void |
|
447 | } |
||
448 |
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..