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