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 |
||
7 | class InstallCommand extends Command |
||
8 | { |
||
9 | /** |
||
10 | * The console command name. |
||
11 | * |
||
12 | * @var string |
||
13 | */ |
||
14 | protected $signature = 'admin:install'; |
||
15 | |||
16 | /** |
||
17 | * The console command description. |
||
18 | * |
||
19 | * @var string |
||
20 | */ |
||
21 | protected $description = 'Install the admin package'; |
||
22 | |||
23 | /** |
||
24 | * Install directory. |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $directory = ''; |
||
29 | |||
30 | /** |
||
31 | * Execute the console command. |
||
32 | * |
||
33 | * @return void |
||
34 | */ |
||
35 | public function handle() |
||
41 | |||
42 | /** |
||
43 | * Create tables and seed it. |
||
44 | * |
||
45 | * @return void |
||
46 | */ |
||
47 | public function initDatabase() |
||
57 | |||
58 | /** |
||
59 | * Initialize the admAin directory. |
||
60 | * |
||
61 | * @return void |
||
62 | */ |
||
63 | protected function initAdminDirectory() |
||
85 | |||
86 | /** |
||
87 | * Create HomeController. |
||
88 | * |
||
89 | * @return void |
||
90 | */ |
||
91 | View Code Duplication | public function createHomeController() |
|
102 | |||
103 | /** |
||
104 | * Create AuthController. |
||
105 | * |
||
106 | * @return void |
||
107 | */ |
||
108 | View Code Duplication | public function createAuthController() |
|
119 | |||
120 | /** |
||
121 | * Create HomeController. |
||
122 | * |
||
123 | * @return void |
||
124 | */ |
||
125 | View Code Duplication | public function createExampleController() |
|
126 | { |
||
127 | $exampleController = $this->directory.'/Controllers/ExampleController.php'; |
||
128 | $contents = $this->getStub('ExampleController'); |
||
129 | |||
130 | $this->laravel['files']->put( |
||
131 | $exampleController, |
||
132 | str_replace('DummyNamespace', config('admin.route.namespace'), $contents) |
||
133 | ); |
||
134 | $this->line('<info>ExampleController file was created:</info> '.str_replace(base_path(), '', $exampleController)); |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * Create routes file. |
||
139 | * |
||
140 | * @return void |
||
141 | */ |
||
142 | protected function createBootstrapFile() |
||
150 | |||
151 | /** |
||
152 | * Create routes file. |
||
153 | * |
||
154 | * @return void |
||
155 | */ |
||
156 | protected function createRoutesFile() |
||
157 | { |
||
158 | $file = $this->directory.'/routes.php'; |
||
159 | |||
160 | $contents = $this->getStub('routes'); |
||
161 | $this->laravel['files']->put($file, str_replace('DummyNamespace', config('admin.route.namespace'), $contents)); |
||
162 | $this->line('<info>Routes file was created:</info> '.str_replace(base_path(), '', $file)); |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * Get stub contents. |
||
167 | * |
||
168 | * @param $name |
||
169 | * |
||
170 | * @return string |
||
171 | */ |
||
172 | protected function getStub($name) |
||
176 | |||
177 | /** |
||
178 | * Make new directory. |
||
179 | * |
||
180 | * @param string $path |
||
181 | */ |
||
182 | protected function makeDir($path = '') |
||
186 | } |
||
187 |
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.