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 \Illuminate\Console\GeneratorCommand |
||
10 | { |
||
11 | /** |
||
12 | * The filesystem instance. |
||
13 | * |
||
14 | * @var Filesystem |
||
15 | */ |
||
16 | protected $files; |
||
17 | |||
18 | /** |
||
19 | * The type of class being generated. |
||
20 | * |
||
21 | * @var string |
||
22 | */ |
||
23 | protected $type; |
||
24 | |||
25 | |||
26 | /** |
||
27 | * Get the stub file for the generator. |
||
28 | * |
||
29 | * @return string |
||
30 | */ |
||
31 | |||
32 | /** |
||
33 | * Execute the console command. |
||
34 | * |
||
35 | * @return bool|null |
||
36 | */ |
||
37 | View Code Duplication | public function handle() |
|
64 | |||
65 | /** |
||
66 | * Parse the class name and format according to the root namespace. |
||
67 | * |
||
68 | * @param string $name |
||
69 | * |
||
70 | * @return string |
||
71 | */ |
||
72 | protected function qualifyClass($name) |
||
87 | |||
88 | /** |
||
89 | * Get the root namespace for the class. |
||
90 | * |
||
91 | * @return string |
||
92 | */ |
||
93 | protected function rootNamespace() |
||
97 | |||
98 | /** |
||
99 | * Get the default namespace for the class. |
||
100 | * |
||
101 | * @param string $rootNamespace |
||
102 | * |
||
103 | * @return string |
||
104 | */ |
||
105 | protected function getDefaultNamespace($rootNamespace) |
||
109 | |||
110 | /** |
||
111 | * Get the desired class name from the input. |
||
112 | * |
||
113 | * @return string |
||
114 | */ |
||
115 | protected function getNameInput() |
||
119 | |||
120 | /** |
||
121 | * Get the destination class path. |
||
122 | * |
||
123 | * @param string $name |
||
124 | * |
||
125 | * @return string |
||
126 | */ |
||
127 | protected function getPath($name) |
||
133 | |||
134 | /** |
||
135 | * Determine if the class already exists. |
||
136 | * |
||
137 | * @param string $rawName |
||
138 | * |
||
139 | * @return bool |
||
140 | */ |
||
141 | protected function alreadyExists($rawName) |
||
145 | |||
146 | /** |
||
147 | * Build the directory for the class if necessary. |
||
148 | * |
||
149 | * @param string $path |
||
150 | * |
||
151 | * @return string |
||
152 | */ |
||
153 | protected function makeDirectory($path) |
||
161 | |||
162 | /** |
||
163 | * Build the class with the given name. |
||
164 | * |
||
165 | * @param string $name |
||
166 | * |
||
167 | * @return string |
||
168 | */ |
||
169 | protected function buildClass($name) |
||
178 | |||
179 | /** |
||
180 | * Replace the class name for the given stub. |
||
181 | * |
||
182 | * @param string $stub |
||
183 | * @param string $name |
||
184 | * |
||
185 | * @return string |
||
186 | */ |
||
187 | protected function replaceClass($stub, $name) |
||
193 | |||
194 | /** |
||
195 | * Get the full namespace for a given class, without the class name. |
||
196 | * |
||
197 | * @param string $name |
||
198 | * |
||
199 | * @return string |
||
200 | */ |
||
201 | protected function getNamespace($name) |
||
205 | |||
206 | /** |
||
207 | * Replace the namespace for the given stub. |
||
208 | * |
||
209 | * @param string $stub |
||
210 | * @param string $name |
||
211 | * |
||
212 | * @return $this |
||
213 | */ |
||
214 | protected function replaceNamespace(&$stub, $name) |
||
226 | |||
227 | /** |
||
228 | * Get the console command arguments. |
||
229 | * |
||
230 | * @return array |
||
231 | */ |
||
232 | protected function getArguments() |
||
238 | |||
239 | protected function getRelativePath($path) |
||
244 | } |
||
245 |
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.