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 namespace Magestead\Installers; |
||
14 | class Magento2Project |
||
15 | { |
||
16 | /** |
||
17 | * Magento2Project constructor. |
||
18 | * @param array $options |
||
19 | * @param array $config |
||
20 | * @param $projectPath |
||
21 | * @param OutputInterface $output |
||
22 | */ |
||
23 | public function __construct(array $options, array $config, $projectPath, OutputInterface $output) |
||
32 | |||
33 | /** |
||
34 | * @param $projectPath |
||
35 | * @param OutputInterface $output |
||
36 | */ |
||
37 | protected function composerInstall($projectPath, OutputInterface $output) |
||
47 | |||
48 | /** |
||
49 | * @param $projectPath |
||
50 | * @param OutputInterface $output |
||
51 | */ |
||
52 | View Code Duplication | protected function addPhpSpecPackage($projectPath, OutputInterface $output) |
|
60 | |||
61 | /** |
||
62 | * @param $projectPath |
||
63 | * @param OutputInterface $output |
||
64 | */ |
||
65 | View Code Duplication | protected function addBehatPackage($projectPath, OutputInterface $output) |
|
73 | |||
74 | /** |
||
75 | * @param array $options |
||
76 | * @param $projectPath |
||
77 | * @param OutputInterface $output |
||
78 | */ |
||
79 | protected function installMagento(array $options, $projectPath, OutputInterface $output) |
||
80 | { |
||
81 | $this->setPermissions($projectPath, $output); |
||
82 | |||
83 | $output->writeln('<info>Installing Magento 2 Software</info>'); |
||
84 | $locale = $options['magestead']['apps']['mba_12345']['locale']; |
||
85 | $db_name = $options['magestead']['apps']['mba_12345']['db_name']; |
||
86 | $base_url = $options['magestead']['apps']['mba_12345']['base_url']; |
||
87 | $default_currency = $options['magestead']['apps']['mba_12345']['default_currency']; |
||
88 | |||
89 | $install = 'vagrant ssh -c \'cd /var/www/public; bin/magento setup:install --base-url=http://'.$base_url.'/ \ |
||
90 | --db-host=localhost \ |
||
91 | --db-name='.$db_name.' \ |
||
92 | --db-user=magestead \ |
||
93 | --db-password=vagrant \ |
||
94 | --admin-firstname=RichDynamix \ |
||
95 | --admin-lastname=Magestead \ |
||
96 | [email protected] \ |
||
97 | --admin-user=admin \ |
||
98 | --admin-password=password123 \ |
||
99 | --language='.$locale.' \ |
||
100 | --currency='.$default_currency.' \ |
||
101 | --timezone=Europe/London \ |
||
102 | --use-rewrites=1 \ |
||
103 | --backend-frontname=admin \ |
||
104 | --session-save=db \''; |
||
105 | |||
106 | new ProcessCommand($install, $projectPath, $output); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @param $projectPath |
||
111 | * @param OutputInterface $output |
||
112 | */ |
||
113 | protected function setPermissions($projectPath, OutputInterface $output) |
||
128 | |||
129 | /** |
||
130 | * @param $projectPath |
||
131 | * @param OutputInterface $output |
||
132 | */ |
||
133 | protected function configureRedis($projectPath, OutputInterface $output) |
||
180 | |||
181 | /** |
||
182 | * @param array $options |
||
183 | * @param $projectPath |
||
184 | * @param OutputInterface $output |
||
185 | */ |
||
186 | protected function finaliseSetup(array $options, $projectPath, OutputInterface $output) |
||
199 | |||
200 | /** |
||
201 | * @param array $options |
||
202 | * @param OutputInterface $output |
||
203 | */ |
||
204 | View Code Duplication | protected function showCredentials(array $options, OutputInterface $output) |
|
217 | |||
218 | /** |
||
219 | * @param array $options |
||
220 | * @param $projectPath |
||
221 | * @param OutputInterface $output |
||
222 | * @return VersionControl|null |
||
223 | */ |
||
224 | View Code Duplication | protected function processVcs(array $options, $projectPath, OutputInterface $output) |
|
231 | |||
232 | /** |
||
233 | * @param $projectPath |
||
234 | */ |
||
235 | protected function setComposerBinDir($projectPath) |
||
243 | |||
244 | /** |
||
245 | * @param $projectPath |
||
246 | * @param OutputInterface $output |
||
247 | */ |
||
248 | protected function setPhpSpecPermissions($projectPath, OutputInterface $output) |
||
253 | |||
254 | /** |
||
255 | * @param $projectPath |
||
256 | * @param OutputInterface $output |
||
257 | */ |
||
258 | protected function setBehatPermissions($projectPath, OutputInterface $output) |
||
263 | } |
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.