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 | class ResolveUrlCommand extends Command |
||
17 | { |
||
18 | /** |
||
19 | * @var UrlShortenerInterface |
||
20 | */ |
||
21 | private $urlShortener; |
||
22 | /** |
||
23 | * @var TranslatorInterface |
||
24 | */ |
||
25 | private $translator; |
||
26 | |||
27 | /** |
||
28 | * ResolveUrlCommand constructor. |
||
29 | * @param UrlShortenerInterface $urlShortener |
||
30 | * @param TranslatorInterface $translator |
||
31 | * |
||
32 | * @Inject({UrlShortener::class, "translator"}) |
||
33 | */ |
||
34 | 3 | public function __construct(UrlShortenerInterface $urlShortener, TranslatorInterface $translator) |
|
40 | |||
41 | 3 | public function configure() |
|
42 | { |
||
43 | 3 | $this->setName('shortcode:parse') |
|
44 | 3 | ->setDescription($this->translator->translate('Returns the long URL behind a short code')) |
|
45 | 3 | ->addArgument( |
|
46 | 3 | 'shortCode', |
|
47 | 3 | InputArgument::REQUIRED, |
|
48 | 3 | $this->translator->translate('The short code to parse') |
|
49 | ); |
||
50 | 3 | } |
|
51 | |||
52 | 3 | View Code Duplication | public function interact(InputInterface $input, OutputInterface $output) |
|
|||
53 | { |
||
54 | 3 | $shortCode = $input->getArgument('shortCode'); |
|
55 | 3 | if (! empty($shortCode)) { |
|
56 | 3 | return; |
|
57 | } |
||
58 | |||
59 | /** @var QuestionHelper $helper */ |
||
60 | $helper = $this->getHelper('question'); |
||
61 | $question = new Question(sprintf( |
||
62 | '<question>%s</question> ', |
||
63 | $this->translator->translate('A short code was not provided. Which short code do you want to parse?:') |
||
64 | )); |
||
65 | |||
66 | $shortCode = $helper->ask($input, $output, $question); |
||
67 | if (! empty($shortCode)) { |
||
68 | $input->setArgument('shortCode', $shortCode); |
||
69 | } |
||
70 | } |
||
71 | |||
72 | 3 | public function execute(InputInterface $input, OutputInterface $output) |
|
93 | } |
||
94 |
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.