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:
Complex classes like ReindexCommand often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ReindexCommand, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class ReindexCommand extends ContainerAwareCommand |
||
27 | { |
||
28 | /** |
||
29 | * @var \eZ\Publish\Core\Search\Common\Indexer|\eZ\Publish\Core\Search\Common\IncrementalIndexer |
||
30 | */ |
||
31 | private $searchIndexer; |
||
32 | |||
33 | /** |
||
34 | * @var \Doctrine\DBAL\Connection |
||
35 | */ |
||
36 | private $connection; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | private $phpPath; |
||
42 | |||
43 | /** |
||
44 | * @var \Psr\Log\LoggerInterface |
||
45 | */ |
||
46 | private $logger; |
||
47 | |||
48 | /** |
||
49 | * Initialize objects required by {@see execute()}. |
||
50 | * |
||
51 | * @param InputInterface $input |
||
52 | * @param OutputInterface $output |
||
53 | */ |
||
54 | public function initialize(InputInterface $input, OutputInterface $output) |
||
69 | |||
70 | /** |
||
71 | * {@inheritdoc} |
||
72 | */ |
||
73 | protected function configure() |
||
138 | |||
139 | /** |
||
140 | * {@inheritdoc} |
||
141 | */ |
||
142 | protected function execute(InputInterface $input, OutputInterface $output) |
||
176 | |||
177 | protected function indexIncrementally(InputInterface $input, OutputInterface $output, $iterationCount, $commit) |
||
178 | { |
||
179 | if ($contentIds = $input->getOption('content-ids')) { |
||
180 | $contentIds = explode(',', $contentIds); |
||
181 | $output->writeln(sprintf( |
||
182 | 'Indexing list of content id\'s (%s)' . ($commit ? ', with commit' : ''), |
||
183 | count($contentIds) |
||
184 | )); |
||
185 | |||
186 | return $this->searchIndexer->updateSearchIndex($contentIds, $commit); |
||
187 | } |
||
188 | |||
189 | if ($since = $input->getOption('since')) { |
||
190 | $stmt = $this->getStatementContentSince(new DateTime($since)); |
||
191 | $count = (int)$this->getStatementContentSince(new DateTime($since), true)->fetchColumn(); |
||
192 | $purge = false; |
||
193 | } elseif ($locationId = (int) $input->getOption('subtree')) { |
||
194 | $stmt = $this->getStatementSubtree($locationId); |
||
195 | $count = (int) $this->getStatementSubtree($locationId, true)->fetchColumn(); |
||
196 | $purge = false; |
||
197 | } else { |
||
198 | $stmt = $this->getStatementContentAll(); |
||
199 | $count = (int) $this->getStatementContentAll(true)->fetchColumn(); |
||
200 | $purge = !$input->getOption('no-purge'); |
||
201 | } |
||
202 | |||
203 | if (!$count) { |
||
204 | $output->writeln('<error>Could not find any items to index, aborting.</error>'); |
||
205 | |||
206 | return 1; |
||
207 | } |
||
208 | |||
209 | $iterations = ceil($count / $iterationCount); |
||
210 | $processes = $input->getOption('processes'); |
||
211 | $processCount = $processes === 'auto' ? $this->getNumberOfCPUCores() - 1 : (int) $processes; |
||
212 | $processCount = min($iterations, $processCount); |
||
213 | $processMessage = $processCount > 1 ? "using $processCount parallel child processes" : 'using single (current) process'; |
||
214 | |||
215 | if ($purge) { |
||
216 | $output->writeln('Purging index...'); |
||
217 | $this->searchIndexer->purge(); |
||
218 | |||
219 | $output->writeln( |
||
220 | "<info>Re-Creating index for {$count} items across $iterations iteration(s), $processMessage:</info>" |
||
221 | ); |
||
222 | } else { |
||
223 | $output->writeln( |
||
224 | "<info>Refreshing index for {$count} items across $iterations iteration(s), $processMessage:</info>" |
||
225 | ); |
||
226 | } |
||
227 | |||
228 | $progress = new ProgressBar($output); |
||
229 | $progress->start($iterations); |
||
230 | |||
231 | if ($processCount > 1) { |
||
232 | $this->runParallelProcess($progress, $stmt, (int) $processCount, (int) $iterationCount, $commit); |
||
233 | } else { |
||
234 | // if we only have one process, or less iterations to warrant running several, we index it all inline |
||
235 | foreach ($this->fetchIteration($stmt, $iterationCount) as $contentIds) { |
||
236 | $this->searchIndexer->updateSearchIndex($contentIds, $commit); |
||
237 | $progress->advance(1); |
||
238 | } |
||
239 | } |
||
240 | |||
241 | $progress->finish(); |
||
242 | } |
||
243 | |||
244 | private function runParallelProcess(ProgressBar $progress, Statement $stmt, $processCount, $iterationCount, $commit) |
||
281 | |||
282 | /** |
||
283 | * @param DateTime $since |
||
284 | * @param bool $count |
||
285 | * |
||
286 | * @return \Doctrine\DBAL\Driver\Statement |
||
287 | */ |
||
288 | private function getStatementContentSince(DateTime $since, $count = false) |
||
300 | |||
301 | /** |
||
302 | * @param mixed $locationId |
||
303 | * @param bool $count |
||
304 | * |
||
305 | * @return \Doctrine\DBAL\Driver\Statement |
||
306 | */ |
||
307 | private function getStatementSubtree($locationId, $count = false) |
||
325 | |||
326 | /** |
||
327 | * @param bool $count |
||
328 | * |
||
329 | * @return \Doctrine\DBAL\Driver\Statement |
||
330 | */ |
||
331 | private function getStatementContentAll($count = false) |
||
341 | |||
342 | /** |
||
343 | * @param \Doctrine\DBAL\Driver\Statement $stmt |
||
344 | * @param int $iterationCount |
||
345 | * |
||
346 | * @return \Generator Return an array of arrays, each array contains content id's of $iterationCount. |
||
347 | */ |
||
348 | private function fetchIteration(Statement $stmt, $iterationCount) |
||
363 | |||
364 | /** |
||
365 | * @param array $contentIds |
||
366 | * @param bool $commit |
||
367 | * |
||
368 | * @return \Symfony\Component\Process\Process |
||
369 | */ |
||
370 | private function getPhpProcess(array $contentIds, $commit) |
||
386 | |||
387 | /** |
||
388 | * @return string |
||
389 | */ |
||
390 | private function getPhpPath() |
||
406 | |||
407 | /** |
||
408 | * @return int |
||
409 | */ |
||
410 | private function getNumberOfCPUCores() |
||
436 | } |
||
437 |
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.