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 GenerateShortcodeCommand extends Command |
||
| 19 | { |
||
| 20 | const NAME = 'shortcode:generate'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var UrlShortenerInterface |
||
| 24 | */ |
||
| 25 | private $urlShortener; |
||
| 26 | /** |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | private $domainConfig; |
||
| 30 | /** |
||
| 31 | * @var TranslatorInterface |
||
| 32 | */ |
||
| 33 | private $translator; |
||
| 34 | |||
| 35 | 2 | public function __construct( |
|
| 36 | UrlShortenerInterface $urlShortener, |
||
| 37 | TranslatorInterface $translator, |
||
| 38 | array $domainConfig |
||
| 39 | ) { |
||
| 40 | 2 | $this->urlShortener = $urlShortener; |
|
| 41 | 2 | $this->translator = $translator; |
|
| 42 | 2 | $this->domainConfig = $domainConfig; |
|
| 43 | 2 | parent::__construct(null); |
|
| 44 | 2 | } |
|
| 45 | |||
| 46 | 2 | public function configure() |
|
| 74 | |||
| 75 | 2 | View Code Duplication | public function interact(InputInterface $input, OutputInterface $output) |
| 90 | |||
| 91 | 2 | public function execute(InputInterface $input, OutputInterface $output) |
|
| 92 | { |
||
| 93 | 2 | $io = new SymfonyStyle($input, $output); |
|
| 94 | 2 | $longUrl = $input->getArgument('longUrl'); |
|
| 95 | 2 | if (empty($longUrl)) { |
|
| 96 | $io->error($this->translator->translate('A URL was not provided!')); |
||
| 97 | return; |
||
| 98 | } |
||
| 99 | |||
| 100 | 2 | $tags = $input->getOption('tags'); |
|
| 101 | 2 | $processedTags = []; |
|
| 102 | 2 | foreach ($tags as $key => $tag) { |
|
| 103 | $explodedTags = \explode(',', $tag); |
||
| 104 | $processedTags = \array_merge($processedTags, $explodedTags); |
||
| 105 | } |
||
| 106 | 2 | $tags = $processedTags; |
|
| 107 | 2 | $customSlug = $input->getOption('customSlug'); |
|
| 108 | 2 | $maxVisits = $input->getOption('maxVisits'); |
|
| 109 | |||
| 110 | try { |
||
| 111 | 2 | $shortCode = $this->urlShortener->urlToShortCode( |
|
| 112 | 2 | new Uri($longUrl), |
|
| 113 | 2 | $tags, |
|
| 114 | 2 | $this->getOptionalDate($input, 'validSince'), |
|
| 115 | 2 | $this->getOptionalDate($input, 'validUntil'), |
|
| 116 | 2 | $customSlug, |
|
| 117 | 2 | $maxVisits !== null ? (int) $maxVisits : null |
|
| 118 | 1 | )->getShortCode(); |
|
| 119 | 1 | $shortUrl = (string) (new Uri())->withPath($shortCode) |
|
| 120 | 1 | ->withScheme($this->domainConfig['schema']) |
|
| 121 | 1 | ->withHost($this->domainConfig['hostname']); |
|
| 122 | |||
| 123 | 1 | $io->writeln([ |
|
| 124 | 1 | \sprintf('%s <info>%s</info>', $this->translator->translate('Processed long URL:'), $longUrl), |
|
| 125 | 1 | \sprintf('%s <info>%s</info>', $this->translator->translate('Generated short URL:'), $shortUrl), |
|
| 126 | ]); |
||
| 127 | 1 | } catch (InvalidUrlException $e) { |
|
| 128 | 1 | $io->error(\sprintf( |
|
| 129 | 1 | $this->translator->translate('Provided URL "%s" is invalid. Try with a different one.'), |
|
| 130 | 1 | $longUrl |
|
| 131 | )); |
||
| 132 | } catch (NonUniqueSlugException $e) { |
||
| 133 | $io->error(\sprintf( |
||
| 134 | $this->translator->translate( |
||
| 135 | 'Provided slug "%s" is already in use by another URL. Try with a different one.' |
||
| 136 | ), |
||
| 137 | $customSlug |
||
| 138 | )); |
||
| 139 | } |
||
| 140 | 2 | } |
|
| 141 | |||
| 142 | 2 | private function getOptionalDate(InputInterface $input, string $fieldName) |
|
| 147 | } |
||
| 148 |
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.