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 |
||
16 | trait LaravelConfigFile |
||
17 | { |
||
18 | /** |
||
19 | * Avoids using bash using stubs instead to modify config/app.php file. |
||
20 | * |
||
21 | * @var bool |
||
22 | */ |
||
23 | protected $noBash = false; |
||
24 | |||
25 | /** |
||
26 | * @param InputInterface $input |
||
27 | * @param OutputInterface $output |
||
28 | */ |
||
29 | View Code Duplication | protected function initialize(InputInterface $input, OutputInterface $output) |
|
|
|||
30 | { |
||
31 | parent::initialize($input, $output); |
||
32 | if ($input->hasOption('no-bash')) { |
||
33 | $this->noBash = $input->getOption('no-bash'); |
||
34 | } |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Check is --no-bash option is active. |
||
39 | * |
||
40 | * @return bool |
||
41 | */ |
||
42 | private function isNoBashActive() |
||
46 | |||
47 | /** |
||
48 | * Add Laravel IDE Helper provider to config/app.php file. |
||
49 | * |
||
50 | * @return int|null |
||
51 | */ |
||
52 | protected function addLaravelIdeHelperProvider() |
||
56 | |||
57 | /** |
||
58 | * Add provider to config/app.php file. |
||
59 | * |
||
60 | * @param $provider |
||
61 | * |
||
62 | * @return int|null |
||
63 | */ |
||
64 | private function addProvider($provider) |
||
68 | |||
69 | /** |
||
70 | * Add service from file to config/services.php file. |
||
71 | * |
||
72 | * @param $file |
||
73 | * |
||
74 | * @return int|null |
||
75 | */ |
||
76 | private function addService($file, $outputFile = null) |
||
87 | |||
88 | /** |
||
89 | * Add alias to config/app.php file. |
||
90 | * |
||
91 | * @param string $alias |
||
92 | * |
||
93 | * @return int|null |
||
94 | */ |
||
95 | private function addAlias($alias) |
||
99 | |||
100 | /** |
||
101 | * Insert text into file using mountpoint. Mountpoint is maintained at file. |
||
102 | * |
||
103 | * @param string $mountpoint |
||
104 | * @param $textToAdd |
||
105 | * |
||
106 | * @return int|null |
||
107 | */ |
||
108 | private function addTextIntoMountPoint($mountpoint, $textToAdd) |
||
115 | |||
116 | /** |
||
117 | * Insert file into file using mountpoint. |
||
118 | * |
||
119 | * @param $mountpoint |
||
120 | * @param $fileToInsert |
||
121 | * |
||
122 | * @return mixed |
||
123 | */ |
||
124 | private function addFileIntoMountPoint($mountpoint, $fileToInsert, $outputFile = null) |
||
137 | |||
138 | /** |
||
139 | * scape single quotes for sed using \x27. |
||
140 | * |
||
141 | * @param string $str |
||
142 | * |
||
143 | * @return string |
||
144 | */ |
||
145 | private function scapeSingleQuotes($str) |
||
149 | |||
150 | /** |
||
151 | * Installs provider in laravel config/app.php file. |
||
152 | * |
||
153 | * @param $provider |
||
154 | */ |
||
155 | protected function provider($provider) |
||
162 | |||
163 | /** |
||
164 | * Add service/s from file to Laravel config/services.php. |
||
165 | * |
||
166 | * @param $file |
||
167 | * @param null $outputFile |
||
168 | * @throws FileNotFoundException |
||
169 | */ |
||
170 | protected function service($file, $outputFile = null) |
||
178 | |||
179 | /** |
||
180 | * Setup Laravel config file adding providers and aliases. |
||
181 | * |
||
182 | * @param $providers |
||
183 | * @param $aliases |
||
184 | * |
||
185 | * @return int |
||
186 | */ |
||
187 | private function setupLaravelConfigFile($providers, $aliases) |
||
197 | |||
198 | /** |
||
199 | * Installs alias/facade in laravel config/app.php file. |
||
200 | * |
||
201 | * @param $aliasName |
||
202 | * @param $aliasClass |
||
203 | */ |
||
204 | protected function alias($aliasName, $aliasClass) |
||
211 | |||
212 | /** |
||
213 | * Install /config/app.php file using bash script. |
||
214 | */ |
||
215 | protected function installConfigFileWithBash() |
||
220 | |||
221 | /** |
||
222 | * Install /stubs/app.php into /config/app.php. |
||
223 | */ |
||
224 | protected function installConfigFileWithStubs() |
||
229 | |||
230 | /** |
||
231 | * Check if Laravel config file exists. |
||
232 | * |
||
233 | * @return bool |
||
234 | */ |
||
235 | protected function checkIfLaravelConfigFileExists() |
||
239 | |||
240 | /** |
||
241 | * Install llum custom config/app.php file. |
||
242 | * |
||
243 | * @return int |
||
244 | */ |
||
245 | protected function installConfigFile() |
||
262 | |||
263 | /** |
||
264 | * Test Laravel config file exists. |
||
265 | * |
||
266 | * @return int |
||
267 | */ |
||
268 | private function testLaravelConfigFileExists() |
||
276 | |||
277 | /** |
||
278 | * Show warning if Laravel config file already supports llum. |
||
279 | * |
||
280 | * @return int |
||
281 | */ |
||
282 | private function showWarningIfLaravelConfigAlreadySupportsLlum() |
||
290 | |||
291 | /** |
||
292 | * Add providers to Laravel config file. |
||
293 | * |
||
294 | * @param $providers |
||
295 | */ |
||
296 | protected function addProviders($providers) |
||
303 | |||
304 | /** |
||
305 | * Add aliases to Laravel config file. |
||
306 | * |
||
307 | * @param $aliases |
||
308 | */ |
||
309 | protected function addAliases($aliases) |
||
319 | |||
320 | /** |
||
321 | * Check if config/app.php stub file is already installed. |
||
322 | * |
||
323 | * @return bool |
||
324 | */ |
||
325 | protected function configAppFileAlreadyInstalled() |
||
333 | } |
||
334 |
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.