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 |
||
| 30 | class CreateIndexCommand extends ContainerAwareCommand |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * @var \eZ\Publish\Core\Search\Legacy\Content\Handler |
||
| 34 | */ |
||
| 35 | private $searchHandler; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var \eZ\Publish\SPI\Persistence\Handler |
||
| 39 | */ |
||
| 40 | private $persistenceHandler; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var \eZ\Publish\Core\Persistence\Database\DatabaseHandler |
||
| 44 | */ |
||
| 45 | private $databaseHandler; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var \Psr\Log\LoggerInterface |
||
| 49 | */ |
||
| 50 | private $logger; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Initialize objects required by {@see execute()}. |
||
| 54 | * |
||
| 55 | * @param InputInterface $input |
||
| 56 | * @param OutputInterface $output |
||
| 57 | */ |
||
| 58 | public function initialize(InputInterface $input, OutputInterface $output) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * {@inheritdoc} |
||
| 76 | */ |
||
| 77 | protected function configure() |
||
| 89 | |||
| 90 | /** |
||
| 91 | * {@inheritdoc} |
||
| 92 | */ |
||
| 93 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 94 | { |
||
| 95 | @trigger_error( |
||
|
|
|||
| 96 | sprintf('%s is deprecated since 6.7. Use ezplatform:reindex command instead', $this->getName()), |
||
| 97 | E_USER_DEPRECATED |
||
| 98 | ); |
||
| 99 | |||
| 100 | $bulkCount = $input->getArgument('bulk_count'); |
||
| 101 | // Indexing Content |
||
| 102 | $totalCount = $this->getContentObjectsTotalCount( |
||
| 103 | $this->databaseHandler, ContentInfo::STATUS_PUBLISHED |
||
| 104 | ); |
||
| 105 | |||
| 106 | $query = $this->databaseHandler->createSelectQuery(); |
||
| 107 | $query->select('id', 'current_version') |
||
| 108 | ->from('ezcontentobject') |
||
| 109 | ->where($query->expr->eq('status', ContentInfo::STATUS_PUBLISHED)); |
||
| 110 | |||
| 111 | $stmt = $query->prepare(); |
||
| 112 | $stmt->execute(); |
||
| 113 | |||
| 114 | $this->searchHandler->purgeIndex(); |
||
| 115 | |||
| 116 | $output->writeln('Indexing Content...'); |
||
| 117 | |||
| 118 | /* @var \Symfony\Component\Console\Helper\ProgressHelper $progress */ |
||
| 119 | $progress = $this->getHelperSet()->get('progress'); |
||
| 120 | $progress->start($output, $totalCount); |
||
| 121 | $i = 0; |
||
| 122 | do { |
||
| 123 | $contentObjects = []; |
||
| 124 | View Code Duplication | for ($k = 0; $k <= $bulkCount; ++$k) { |
|
| 125 | if (!$row = $stmt->fetch(PDO::FETCH_ASSOC)) { |
||
| 126 | break; |
||
| 127 | } |
||
| 128 | try { |
||
| 129 | $contentObjects[] = $this->persistenceHandler->contentHandler()->load( |
||
| 130 | $row['id'], |
||
| 131 | $row['current_version'] |
||
| 132 | ); |
||
| 133 | } catch (NotFoundException $e) { |
||
| 134 | $this->logWarning($output, $progress, "Could not load current version of Content with id ${row['id']}, so skipped for indexing. Full exception: " . $e->getMessage()); |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | $this->searchHandler->bulkIndex( |
||
| 139 | $contentObjects, |
||
| 140 | function (Content $content, NotFoundException $e) use ($output, $progress) { |
||
| 141 | $this->logWarning($output, $progress, 'Content with id ' . $content->versionInfo->id . ' has missing data, so skipped for indexing. Full exception: ' . $e->getMessage() |
||
| 142 | ); |
||
| 143 | } |
||
| 144 | ); |
||
| 145 | |||
| 146 | $progress->advance($k); |
||
| 147 | } while (($i += $bulkCount) < $totalCount); |
||
| 148 | |||
| 149 | $progress->finish(); |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Get content objects total count. |
||
| 154 | * |
||
| 155 | * @param \eZ\Publish\Core\Persistence\Database\DatabaseHandler $databaseHandler |
||
| 156 | * @param int $contentObjectStatus ContentInfo constant |
||
| 157 | * |
||
| 158 | * @return int |
||
| 159 | */ |
||
| 160 | private function getContentObjectsTotalCount(DatabaseHandler $databaseHandler, $contentObjectStatus) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Log warning while progress helper is running. |
||
| 175 | * |
||
| 176 | * @param \Symfony\Component\Console\Output\OutputInterface $output |
||
| 177 | * @param \Symfony\Component\Console\Helper\ProgressHelper $progress |
||
| 178 | * @param $message |
||
| 179 | */ |
||
| 180 | private function logWarning(OutputInterface $output, ProgressHelper $progress, $message) |
||
| 188 | } |
||
| 189 |
If you suppress an error, we recommend checking for the error condition explicitly: