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 |
||
10 | View Code Duplication | class MakeRequestCommand extends Command |
|
|
|||
11 | { |
||
12 | /** |
||
13 | * The console command name. |
||
14 | * |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $name = 'make:module:request'; |
||
18 | |||
19 | /** |
||
20 | * The console command description. |
||
21 | * |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $description = 'Create a new module form request class'; |
||
25 | |||
26 | /** |
||
27 | * Array to store the configuration details. |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $container; |
||
32 | |||
33 | /** |
||
34 | * Create a new command instance. |
||
35 | * |
||
36 | * @param Filesystem $files |
||
37 | * @param Modules $module |
||
38 | */ |
||
39 | public function __construct(Filesystem $files, Modules $module) |
||
46 | |||
47 | /** |
||
48 | * Execute the console command. |
||
49 | * |
||
50 | * @return mixed |
||
51 | */ |
||
52 | public function fire() |
||
67 | |||
68 | /** |
||
69 | * Create a new migration file. |
||
70 | * |
||
71 | * @return int |
||
72 | */ |
||
73 | protected function makeFile() |
||
77 | |||
78 | /** |
||
79 | * Get file destination. |
||
80 | * |
||
81 | * @return string |
||
82 | */ |
||
83 | protected function getDestinationFile() |
||
87 | |||
88 | /** |
||
89 | * Get module migration path. |
||
90 | * |
||
91 | * @return string |
||
92 | */ |
||
93 | protected function getPath() |
||
99 | |||
100 | /** |
||
101 | * Get the migration filename. |
||
102 | * |
||
103 | * @return string |
||
104 | */ |
||
105 | protected function getFilename() |
||
109 | |||
110 | /** |
||
111 | * Get the stub content. |
||
112 | * |
||
113 | * @return string |
||
114 | */ |
||
115 | protected function getStubContent() |
||
119 | |||
120 | /** |
||
121 | * Replace placeholder text with correct values. |
||
122 | * |
||
123 | * @return string |
||
124 | */ |
||
125 | protected function formatContent($content) |
||
133 | |||
134 | /** |
||
135 | * Get the console command arguments. |
||
136 | * |
||
137 | * @return array |
||
138 | */ |
||
139 | protected function getArguments() |
||
146 | } |
||
147 |
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.