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 |
||
31 | final class AddPrefixCommand extends Command |
||
32 | { |
||
33 | /** @internal */ |
||
34 | const PATH_ARG = 'paths'; |
||
35 | /** @internal */ |
||
36 | const PREFIX_OPT = 'prefix'; |
||
37 | /** @internal */ |
||
38 | const OUTPUT_DIR_OPT = 'output-dir'; |
||
39 | /** @internal */ |
||
40 | const FORCE_OPT = 'force'; |
||
41 | /** @internal */ |
||
42 | const STOP_ON_FAILURE_OPT = 'stop-on-failure'; |
||
43 | /** @internal */ |
||
44 | const CONFIG_FILE = 'config'; |
||
45 | /** @internal */ |
||
46 | const CONFIG_FILE_DEFAULT = 'scoper.inc.php'; |
||
47 | |||
48 | private $fileSystem; |
||
49 | private $handle; |
||
50 | |||
51 | /** |
||
52 | * @inheritdoc |
||
53 | */ |
||
54 | 21 | public function __construct(Filesystem $fileSystem, HandleAddPrefix $handle) |
|
61 | |||
62 | /** |
||
63 | * @inheritdoc |
||
64 | */ |
||
65 | 21 | protected function configure() |
|
112 | |||
113 | /** |
||
114 | * @inheritdoc |
||
115 | */ |
||
116 | 19 | protected function execute(InputInterface $input, OutputInterface $output) |
|
154 | |||
155 | 19 | private function validatePrefix(InputInterface $input) |
|
180 | |||
181 | 14 | private function validatePaths(InputInterface $input) |
|
203 | |||
204 | 14 | private function validateOutputDir(InputInterface $input, OutputStyle $io) |
|
205 | { |
||
206 | 14 | $outputDir = $input->getOption(self::OUTPUT_DIR_OPT); |
|
207 | |||
208 | 14 | View Code Duplication | if (false === $this->fileSystem->isAbsolutePath($outputDir)) { |
|
|||
209 | 1 | $outputDir = getcwd().DIRECTORY_SEPARATOR.$outputDir; |
|
210 | } |
||
211 | |||
212 | 14 | $input->setOption(self::OUTPUT_DIR_OPT, $outputDir); |
|
213 | |||
214 | 14 | if (false === $this->fileSystem->exists($outputDir)) { |
|
215 | 14 | return; |
|
216 | } |
||
217 | |||
218 | if (false === is_writable($outputDir)) { |
||
219 | throw new RuntimeException( |
||
220 | sprintf( |
||
221 | 'Expected "<comment>%s</comment>" to be writeable.', |
||
222 | $outputDir |
||
223 | ) |
||
224 | ); |
||
225 | } |
||
226 | |||
227 | if ($input->getOption(self::FORCE_OPT)) { |
||
228 | $this->fileSystem->remove($outputDir); |
||
229 | |||
230 | return; |
||
231 | } |
||
232 | |||
233 | if (false === is_dir($outputDir)) { |
||
234 | $canDeleteFile = $io->confirm( |
||
235 | sprintf( |
||
236 | 'Expected "<comment>%s</comment>" to be a directory but found a file instead. It will be ' |
||
237 | .'removed, do you wish to proceed?', |
||
238 | $outputDir |
||
239 | ), |
||
240 | false |
||
241 | ); |
||
242 | |||
243 | if (false === $canDeleteFile) { |
||
244 | return; |
||
245 | } |
||
246 | |||
247 | $this->fileSystem->remove($outputDir); |
||
248 | } else { |
||
249 | $canDeleteFile = $io->confirm( |
||
250 | sprintf( |
||
251 | 'The output directory "<comment>%s</comment>" already exists. Continuing will erase its' |
||
252 | .' content, do you wish to proceed?', |
||
253 | $outputDir |
||
254 | ), |
||
255 | false |
||
256 | ); |
||
257 | |||
258 | if (false === $canDeleteFile) { |
||
259 | return; |
||
260 | } |
||
261 | |||
262 | $this->fileSystem->remove($outputDir); |
||
263 | } |
||
264 | } |
||
265 | |||
266 | 14 | private function retrieveConfig(InputInterface $input, OutputStyle $io): Configuration |
|
307 | |||
308 | 14 | private function makeAbsolutePath(string $path): string |
|
316 | } |
||
317 |
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.