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 ModuleGenerator 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 ModuleGenerator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class ModuleGenerator extends Generator |
||
14 | { |
||
15 | /** |
||
16 | * The module name will created. |
||
17 | * |
||
18 | * @var string |
||
19 | */ |
||
20 | protected $name; |
||
21 | |||
22 | /** |
||
23 | * The laravel config instance. |
||
24 | * |
||
25 | * @var Config |
||
26 | */ |
||
27 | protected $config; |
||
28 | |||
29 | /** |
||
30 | * The laravel filesystem instance. |
||
31 | * |
||
32 | * @var Filesystem |
||
33 | */ |
||
34 | protected $filesystem; |
||
35 | |||
36 | /** |
||
37 | * The laravel console instance. |
||
38 | * |
||
39 | * @var Console |
||
40 | */ |
||
41 | protected $console; |
||
42 | |||
43 | /** |
||
44 | * The pingpong module instance. |
||
45 | * |
||
46 | * @var \Nwidart\Modules\Module |
||
47 | */ |
||
48 | protected $module; |
||
49 | |||
50 | /** |
||
51 | * Force status. |
||
52 | * |
||
53 | * @var bool |
||
54 | */ |
||
55 | protected $force = false; |
||
56 | |||
57 | /** |
||
58 | * Generate a plain module. |
||
59 | * |
||
60 | * @var bool |
||
61 | */ |
||
62 | protected $plain = false; |
||
63 | |||
64 | /** |
||
65 | * Generate a api module. |
||
66 | * |
||
67 | * @var bool |
||
68 | */ |
||
69 | protected $api = false; |
||
70 | |||
71 | /** |
||
72 | * The constructor. |
||
73 | * @param $name |
||
74 | * @param FileRepository $module |
||
75 | * @param Config $config |
||
76 | * @param Filesystem $filesystem |
||
77 | * @param Console $console |
||
78 | */ |
||
79 | public function __construct( |
||
92 | |||
93 | /** |
||
94 | * Set plain flag. |
||
95 | * |
||
96 | * @param bool $plain |
||
97 | * |
||
98 | * @return $this |
||
99 | */ |
||
100 | public function setPlain($plain) |
||
106 | |||
107 | /** |
||
108 | * Get the name of module will created. By default in studly case. |
||
109 | * |
||
110 | * @return string |
||
111 | */ |
||
112 | public function getName() |
||
116 | |||
117 | /** |
||
118 | * Get the laravel config instance. |
||
119 | * |
||
120 | * @return Config |
||
121 | */ |
||
122 | public function getConfig() |
||
126 | |||
127 | /** |
||
128 | * Set the laravel config instance. |
||
129 | * |
||
130 | * @param Config $config |
||
131 | * |
||
132 | * @return $this |
||
133 | */ |
||
134 | public function setConfig($config) |
||
140 | |||
141 | /** |
||
142 | * Get the laravel filesystem instance. |
||
143 | * |
||
144 | * @return Filesystem |
||
145 | */ |
||
146 | public function getFilesystem() |
||
150 | |||
151 | /** |
||
152 | * Set the laravel filesystem instance. |
||
153 | * |
||
154 | * @param Filesystem $filesystem |
||
155 | * |
||
156 | * @return $this |
||
157 | */ |
||
158 | public function setFilesystem($filesystem) |
||
164 | |||
165 | /** |
||
166 | * Get the laravel console instance. |
||
167 | * |
||
168 | * @return Console |
||
169 | */ |
||
170 | public function getConsole() |
||
174 | |||
175 | /** |
||
176 | * Set the laravel console instance. |
||
177 | * |
||
178 | * @param Console $console |
||
179 | * |
||
180 | * @return $this |
||
181 | */ |
||
182 | public function setConsole($console) |
||
188 | |||
189 | /** |
||
190 | * Get the module instance. |
||
191 | * |
||
192 | * @return \Nwidart\Modules\Module |
||
193 | */ |
||
194 | public function getModule() |
||
198 | |||
199 | /** |
||
200 | * Set the pingpong module instance. |
||
201 | * |
||
202 | * @param mixed $module |
||
203 | * |
||
204 | * @return $this |
||
205 | */ |
||
206 | public function setModule($module) |
||
212 | |||
213 | /** |
||
214 | * Get the list of folders will created. |
||
215 | * |
||
216 | * @return array |
||
217 | */ |
||
218 | public function getFolders() |
||
222 | |||
223 | /** |
||
224 | * Get the list of files will created. |
||
225 | * |
||
226 | * @return array |
||
227 | */ |
||
228 | public function getFiles() |
||
232 | |||
233 | /** |
||
234 | * Set force status. |
||
235 | * |
||
236 | * @param bool|int $force |
||
237 | * |
||
238 | * @return $this |
||
239 | */ |
||
240 | public function setForce($force) |
||
246 | |||
247 | /** |
||
248 | * Set Api Flog. |
||
249 | * |
||
250 | * @param bool $api |
||
251 | * |
||
252 | * @return $this |
||
253 | */ |
||
254 | public function setApi($api) |
||
260 | |||
261 | /** |
||
262 | * Generate the module. |
||
263 | */ |
||
264 | public function generate() |
||
293 | |||
294 | /** |
||
295 | * Generate the folders. |
||
296 | */ |
||
297 | public function generateFolders() |
||
314 | |||
315 | /** |
||
316 | * Generate git keep to the specified path. |
||
317 | * |
||
318 | * @param string $path |
||
319 | */ |
||
320 | public function generateGitKeep($path) |
||
324 | |||
325 | /** |
||
326 | * Generate the files. |
||
327 | */ |
||
328 | public function generateFiles() |
||
348 | |||
349 | /** |
||
350 | * Generate some resources. |
||
351 | */ |
||
352 | public function generateResources() |
||
362 | |||
363 | /** |
||
364 | * get Make Route Provider Arguments |
||
365 | * |
||
366 | * @since 2018/11/16 |
||
367 | * @return array |
||
368 | */ |
||
369 | protected function getMakeRouteProviderArguments() |
||
380 | |||
381 | /** |
||
382 | * get Make Provider Arguments |
||
383 | * |
||
384 | * @since 2018/11/16 |
||
385 | * @return array |
||
386 | */ |
||
387 | protected function getMakeProviderArguments() |
||
400 | |||
401 | /** |
||
402 | * get Make Controller Arguments |
||
403 | * |
||
404 | * @since 2018/11/16 |
||
405 | * @return array |
||
406 | */ |
||
407 | protected function getMakeControllerArguments() |
||
419 | |||
420 | /** |
||
421 | * get Make Seed Arguments |
||
422 | * |
||
423 | * @since 2018/11/16 |
||
424 | * @return array |
||
425 | */ |
||
426 | protected function getMakeSeedArguments() |
||
434 | |||
435 | /** |
||
436 | * Get the contents of the specified stub file by given stub name. |
||
437 | * |
||
438 | * @param $stub |
||
439 | * |
||
440 | * @return string |
||
441 | */ |
||
442 | protected function getStubContents($stub) |
||
450 | |||
451 | /** |
||
452 | * get the list for the replacements. |
||
453 | */ |
||
454 | public function getReplacements() |
||
458 | |||
459 | /** |
||
460 | * Get array replacement for the specified stub. |
||
461 | * |
||
462 | * @param $stub |
||
463 | * |
||
464 | * @return array |
||
465 | */ |
||
466 | protected function getReplacement($stub) |
||
488 | |||
489 | /** |
||
490 | * Generate the module.json file |
||
491 | */ |
||
492 | private function generateModuleJsonFile() |
||
504 | |||
505 | /** |
||
506 | * Remove the default service provider that was added in the module.json file |
||
507 | * This is needed when a --plain module was created |
||
508 | */ |
||
509 | private function cleanModuleJsonFile() |
||
523 | |||
524 | /** |
||
525 | * Get the module name in lower case. |
||
526 | * |
||
527 | * @return string |
||
528 | */ |
||
529 | protected function getLowerNameReplacement() |
||
533 | |||
534 | /** |
||
535 | * Get the module name in studly case. |
||
536 | * |
||
537 | * @return string |
||
538 | */ |
||
539 | protected function getStudlyNameReplacement() |
||
543 | |||
544 | /** |
||
545 | * Get replacement for $VENDOR$. |
||
546 | * |
||
547 | * @return string |
||
548 | */ |
||
549 | protected function getVendorReplacement() |
||
553 | |||
554 | /** |
||
555 | * Get replacement for $MODULE_NAMESPACE$. |
||
556 | * |
||
557 | * @return string |
||
558 | */ |
||
559 | protected function getModuleNamespaceReplacement() |
||
563 | |||
564 | /** |
||
565 | * Get replacement for $AUTHOR_NAME$. |
||
566 | * |
||
567 | * @return string |
||
568 | */ |
||
569 | protected function getAuthorNameReplacement() |
||
573 | |||
574 | /** |
||
575 | * Get replacement for $AUTHOR_EMAIL$. |
||
576 | * |
||
577 | * @return string |
||
578 | */ |
||
579 | protected function getAuthorEmailReplacement() |
||
583 | } |
||
584 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.