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 |
||
18 | class SelfUpdateCommand extends Command |
||
19 | { |
||
20 | const MANIFEST_FILE = 'https://php-translation.github.io/cli/manifest.json'; |
||
21 | const PHAR_URL = 'https://php-translation.github.io/cli/downloads/translation.phar'; |
||
22 | const PACKAGE_NAME = 'php-translation/cli'; |
||
23 | const FILE_NAME = 'translation.phar'; |
||
24 | /** |
||
25 | * @var OutputInterface |
||
26 | */ |
||
27 | protected $output; |
||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $version; |
||
32 | /** |
||
33 | * Execute the command. |
||
34 | * |
||
35 | * @param InputInterface $input |
||
36 | * @param OutputInterface $output |
||
37 | */ |
||
38 | protected function execute(InputInterface $input, OutputInterface $output) |
||
39 | { |
||
40 | $this->output = $output; |
||
41 | $this->version = $this->getApplication()->getVersion(); |
||
42 | $parser = new VersionParser(); |
||
43 | /** |
||
44 | * Check for ancilliary options. |
||
45 | */ |
||
46 | if ($input->getOption('rollback')) { |
||
47 | $this->rollback(); |
||
48 | |||
49 | return; |
||
50 | } |
||
51 | if ($input->getOption('check')) { |
||
52 | $this->printAvailableUpdates(); |
||
53 | |||
54 | return; |
||
55 | } |
||
56 | |||
57 | if ($input->getOption('pre')) { |
||
58 | $this->updateToPreReleaseBuild(); |
||
59 | |||
60 | return; |
||
61 | } |
||
62 | if ($input->getOption('stable')) { |
||
63 | $this->updateToStableBuild(); |
||
64 | |||
65 | return; |
||
66 | } |
||
67 | if ($input->getOption('non-dev')) { |
||
68 | $this->updateToMostRecentNonDevRemote(); |
||
69 | |||
70 | return; |
||
71 | } |
||
72 | /** |
||
73 | * If current build is stable, only update to more recent stable |
||
74 | * versions if available. User may specify otherwise using options. |
||
75 | */ |
||
76 | if ($parser->isStable($this->version)) { |
||
77 | $this->updateToStableBuild(); |
||
78 | |||
79 | return; |
||
80 | } |
||
81 | /** |
||
82 | * By default, update to most recent remote version regardless |
||
83 | * of stability. |
||
84 | */ |
||
85 | $this->updateToMostRecentNonDevRemote(); |
||
86 | } |
||
87 | protected function getStableUpdater() |
||
118 | |||
119 | protected function updateToStableBuild() |
||
131 | protected function update(Updater $updater) |
||
132 | { |
||
133 | $this->output->writeln('Updating...'.PHP_EOL); |
||
134 | try { |
||
135 | $result = $updater->update(); |
||
136 | $newVersion = $updater->getNewVersion(); |
||
137 | $oldVersion = $updater->getOldVersion(); |
||
138 | if (strlen($newVersion) == 40) { |
||
139 | $newVersion = 'dev-'.$newVersion; |
||
140 | } |
||
141 | if (strlen($oldVersion) == 40) { |
||
142 | $oldVersion = 'dev-'.$oldVersion; |
||
143 | } |
||
144 | |||
145 | if ($result) { |
||
146 | $this->output->writeln('<fg=green>PHP-Translation has been updated.</fg=green>'); |
||
147 | $this->output->writeln(sprintf( |
||
148 | '<fg=green>Current version is:</fg=green> <options=bold>%s</options=bold>.', |
||
149 | $newVersion |
||
150 | )); |
||
151 | $this->output->writeln(sprintf( |
||
152 | '<fg=green>Previous version was:</fg=green> <options=bold>%s</options=bold>.', |
||
153 | $oldVersion |
||
154 | )); |
||
155 | } else { |
||
156 | $this->output->writeln('<fg=green>PHP-Translation is currently up to date.</fg=green>'); |
||
157 | $this->output->writeln(sprintf( |
||
158 | '<fg=green>Current version is:</fg=green> <options=bold>%s</options=bold>.', |
||
159 | $oldVersion |
||
160 | )); |
||
161 | } |
||
162 | } catch (\Exception $e) { |
||
163 | $this->output->writeln(sprintf('Error: <fg=yellow>%s</fg=yellow>', $e->getMessage())); |
||
164 | } |
||
165 | $this->output->write(PHP_EOL); |
||
166 | $this->output->writeln('You can also select update stability using --dev, --pre (alpha/beta/rc) or --stable.'); |
||
167 | } |
||
168 | protected function rollback() |
||
169 | { |
||
170 | $updater = new Updater(); |
||
171 | try { |
||
172 | $result = $updater->rollback(); |
||
173 | if ($result) { |
||
174 | $this->output->writeln('<fg=green>PHP-Translation has been rolled back to prior version.</fg=green>'); |
||
175 | } else { |
||
176 | $this->output->writeln('<fg=red>Rollback failed for reasons unknown.</fg=red>'); |
||
177 | } |
||
178 | } catch (\Exception $e) { |
||
179 | $this->output->writeln(sprintf('Error: <fg=yellow>%s</fg=yellow>', $e->getMessage())); |
||
180 | } |
||
181 | } |
||
182 | protected function printAvailableUpdates() |
||
196 | protected function printCurrentStableVersion() |
||
197 | { |
||
198 | $this->printVersion($this->getStableUpdater()); |
||
199 | } |
||
200 | protected function printCurrentPreReleaseVersion() |
||
204 | protected function printVersion(Updater $updater) |
||
205 | { |
||
206 | $stability = 'stable'; |
||
207 | if ($updater->getStrategy() instanceof ShaStrategy) { |
||
208 | $stability = 'development'; |
||
272 | } |
||
273 |
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.