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 |
||
11 | View Code Duplication | class LumenMigrator |
|
|
|||
12 | { |
||
13 | /** |
||
14 | * Module instance. |
||
15 | * @var Module |
||
16 | */ |
||
17 | protected $module; |
||
18 | |||
19 | /** |
||
20 | * Laravel Application instance. |
||
21 | * |
||
22 | * @var Application. |
||
23 | */ |
||
24 | protected $laravel; |
||
25 | |||
26 | /** |
||
27 | * The database connection to be used |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $database = ''; |
||
32 | |||
33 | /** |
||
34 | * Create new instance. |
||
35 | * @param Module $module |
||
36 | * @param Application $application |
||
37 | */ |
||
38 | public function __construct(Module $module, Application $application) |
||
43 | |||
44 | /** |
||
45 | * Set the database connection to be used |
||
46 | * |
||
47 | * @param $database |
||
48 | * |
||
49 | * @return $this |
||
50 | */ |
||
51 | public function setDatabase($database) |
||
59 | |||
60 | /** |
||
61 | * @return Module |
||
62 | */ |
||
63 | public function getModule() |
||
67 | |||
68 | /** |
||
69 | * Get migration path. |
||
70 | * |
||
71 | * @return string |
||
72 | */ |
||
73 | public function getPath() |
||
82 | |||
83 | /** |
||
84 | * Get migration files. |
||
85 | * |
||
86 | * @param boolean $reverse |
||
87 | * @return array |
||
88 | */ |
||
89 | public function getMigrations($reverse = false) |
||
115 | |||
116 | /** |
||
117 | * Rollback migration. |
||
118 | * |
||
119 | * @return array |
||
120 | */ |
||
121 | public function rollback() |
||
143 | |||
144 | /** |
||
145 | * Reset migration. |
||
146 | * |
||
147 | * @return array |
||
148 | */ |
||
149 | public function reset() |
||
171 | |||
172 | /** |
||
173 | * Run down schema from the given migration name. |
||
174 | * |
||
175 | * @param string $migration |
||
176 | */ |
||
177 | public function down($migration) |
||
181 | |||
182 | /** |
||
183 | * Run up schema from the given migration name. |
||
184 | * |
||
185 | * @param string $migration |
||
186 | */ |
||
187 | public function up($migration) |
||
191 | |||
192 | /** |
||
193 | * Resolve a migration instance from a file. |
||
194 | * |
||
195 | * @param string $file |
||
196 | * |
||
197 | * @return object |
||
198 | */ |
||
199 | public function resolve($file) |
||
207 | |||
208 | /** |
||
209 | * Require in all the migration files in a given path. |
||
210 | * |
||
211 | * @param array $files |
||
212 | */ |
||
213 | public function requireFiles(array $files) |
||
220 | |||
221 | /** |
||
222 | * Get table instance. |
||
223 | * |
||
224 | * @return \Illuminate\Database\Query\Builder |
||
225 | */ |
||
226 | public function table() |
||
230 | |||
231 | /** |
||
232 | * Find migration data from database by given migration name. |
||
233 | * |
||
234 | * @param string $migration |
||
235 | * |
||
236 | * @return object |
||
237 | */ |
||
238 | public function find($migration) |
||
242 | |||
243 | /** |
||
244 | * Save new migration to database. |
||
245 | * |
||
246 | * @param string $migration |
||
247 | * |
||
248 | * @return mixed |
||
249 | */ |
||
250 | public function log($migration) |
||
257 | |||
258 | /** |
||
259 | * Get the next migration batch number. |
||
260 | * |
||
261 | * @return int |
||
262 | */ |
||
263 | public function getNextBatchNumber() |
||
267 | |||
268 | /** |
||
269 | * Get the last migration batch number. |
||
270 | * |
||
271 | * @param array|null $migrations |
||
272 | * @return int |
||
273 | */ |
||
274 | public function getLastBatchNumber($migrations = null) |
||
284 | |||
285 | /** |
||
286 | * Get the last migration batch. |
||
287 | * |
||
288 | * @param array $migrations |
||
289 | * |
||
290 | * @return Collection |
||
291 | */ |
||
292 | public function getLast($migrations) |
||
304 | |||
305 | /** |
||
306 | * Get the ran migrations. |
||
307 | * |
||
308 | * @return Collection |
||
309 | */ |
||
310 | public function getRan() |
||
314 | } |
||
315 |
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.