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 Handler implements SearchHandlerInterface |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @var \eZ\Publish\Core\Search\Elasticsearch\Content\Gateway |
||
| 24 | */ |
||
| 25 | protected $gateway; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var \eZ\Publish\Core\Search\Elasticsearch\Content\Gateway |
||
| 29 | */ |
||
| 30 | protected $locationGateway; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var \eZ\Publish\Core\Search\Elasticsearch\Content\MapperInterface |
||
| 34 | */ |
||
| 35 | protected $mapper; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Search result extractor. |
||
| 39 | * |
||
| 40 | * @var \eZ\Publish\Core\Search\Elasticsearch\Content\Extractor |
||
| 41 | */ |
||
| 42 | protected $extractor; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Identifier of Content document type in the search backend. |
||
| 46 | * |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | protected $contentDocumentTypeIdentifier; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Identifier of Location document type in the search backend. |
||
| 53 | * |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | protected $locationDocumentTypeIdentifier; |
||
| 57 | |||
| 58 | public function __construct( |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Finds content objects for the given query. |
||
| 76 | * |
||
| 77 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if Query criterion is not applicable to its target |
||
| 78 | * |
||
| 79 | * @param \eZ\Publish\API\Repository\Values\Content\Query $query |
||
| 80 | * @param array $languageFilter - a map of language related filters specifying languages query will be performed on. |
||
| 81 | * Also used to define which field languages are loaded for the returned content. |
||
| 82 | * Currently supports: <code>array("languages" => array(<language1>,..), "useAlwaysAvailable" => bool)</code> |
||
| 83 | * useAlwaysAvailable defaults to true to avoid exceptions on missing translations |
||
| 84 | * |
||
| 85 | * @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult |
||
| 86 | */ |
||
| 87 | View Code Duplication | public function findContent(Query $query, array $languageFilter = array()) |
|
| 96 | |||
| 97 | /** |
||
| 98 | * Performs a query for a single content object. |
||
| 99 | * |
||
| 100 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the object was not found by the query or due to permissions |
||
| 101 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if Criterion is not applicable to its target |
||
| 102 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if there is more than than one result matching the criterions |
||
| 103 | * |
||
| 104 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $filter |
||
| 105 | * @param array $languageFilter - a map of language related filters specifying languages query will be performed on. |
||
| 106 | * Also used to define which field languages are loaded for the returned content. |
||
| 107 | * Currently supports: <code>array("languages" => array(<language1>,..), "useAlwaysAvailable" => bool)</code> |
||
| 108 | * useAlwaysAvailable defaults to true to avoid exceptions on missing translations |
||
| 109 | * |
||
| 110 | * @return \eZ\Publish\SPI\Persistence\Content |
||
| 111 | */ |
||
| 112 | public function findSingle(Criterion $filter, array $languageFilter = array()) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Finds Locations for the given $query. |
||
| 137 | * |
||
| 138 | * @param \eZ\Publish\API\Repository\Values\Content\LocationQuery $query |
||
| 139 | * @param array $languageFilter - a map of language related filters specifying languages query will be performed on. |
||
| 140 | * Also used to define which field languages are loaded for the returned content. |
||
| 141 | * Currently supports: <code>array("languages" => array(<language1>,..), "useAlwaysAvailable" => bool)</code> |
||
| 142 | * useAlwaysAvailable defaults to true to avoid exceptions on missing translations |
||
| 143 | * |
||
| 144 | * @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult |
||
| 145 | */ |
||
| 146 | View Code Duplication | public function findLocations(LocationQuery $query, array $languageFilter = array()) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Suggests a list of values for the given prefix. |
||
| 158 | * |
||
| 159 | * @param string $prefix |
||
| 160 | * @param string[] $fieldPaths |
||
| 161 | * @param int $limit |
||
| 162 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $filter |
||
| 163 | */ |
||
| 164 | public function suggest($prefix, $fieldPaths = array(), $limit = 10, Criterion $filter = null) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Indexes a content object. |
||
| 171 | * |
||
| 172 | * @param \eZ\Publish\SPI\Persistence\Content $content |
||
| 173 | */ |
||
| 174 | public function indexContent(Content $content) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Indexes several content objects. |
||
| 183 | * |
||
| 184 | * @todo: This function and setCommit() is needed for Persistence\Solr for test speed but not part |
||
| 185 | * of interface for the reason described in Solr\Content\Search\Gateway\Native::bulkIndexContent |
||
| 186 | * Short: Bulk handling should be properly designed before added to the interface. |
||
| 187 | * |
||
| 188 | * @param \eZ\Publish\SPI\Persistence\Content[] $contentObjects |
||
| 189 | */ |
||
| 190 | public function bulkIndexContent(array $contentObjects) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Indexes a Location in the index storage. |
||
| 202 | * |
||
| 203 | * @param \eZ\Publish\SPI\Persistence\Content\Location $location |
||
| 204 | */ |
||
| 205 | public function indexLocation(Location $location) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Indexes several Locations. |
||
| 214 | * |
||
| 215 | * @todo: This function and setCommit() is needed for Persistence\Solr for test speed but not part |
||
| 216 | * of interface for the reason described in Solr\Content\Search\Gateway\Native::bulkIndexContent |
||
| 217 | * Short: Bulk handling should be properly designed before added to the interface. |
||
| 218 | * |
||
| 219 | * @param \eZ\Publish\SPI\Persistence\Content\Location[] $locations |
||
| 220 | */ |
||
| 221 | public function bulkIndexLocations(array $locations) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Deletes a content object from the index. |
||
| 233 | * |
||
| 234 | * @param int $contentId |
||
| 235 | * @param int|null $versionId |
||
| 236 | */ |
||
| 237 | public function deleteContent($contentId, $versionId = null) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Deletes a location from the index. |
||
| 252 | * |
||
| 253 | * @todo When we support Location-less Content, we will have to reindex instead of removing |
||
| 254 | * @todo Should we not already support the above? |
||
| 255 | * @todo The subtree could potentially be huge, so this implementation should scroll reindex |
||
| 256 | * |
||
| 257 | * @param mixed $locationId |
||
| 258 | * @param mixed $contentId @todo Make use of this, or remove if not needed. |
||
| 259 | */ |
||
| 260 | public function deleteLocation($locationId, $contentId) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Purges all contents from the index. |
||
| 325 | */ |
||
| 326 | public function purgeIndex() |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Set if index/delete actions should commit or if several actions is to be expected. |
||
| 334 | * |
||
| 335 | * This should be set to false before group of actions and true before the last one |
||
| 336 | * |
||
| 337 | * @param bool $commit |
||
| 338 | */ |
||
| 339 | public function setCommit($commit) |
||
| 343 | |||
| 344 | public function flush() |
||
| 348 | |||
| 349 | public function supports($capabilityFlag) |
||
| 353 | } |
||
| 354 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: