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 ScrapeStartCommand extends Command |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @var ManagerRegistry |
||
| 22 | */ |
||
| 23 | protected $doctrine; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var ScraperFactory |
||
| 27 | */ |
||
| 28 | protected $factory; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @param ManagerRegistry $doctrine |
||
| 32 | * @param ScraperFactory $factory |
||
| 33 | */ |
||
| 34 | public function __construct(ManagerRegistry $doctrine, ScraperFactory $factory) |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @inheritdoc |
||
| 44 | */ |
||
| 45 | View Code Duplication | protected function configure() |
|
| 58 | |||
| 59 | /** |
||
| 60 | * @inheritdoc |
||
| 61 | */ |
||
| 62 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 63 | { |
||
| 64 | if ($output->getVerbosity() >= OutputInterface::VERBOSITY_NORMAL) { |
||
| 65 | $dispatcher = $this->factory->getEventDispatcher(); |
||
| 66 | $dispatcher->addSubscriber(new ScrapeOutputSubscriber($output)); |
||
| 67 | } |
||
| 68 | |||
| 69 | $scrapers = $this->findScrapers($input->getArgument('scraper')); |
||
| 70 | foreach ($scrapers as $scraperEntity) { |
||
| 71 | $url = $scraperEntity->getUrl(); |
||
| 72 | |||
| 73 | $output->writeln(sprintf('Found scraper: <info>%s</info>', $url)); |
||
| 74 | $output->writeln(sprintf('- Start every <info>%s</info> hours', $scraperEntity->getStartFrequency())); |
||
| 75 | |||
| 76 | if ($date = $scraperEntity->getDatetimeLastStarted()) { |
||
| 77 | $output->writeln(sprintf('- Last started at <info>%s</info>', $date->format(DATE_RFC2822))); |
||
| 78 | |||
| 79 | $nextStart = $date->add(new \DateInterval(sprintf('PT%sH', $scraperEntity->getStartFrequency()))); |
||
| 80 | if ($nextStart > new \DateTime()) { |
||
| 81 | $output->writeln(sprintf('- Next start time at <info>%s</info>', $nextStart->format(DATE_RFC2822))); |
||
| 82 | |||
| 83 | continue; |
||
| 84 | } |
||
| 85 | } else { |
||
| 86 | $output->writeln('- Scraper has <info>never</info> started'); |
||
| 87 | } |
||
| 88 | |||
| 89 | $output->writeln('Starting scraper...'); |
||
| 90 | $output->writeln('-------------------'); |
||
| 91 | |||
| 92 | try { |
||
| 93 | $this->scrape($input, $scraperEntity); |
||
| 94 | } catch (CrawlException $e) { |
||
| 95 | $output->writeln(sprintf('<error>%s</error>', $e->getMessage())); |
||
| 96 | } |
||
| 97 | |||
| 98 | $output->writeln('-------------------'); |
||
| 99 | $output->writeln(''); |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @param integer[] $ids |
||
| 105 | * |
||
| 106 | * @return ScraperEntity[] |
||
| 107 | */ |
||
| 108 | View Code Duplication | protected function findScrapers(array $ids) |
|
| 118 | |||
| 119 | /** |
||
| 120 | * @param InputInterface $input |
||
| 121 | * @param Scraper $scraperEntity |
||
| 122 | */ |
||
| 123 | protected function scrape(InputInterface $input, $scraperEntity) |
||
| 143 | } |
||
| 144 |
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.