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 |
||
| 23 | class DoctrineDatabase extends Gateway |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * Database handler. |
||
| 27 | * |
||
| 28 | * @var \eZ\Publish\Core\Persistence\Database\DatabaseHandler |
||
| 29 | */ |
||
| 30 | protected $handler; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Criteria converter. |
||
| 34 | * |
||
| 35 | * @var \eZ\Publish\Core\Search\Legacy\Content\Common\Gateway\CriteriaConverter |
||
| 36 | */ |
||
| 37 | protected $criteriaConverter; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Sort clause converter. |
||
| 41 | * |
||
| 42 | * @var \eZ\Publish\Core\Search\Legacy\Content\Common\Gateway\SortClauseConverter |
||
| 43 | */ |
||
| 44 | protected $sortClauseConverter; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Language handler. |
||
| 48 | * |
||
| 49 | * @var \eZ\Publish\SPI\Persistence\Content\Language\Handler |
||
| 50 | */ |
||
| 51 | protected $languageHandler; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Construct from handler handler. |
||
| 55 | * |
||
| 56 | * @param \eZ\Publish\Core\Persistence\Database\DatabaseHandler $handler |
||
| 57 | * @param \eZ\Publish\Core\Search\Legacy\Content\Common\Gateway\CriteriaConverter $criteriaConverter |
||
| 58 | * @param \eZ\Publish\Core\Search\Legacy\Content\Common\Gateway\SortClauseConverter $sortClauseConverter |
||
| 59 | * @param \eZ\Publish\SPI\Persistence\Content\Language\Handler $languageHandler |
||
| 60 | */ |
||
| 61 | View Code Duplication | public function __construct( |
|
| 72 | |||
| 73 | /** |
||
| 74 | * Returns a list of object satisfying the $filter. |
||
| 75 | * |
||
| 76 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if Criterion is not applicable to its target |
||
| 77 | * |
||
| 78 | * @param Criterion $criterion |
||
| 79 | * @param int $offset |
||
| 80 | * @param int $limit |
||
| 81 | * @param \eZ\Publish\API\Repository\Values\Content\Query\SortClause[] $sort |
||
| 82 | * @param array $languageFilter |
||
| 83 | * @param bool $doCount |
||
| 84 | * |
||
| 85 | * @return mixed[][] |
||
| 86 | */ |
||
| 87 | public function find( |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Generates a language mask from the given $languageSettings. |
||
| 115 | * |
||
| 116 | * @param array $languageSettings |
||
| 117 | * |
||
| 118 | * @return int |
||
| 119 | */ |
||
| 120 | protected function getLanguageMask(array $languageSettings) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Get query condition. |
||
| 136 | * |
||
| 137 | * @param Criterion $filter |
||
| 138 | * @param \eZ\Publish\Core\Persistence\Database\SelectQuery $query |
||
| 139 | * @param array $languageFilter |
||
| 140 | * |
||
| 141 | * @return string |
||
| 142 | */ |
||
| 143 | protected function getQueryCondition( |
||
| 144 | Criterion $filter, |
||
| 145 | SelectQuery $query, |
||
| 146 | array $languageFilter |
||
| 147 | ) { |
||
| 148 | $condition = $query->expr->lAnd( |
||
| 149 | $this->criteriaConverter->convertCriteria($query, $filter, $languageFilter), |
||
| 150 | $query->expr->eq( |
||
| 151 | 'ezcontentobject.status', |
||
| 152 | ContentInfo::STATUS_PUBLISHED |
||
| 153 | ), |
||
| 154 | $query->expr->eq( |
||
| 155 | 'ezcontentobject_version.status', |
||
| 156 | VersionInfo::STATUS_PUBLISHED |
||
| 157 | ) |
||
| 158 | ); |
||
| 159 | |||
| 160 | // If not main-languages query |
||
| 161 | View Code Duplication | if (!empty($languageFilter['languages'])) { |
|
| 162 | $condition = $query->expr->lAnd( |
||
| 163 | $condition, |
||
| 164 | $query->expr->gt( |
||
| 165 | $query->expr->bitAnd( |
||
| 166 | $this->handler->quoteColumn('language_mask', 'ezcontentobject'), |
||
| 167 | $query->bindValue( |
||
| 168 | $this->getLanguageMask($languageFilter), |
||
| 169 | null, |
||
| 170 | PDO::PARAM_INT |
||
| 171 | ) |
||
| 172 | ), |
||
| 173 | $query->bindValue(0, null, PDO::PARAM_INT) |
||
| 174 | ) |
||
| 175 | ); |
||
| 176 | } |
||
| 177 | |||
| 178 | return $condition; |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Get result count. |
||
| 183 | * |
||
| 184 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $filter |
||
| 185 | * @param array $languageFilter |
||
| 186 | * |
||
| 187 | * @return int |
||
| 188 | */ |
||
| 189 | protected function getResultCount(Criterion $filter, array $languageFilter) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Get sorted arrays of content IDs, which should be returned. |
||
| 215 | * |
||
| 216 | * @param Criterion $filter |
||
| 217 | * @param array $sort |
||
| 218 | * @param mixed $offset |
||
| 219 | * @param mixed $limit |
||
| 220 | * @param array $languageFilter |
||
| 221 | * |
||
| 222 | * @return int[] |
||
| 223 | */ |
||
| 224 | protected function getContentInfoList( |
||
| 283 | } |
||
| 284 |
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.