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 |
||
13 | class ModuleMigrateResetCommand extends Command |
||
14 | { |
||
15 | use ConfirmableTrait; |
||
16 | |||
17 | /** |
||
18 | * The console command name. |
||
19 | * |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $name = 'module:migrate:reset'; |
||
23 | |||
24 | /** |
||
25 | * The console command description. |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | protected $description = 'Rollback all database migrations for a specific or all modules'; |
||
30 | |||
31 | /** |
||
32 | * @var Modules |
||
33 | */ |
||
34 | protected $module; |
||
35 | |||
36 | /** |
||
37 | * @var Migrator |
||
38 | */ |
||
39 | protected $migrator; |
||
40 | |||
41 | /** |
||
42 | * @var Filesystem |
||
43 | */ |
||
44 | protected $files; |
||
45 | |||
46 | /** |
||
47 | * Create a new command instance. |
||
48 | * |
||
49 | * @param Modules $module |
||
50 | * @param Filesystem $files |
||
51 | * @param Migrator $migrator |
||
52 | */ |
||
53 | public function __construct(Modules $module, Filesystem $files, Migrator $migrator) |
||
54 | { |
||
55 | parent::__construct(); |
||
56 | |||
57 | $this->module = $module; |
||
58 | $this->files = $files; |
||
59 | $this->migrator = $migrator; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Execute the console command. |
||
64 | * |
||
65 | * @return mixed |
||
66 | */ |
||
67 | View Code Duplication | public function fire() |
|
93 | |||
94 | /** |
||
95 | * Run the migration reset for the specified module. |
||
96 | * |
||
97 | * Migrations should be reset in the reverse order that they were |
||
98 | * migrated up as. This ensures the database is properly reversed |
||
99 | * without conflict. |
||
100 | * |
||
101 | * @param string $slug |
||
102 | * |
||
103 | * @return mixed |
||
104 | */ |
||
105 | protected function reset($slug) |
||
106 | { |
||
107 | $this->migrator->setconnection($this->input->getOption('database')); |
||
108 | |||
109 | $pretend = $this->input->getOption('pretend'); |
||
110 | $migrationPath = $this->getMigrationPath($slug); |
||
111 | $migrations = array_reverse($this->migrator->getMigrationFiles($migrationPath)); |
||
112 | |||
113 | if (count($migrations) == 0) { |
||
114 | return $this->error('Nothing to rollback.'); |
||
115 | } |
||
116 | |||
117 | foreach ($migrations as $migration) { |
||
118 | $this->info('Migration: '.$migration); |
||
119 | $this->runDown($slug, $migration, $pretend); |
||
120 | } |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Run "down" a migration instance. |
||
125 | * |
||
126 | * @param string $slug |
||
127 | * @param object $migration |
||
128 | * @param bool $pretend |
||
129 | */ |
||
130 | protected function runDown($slug, $migration, $pretend) |
||
131 | { |
||
132 | $migrationPath = $this->getMigrationPath($slug); |
||
133 | $file = (string) $migrationPath.'/'.$migration.'.php'; |
||
134 | $classFile = implode('_', array_slice(explode('_', basename($file, '.php')), 4)); |
||
135 | $class = studly_case($classFile); |
||
136 | $table = $this->laravel['config']['database.migrations']; |
||
137 | |||
138 | include $file; |
||
139 | |||
140 | $instance = new $class(); |
||
141 | $instance->down(); |
||
142 | |||
143 | $this->laravel['db']->table($table) |
||
144 | ->where('migration', $migration) |
||
145 | ->delete(); |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Get the console command parameters. |
||
150 | * |
||
151 | * @param string $slug |
||
152 | * |
||
153 | * @return array |
||
154 | */ |
||
155 | protected function getParameters($slug) |
||
175 | |||
176 | /** |
||
177 | * Get migrations path. |
||
178 | * |
||
179 | * @return string |
||
180 | */ |
||
181 | protected function getMigrationPath($slug) |
||
187 | |||
188 | /** |
||
189 | * Get the console command arguments. |
||
190 | * |
||
191 | * @return array |
||
192 | */ |
||
193 | protected function getArguments() |
||
197 | |||
198 | /** |
||
199 | * Get the console command options. |
||
200 | * |
||
201 | * @return array |
||
202 | */ |
||
203 | View Code Duplication | protected function getOptions() |
|
212 | } |
||
213 |
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.