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 |
||
| 50 | class Handler implements SearchHandlerInterface |
||
| 51 | { |
||
| 52 | /** |
||
| 53 | * Content locator gateway. |
||
| 54 | * |
||
| 55 | * @var \eZ\Publish\Core\Search\Legacy\Content\Gateway |
||
| 56 | */ |
||
| 57 | protected $gateway; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Location locator gateway. |
||
| 61 | * |
||
| 62 | * @var \eZ\Publish\Core\Search\Legacy\Content\Location\Gateway |
||
| 63 | */ |
||
| 64 | protected $locationGateway; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Word indexer gateway. |
||
| 68 | * |
||
| 69 | * @var \eZ\Publish\Core\Search\Legacy\Content\WordIndexer\Gateway |
||
| 70 | */ |
||
| 71 | protected $indexerGateway; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Content mapper. |
||
| 75 | * |
||
| 76 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Mapper |
||
| 77 | */ |
||
| 78 | protected $contentMapper; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Location locationMapper. |
||
| 82 | * |
||
| 83 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Location\Mapper |
||
| 84 | */ |
||
| 85 | protected $locationMapper; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Language handler. |
||
| 89 | * |
||
| 90 | * @var \eZ\Publish\SPI\Persistence\Content\Language\Handler |
||
| 91 | */ |
||
| 92 | protected $languageHandler; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * FullText mapper. |
||
| 96 | * |
||
| 97 | * @var \eZ\Publish\Core\Search\Legacy\Content\Mapper\FullTextMapper |
||
| 98 | */ |
||
| 99 | protected $mapper; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Creates a new content handler. |
||
| 103 | * |
||
| 104 | * @param \eZ\Publish\Core\Search\Legacy\Content\Gateway $gateway |
||
| 105 | * @param \eZ\Publish\Core\Search\Legacy\Content\Location\Gateway $locationGateway |
||
| 106 | * @param \eZ\Publish\Core\Search\Legacy\Content\WordIndexer\Gateway $indexerGateway |
||
| 107 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\Mapper $contentMapper |
||
| 108 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\Location\Mapper $locationMapper |
||
| 109 | * @param \eZ\Publish\SPI\Persistence\Content\Language\Handler $languageHandler |
||
| 110 | * @param \eZ\Publish\Core\Search\Legacy\Content\Mapper\FullTextMapper $mapper |
||
| 111 | */ |
||
| 112 | public function __construct( |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Finds content objects for the given query. |
||
| 132 | * |
||
| 133 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if Query criterion is not applicable to its target |
||
| 134 | * |
||
| 135 | * @param \eZ\Publish\API\Repository\Values\Content\Query $query |
||
| 136 | * @param array $languageFilter - a map of language related filters specifying languages query will be performed on. |
||
| 137 | * Also used to define which field languages are loaded for the returned content. |
||
| 138 | * Currently supports: <code>array("languages" => array(<language1>,..), "useAlwaysAvailable" => bool)</code> |
||
| 139 | * useAlwaysAvailable defaults to true to avoid exceptions on missing translations |
||
| 140 | * |
||
| 141 | * @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult |
||
| 142 | */ |
||
| 143 | View Code Duplication | public function findContent(Query $query, array $languageFilter = array()) |
|
| 197 | |||
| 198 | protected function extractMatchedLanguage($languageMask, $mainLanguageId, $languageSettings) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Performs a query for a single content object. |
||
| 215 | * |
||
| 216 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the object was not found by the query or due to permissions |
||
| 217 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if Criterion is not applicable to its target |
||
| 218 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if there is more than than one result matching the criterions |
||
| 219 | * |
||
| 220 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $filter |
||
| 221 | * @param array $languageFilter - a map of language related filters specifying languages query will be performed on. |
||
| 222 | * Also used to define which field languages are loaded for the returned content. |
||
| 223 | * Currently supports: <code>array("languages" => array(<language1>,..), "useAlwaysAvailable" => bool)</code> |
||
| 224 | * useAlwaysAvailable defaults to true to avoid exceptions on missing translations |
||
| 225 | * |
||
| 226 | * @return \eZ\Publish\SPI\Persistence\Content\ContentInfo |
||
| 227 | */ |
||
| 228 | public function findSingle(Criterion $filter, array $languageFilter = array()) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @see \eZ\Publish\SPI\Search\Handler::findLocations |
||
| 260 | */ |
||
| 261 | View Code Duplication | public function findLocations(LocationQuery $query, array $languageFilter = array()) |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Suggests a list of values for the given prefix. |
||
| 312 | * |
||
| 313 | * @param string $prefix |
||
| 314 | * @param string[] $fieldPaths |
||
| 315 | * @param int $limit |
||
| 316 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $filter |
||
| 317 | * @throws NotImplementedException |
||
| 318 | */ |
||
| 319 | public function suggest($prefix, $fieldPaths = array(), $limit = 10, Criterion $filter = null) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Indexes a content object. |
||
| 326 | * |
||
| 327 | * @param \eZ\Publish\SPI\Persistence\Content $content |
||
| 328 | */ |
||
| 329 | public function indexContent(Content $content) |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Bulk index list of content objects. |
||
| 338 | * |
||
| 339 | * @param \eZ\Publish\SPI\Persistence\Content[] $contentList |
||
| 340 | * @param callable $errorCallback (Content $content, NotFoundException $e) |
||
| 341 | */ |
||
| 342 | public function bulkIndex(array $contentList, callable $errorCallback) |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @param \eZ\Publish\SPI\Persistence\Content\Location $location |
||
| 358 | */ |
||
| 359 | public function indexLocation(Location $location) |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Deletes a content object from the index. |
||
| 366 | * |
||
| 367 | * @param int $contentId |
||
| 368 | * @param int|null $versionId |
||
| 369 | */ |
||
| 370 | public function deleteContent($contentId, $versionId = null) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Deletes a location from the index. |
||
| 377 | * |
||
| 378 | * @param mixed $locationId |
||
| 379 | * @param mixed $contentId |
||
| 380 | */ |
||
| 381 | public function deleteLocation($locationId, $contentId) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Purges all contents from the index. |
||
| 388 | */ |
||
| 389 | public function purgeIndex() |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Commits the data to the index, making it available for search. |
||
| 396 | * |
||
| 397 | * @param bool $flush |
||
| 398 | */ |
||
| 399 | public function commit($flush = false) |
||
| 403 | } |
||
| 404 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.