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:
1 | <?php |
||
9 | abstract class GeneratorCommand extends Command |
||
10 | { |
||
11 | /** |
||
12 | * The name of 'name' argument. |
||
13 | * |
||
14 | * @var string |
||
15 | */ |
||
16 | protected $argumentName = ''; |
||
17 | |||
18 | /** |
||
19 | * Get template contents. |
||
20 | * |
||
21 | * @return string |
||
22 | */ |
||
23 | abstract protected function getTemplateContents(); |
||
24 | |||
25 | /** |
||
26 | * Get the destination file path. |
||
27 | * |
||
28 | * @return string |
||
29 | */ |
||
30 | abstract protected function getDestinationFilePath(); |
||
31 | |||
32 | /** |
||
33 | * Execute the console command. |
||
34 | */ |
||
35 | public function handle() |
||
53 | |||
54 | /** |
||
55 | * Get class name. |
||
56 | * |
||
57 | * @return string |
||
58 | */ |
||
59 | public function getClass() |
||
63 | |||
64 | /** |
||
65 | * Get default namespace. |
||
66 | * |
||
67 | * @return string |
||
68 | */ |
||
69 | public function getDefaultNamespace() : string |
||
73 | |||
74 | /** |
||
75 | * Get class namespace. |
||
76 | * |
||
77 | * @param \Nwidart\Modules\Module $module |
||
78 | * |
||
79 | * @return string |
||
80 | */ |
||
81 | public function getClassNamespace($module) |
||
99 | } |
||
100 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.