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 |
||
| 40 | class PopulateCommand extends Command |
||
| 41 | { |
||
| 42 | protected static $defaultName = 'fos:elastica:populate'; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var EventDispatcherInterface |
||
| 46 | */ |
||
| 47 | private $dispatcher; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var IndexManager |
||
| 51 | */ |
||
| 52 | private $indexManager; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var PagerProviderRegistry |
||
| 56 | */ |
||
| 57 | private $pagerProviderRegistry; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var PagerPersisterRegistry |
||
| 61 | */ |
||
| 62 | private $pagerPersisterRegistry; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var PagerPersisterInterface |
||
| 66 | */ |
||
| 67 | private $pagerPersister; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var Resetter |
||
| 71 | */ |
||
| 72 | private $resetter; |
||
| 73 | |||
| 74 | View Code Duplication | public function __construct( |
|
| 75 | EventDispatcherInterface $dispatcher, |
||
| 76 | IndexManager $indexManager, |
||
| 77 | PagerProviderRegistry $pagerProviderRegistry, |
||
| 78 | PagerPersisterRegistry $pagerPersisterRegistry, |
||
| 79 | Resetter $resetter |
||
| 80 | ) { |
||
| 81 | parent::__construct(); |
||
| 82 | |||
| 83 | $this->dispatcher = $dispatcher; |
||
| 84 | |||
| 85 | if (class_exists(LegacyEventDispatcherProxy::class)) { |
||
| 86 | $this->dispatcher = LegacyEventDispatcherProxy::decorate($dispatcher); |
||
| 87 | } |
||
| 88 | |||
| 89 | $this->indexManager = $indexManager; |
||
| 90 | $this->pagerProviderRegistry = $pagerProviderRegistry; |
||
| 91 | $this->pagerPersisterRegistry = $pagerPersisterRegistry; |
||
| 92 | $this->resetter = $resetter; |
||
| 93 | } |
||
| 94 | |||
| 95 | protected function configure() |
||
| 96 | { |
||
| 97 | $this |
||
| 98 | ->setName('fos:elastica:populate') |
||
| 99 | ->addOption('index', null, InputOption::VALUE_OPTIONAL, 'The index to repopulate') |
||
| 100 | ->addOption('type', null, InputOption::VALUE_OPTIONAL, 'The type to repopulate') |
||
| 101 | ->addOption('no-reset', null, InputOption::VALUE_NONE, 'Do not reset index before populating') |
||
| 102 | ->addOption('no-delete', null, InputOption::VALUE_NONE, 'Do not delete index after populate') |
||
| 103 | ->addOption('sleep', null, InputOption::VALUE_REQUIRED, 'Sleep time between persisting iterations (microseconds)', 0) |
||
| 104 | ->addOption('ignore-errors', null, InputOption::VALUE_NONE, 'Do not stop on errors') |
||
| 105 | ->addOption('no-overwrite-format', null, InputOption::VALUE_NONE, 'Prevent this command from overwriting ProgressBar\'s formats') |
||
| 106 | |||
| 107 | ->addOption('first-page', null, InputOption::VALUE_REQUIRED, 'The pager\'s page to start population from. Including the given page.', 1) |
||
| 108 | ->addOption('last-page', null, InputOption::VALUE_REQUIRED, 'The pager\'s page to end population on. Including the given page.', null) |
||
| 109 | ->addOption('max-per-page', null, InputOption::VALUE_REQUIRED, 'The pager\'s page size', 100) |
||
| 110 | ->addOption('pager-persister', null, InputOption::VALUE_REQUIRED, 'The pager persister to be used to populate the index', InPlacePagerPersister::NAME) |
||
| 111 | |||
| 112 | ->setDescription('Populates search indexes from providers') |
||
| 113 | ; |
||
| 114 | } |
||
| 115 | |||
| 116 | protected function initialize(InputInterface $input, OutputInterface $output) |
||
| 127 | |||
| 128 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Recreates an index, populates its types, and refreshes the index. |
||
| 179 | * |
||
| 180 | * @param OutputInterface $output |
||
| 181 | * @param string $index |
||
| 182 | * @param bool $reset |
||
| 183 | * @param array $options |
||
| 184 | */ |
||
| 185 | private function populateIndex(OutputInterface $output, $index, $reset, $options) |
||
| 204 | |||
| 205 | /** |
||
| 206 | * Deletes/remaps an index type, populates it, and refreshes the index. |
||
| 207 | * |
||
| 208 | * @param OutputInterface $output |
||
| 209 | * @param string $index |
||
| 210 | * @param string $type |
||
| 211 | * @param bool $reset |
||
| 212 | * @param array $options |
||
| 213 | */ |
||
| 214 | private function populateIndexType(OutputInterface $output, $index, $type, $reset, $options) |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Refreshes an index. |
||
| 276 | * |
||
| 277 | * @param OutputInterface $output |
||
| 278 | * @param string $index |
||
| 279 | */ |
||
| 280 | private function refreshIndex(OutputInterface $output, $index) |
||
| 286 | } |
||
| 287 |