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 |
||
| 51 | class Handler implements SearchHandlerInterface |
||
| 52 | { |
||
| 53 | /** |
||
| 54 | * Content locator gateway. |
||
| 55 | * |
||
| 56 | * @var \eZ\Publish\Core\Search\Legacy\Content\Gateway |
||
| 57 | */ |
||
| 58 | protected $gateway; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Location locator gateway. |
||
| 62 | * |
||
| 63 | * @var \eZ\Publish\Core\Search\Legacy\Content\Location\Gateway |
||
| 64 | */ |
||
| 65 | protected $locationGateway; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Word indexer gateway. |
||
| 69 | * |
||
| 70 | * @var \eZ\Publish\Core\Search\Legacy\Content\WordIndexer\Gateway |
||
| 71 | */ |
||
| 72 | protected $indexerGateway; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Content mapper. |
||
| 76 | * |
||
| 77 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Mapper |
||
| 78 | */ |
||
| 79 | protected $contentMapper; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Location locationMapper. |
||
| 83 | * |
||
| 84 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Location\Mapper |
||
| 85 | */ |
||
| 86 | protected $locationMapper; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Language handler. |
||
| 90 | * |
||
| 91 | * @var \eZ\Publish\SPI\Persistence\Content\Language\Handler |
||
| 92 | */ |
||
| 93 | protected $languageHandler; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Creates a new content handler. |
||
| 97 | * |
||
| 98 | * @param \eZ\Publish\Core\Search\Legacy\Content\Gateway $gateway |
||
| 99 | * @param \eZ\Publish\Core\Search\Legacy\Content\Location\Gateway $locationGateway |
||
| 100 | * @param \eZ\Publish\Core\Search\Legacy\Content\WordIndexer\Gateway $indexerGateway |
||
| 101 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\Mapper $contentMapper |
||
| 102 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\Location\Mapper $locationMapper |
||
| 103 | * @param \eZ\Publish\SPI\Persistence\Content\Language\Handler $languageHandler |
||
| 104 | */ |
||
| 105 | public function __construct( |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Finds content objects for the given query. |
||
| 123 | * |
||
| 124 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if Query criterion is not applicable to its target |
||
| 125 | * |
||
| 126 | * @param \eZ\Publish\API\Repository\Values\Content\Query $query |
||
| 127 | * @param array $languageFilter - a map of language related filters specifying languages query will be performed on. |
||
| 128 | * Also used to define which field languages are loaded for the returned content. |
||
| 129 | * Currently supports: <code>array("languages" => array(<language1>,..), "useAlwaysAvailable" => bool)</code> |
||
| 130 | * useAlwaysAvailable defaults to true to avoid exceptions on missing translations |
||
| 131 | * |
||
| 132 | * @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult |
||
| 133 | */ |
||
| 134 | View Code Duplication | public function findContent(Query $query, array $languageFilter = array()) |
|
| 188 | |||
| 189 | protected function extractMatchedLanguage($languageMask, $mainLanguageId, $languageSettings) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Performs a query for a single content object. |
||
| 206 | * |
||
| 207 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the object was not found by the query or due to permissions |
||
| 208 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if Criterion is not applicable to its target |
||
| 209 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if there is more than than one result matching the criterions |
||
| 210 | * |
||
| 211 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $filter |
||
| 212 | * @param array $languageFilter - a map of language related filters specifying languages query will be performed on. |
||
| 213 | * Also used to define which field languages are loaded for the returned content. |
||
| 214 | * Currently supports: <code>array("languages" => array(<language1>,..), "useAlwaysAvailable" => bool)</code> |
||
| 215 | * useAlwaysAvailable defaults to true to avoid exceptions on missing translations |
||
| 216 | * |
||
| 217 | * @return \eZ\Publish\SPI\Persistence\Content\ContentInfo |
||
| 218 | */ |
||
| 219 | public function findSingle(Criterion $filter, array $languageFilter = array()) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @see \eZ\Publish\SPI\Search\Handler::findLocations |
||
| 251 | */ |
||
| 252 | View Code Duplication | public function findLocations(LocationQuery $query, array $languageFilter = array()) |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Suggests a list of values for the given prefix. |
||
| 303 | * |
||
| 304 | * @param string $prefix |
||
| 305 | * @param string[] $fieldPaths |
||
| 306 | * @param int $limit |
||
| 307 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $filter |
||
| 308 | */ |
||
| 309 | public function suggest($prefix, $fieldPaths = array(), $limit = 10, Criterion $filter = null) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Indexes a content object. |
||
| 316 | * |
||
| 317 | * @param \eZ\Publish\SPI\Persistence\Content $content |
||
| 318 | */ |
||
| 319 | public function indexContent(Content $content) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @param \eZ\Publish\SPI\Persistence\Content\Location $location |
||
| 326 | */ |
||
| 327 | public function indexLocation(Location $location) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Deletes a content object from the index. |
||
| 334 | * |
||
| 335 | * @param int $contentId |
||
| 336 | * @param int|null $versionId |
||
| 337 | */ |
||
| 338 | public function deleteContent($contentId, $versionId = null) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Deletes a location from the index. |
||
| 345 | * |
||
| 346 | * @param mixed $locationId |
||
| 347 | * @param mixed $contentId |
||
| 348 | */ |
||
| 349 | public function deleteLocation($locationId, $contentId) |
||
| 353 | } |
||
| 354 |
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.