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 |
||
20 | class ImportRunCommand extends Command |
||
21 | { |
||
22 | /** |
||
23 | * @var ManagerRegistry |
||
24 | */ |
||
25 | protected $doctrine; |
||
26 | |||
27 | /** |
||
28 | * @var ImportFactory |
||
29 | */ |
||
30 | protected $importFactory; |
||
31 | |||
32 | /** |
||
33 | * @var LoggerInterface |
||
34 | */ |
||
35 | protected $logger; |
||
36 | |||
37 | /** |
||
38 | * @param ManagerRegistry $doctrine |
||
39 | * @param ImportFactory $importFactory |
||
40 | * @param LoggerInterface $logger |
||
41 | */ |
||
42 | View Code Duplication | public function __construct(ManagerRegistry $doctrine, ImportFactory $importFactory, LoggerInterface $logger) |
|
50 | |||
51 | /** |
||
52 | * @inheritdoc |
||
53 | */ |
||
54 | protected function configure() |
||
63 | |||
64 | /** |
||
65 | * @inheritdoc |
||
66 | */ |
||
67 | protected function execute(InputInterface $input, OutputInterface $output) |
||
68 | { |
||
69 | $ids = (array) $input->getArgument('id'); |
||
70 | |||
71 | if (empty($ids)) { |
||
72 | $feeds = $this->getFeedRepository()->findAll(); |
||
73 | } else { |
||
74 | $feeds = $this->getFeedRepository()->findBy(['id' => $ids]); |
||
75 | } |
||
76 | |||
77 | $dispatcher = $this->importFactory->getEventDispatcher(); |
||
78 | $dispatcher->addSubscriber(new ImportOutputSubscriber($output)); |
||
79 | |||
80 | $force = $input->getOption('force'); |
||
81 | |||
82 | if (empty($feeds)) { |
||
83 | $output->writeln('No feeds to import'); |
||
84 | |||
85 | return 1; |
||
86 | } |
||
87 | |||
88 | foreach ($feeds as $feed) { |
||
89 | if ($input->isInteractive()) { |
||
90 | $this->checkForUnfinishedImports($feed, $input, $output); |
||
91 | } |
||
92 | |||
93 | $this->runImport($output, $feed, $force); |
||
94 | } |
||
95 | |||
96 | return 0; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @param OutputInterface $output |
||
101 | * @param Feed $feed |
||
102 | * @param bool $force |
||
103 | */ |
||
104 | protected function runImport(OutputInterface $output, Feed $feed, $force = false) |
||
105 | { |
||
106 | $output->writeln( |
||
107 | sprintf( |
||
108 | 'Starting a new import for <info>%s</info> feed <info>%d</info>', |
||
109 | $feed->getOrigin()->getName(), |
||
110 | $feed->getId() |
||
111 | ) |
||
112 | ); |
||
113 | |||
114 | $import = $this->importFactory->createImport($feed, new \DateTime(), $force); |
||
115 | $output->writeln(sprintf('Created import <info>%d</info>', $import->getId())); |
||
116 | |||
117 | foreach ($import->getParts() as $part) { |
||
118 | $output->writeln(sprintf('Importing part <comment>%d</comment>', $part->getPosition())); |
||
119 | |||
120 | $job = $this->importFactory->createImportJob($part); |
||
121 | $job->setLogger($this->logger); |
||
122 | $job->run(); |
||
123 | } |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Checks feed for unfinished imports and gives the user an option to close them first. |
||
128 | * |
||
129 | * @param Feed $feed |
||
130 | * @param InputInterface $input |
||
131 | * @param OutputInterface $output |
||
132 | * |
||
133 | * @throws \Exception |
||
134 | */ |
||
135 | View Code Duplication | protected function checkForUnfinishedImports(Feed $feed, InputInterface $input, OutputInterface $output) |
|
136 | { |
||
137 | $helper = new QuestionHelper(); |
||
138 | |||
139 | foreach ($feed->getImports() as $import) { |
||
140 | if (!$import->isFinished()) { |
||
141 | $msg = sprintf( |
||
142 | 'Import <info>%d</info> for this feed is unfinished, close it now? [y] ', |
||
143 | $import->getId() |
||
144 | ); |
||
145 | |||
146 | $question = new ConfirmationQuestion($msg); |
||
147 | if ($helper->ask($input, $output, $question)) { |
||
148 | $command = 'io:import:close'; |
||
149 | $closeImport = $this->getApplication()->find($command); |
||
150 | $input = new ArrayInput(['command' => $command, 'import' => $import->getId()]); |
||
151 | $closeImport->run($input, $output); |
||
152 | } |
||
153 | } |
||
154 | } |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * @return FeedRepository |
||
159 | */ |
||
160 | protected function getFeedRepository() |
||
164 | } |
||
165 |
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.