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, ContentIndexer |
||
| 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 | * Content mapper. |
||
| 69 | * |
||
| 70 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Mapper |
||
| 71 | */ |
||
| 72 | protected $contentMapper; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Location locationMapper. |
||
| 76 | * |
||
| 77 | * @var \eZ\Publish\Core\Persistence\Legacy\Content\Location\Mapper |
||
| 78 | */ |
||
| 79 | protected $locationMapper; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Language handler. |
||
| 83 | * |
||
| 84 | * @var \eZ\Publish\SPI\Persistence\Content\Language\Handler |
||
| 85 | */ |
||
| 86 | protected $languageHandler; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Creates a new content handler. |
||
| 90 | * |
||
| 91 | * @param \eZ\Publish\Core\Search\Legacy\Content\Gateway $gateway |
||
| 92 | * @param \eZ\Publish\Core\Search\Legacy\Content\Location\Gateway $locationGateway |
||
| 93 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\Mapper $contentMapper |
||
| 94 | * @param \eZ\Publish\Core\Persistence\Legacy\Content\Location\Mapper $locationMapper |
||
| 95 | * @param \eZ\Publish\SPI\Persistence\Content\Language\Handler $languageHandler |
||
| 96 | */ |
||
| 97 | public function __construct( |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Finds content objects for the given query. |
||
| 113 | * |
||
| 114 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if Query criterion is not applicable to its target |
||
| 115 | * |
||
| 116 | * @param \eZ\Publish\API\Repository\Values\Content\Query $query |
||
| 117 | * @param array $languageFilter - a map of language related filters specifying languages query will be performed on. |
||
| 118 | * Also used to define which field languages are loaded for the returned content. |
||
| 119 | * Currently supports: <code>array("languages" => array(<language1>,..), "useAlwaysAvailable" => bool)</code> |
||
| 120 | * useAlwaysAvailable defaults to true to avoid exceptions on missing translations |
||
| 121 | * |
||
| 122 | * @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult |
||
| 123 | */ |
||
| 124 | View Code Duplication | public function findContent(Query $query, array $languageFilter = array()) |
|
| 178 | |||
| 179 | protected function extractMatchedLanguage($languageMask, $mainLanguageId, $languageSettings) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Performs a query for a single content object. |
||
| 196 | * |
||
| 197 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the object was not found by the query or due to permissions |
||
| 198 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if Criterion is not applicable to its target |
||
| 199 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if there is more than than one result matching the criterions |
||
| 200 | * |
||
| 201 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $filter |
||
| 202 | * @param array $languageFilter - a map of language related filters specifying languages query will be performed on. |
||
| 203 | * Also used to define which field languages are loaded for the returned content. |
||
| 204 | * Currently supports: <code>array("languages" => array(<language1>,..), "useAlwaysAvailable" => bool)</code> |
||
| 205 | * useAlwaysAvailable defaults to true to avoid exceptions on missing translations |
||
| 206 | * |
||
| 207 | * @return \eZ\Publish\SPI\Persistence\Content\ContentInfo |
||
| 208 | */ |
||
| 209 | public function findSingle(Criterion $filter, array $languageFilter = array()) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @see \eZ\Publish\SPI\Search\Handler::findLocations |
||
| 241 | */ |
||
| 242 | View Code Duplication | public function findLocations(LocationQuery $query, array $languageFilter = array()) |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Suggests a list of values for the given prefix. |
||
| 293 | * |
||
| 294 | * @param string $prefix |
||
| 295 | * @param string[] $fieldPaths |
||
| 296 | * @param int $limit |
||
| 297 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $filter |
||
| 298 | */ |
||
| 299 | public function suggest($prefix, $fieldPaths = array(), $limit = 10, Criterion $filter = null) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Indexes a content object. |
||
| 306 | * |
||
| 307 | * @param \eZ\Publish\SPI\Persistence\Content $content |
||
| 308 | */ |
||
| 309 | public function indexContent(Content $content) |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Deletes a content object from the index. |
||
| 316 | * |
||
| 317 | * @param int $contentId |
||
| 318 | * @param int|null $versionId |
||
| 319 | */ |
||
| 320 | public function deleteContent($contentId, $versionId = null) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Deletes a location from the index. |
||
| 327 | * |
||
| 328 | * @param mixed $locationId |
||
| 329 | * @param mixed $contentId |
||
| 330 | */ |
||
| 331 | public function deleteLocation($locationId, $contentId) |
||
| 335 | } |
||
| 336 |
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.