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()) |
|
193 | |||
194 | protected function extractMatchedLanguage($languageMask, $mainLanguageId, $languageSettings) |
||
208 | |||
209 | /** |
||
210 | * Performs a query for a single content object. |
||
211 | * |
||
212 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the object was not found by the query or due to permissions |
||
213 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if Criterion is not applicable to its target |
||
214 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if there is more than than one result matching the criterions |
||
215 | * |
||
216 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $filter |
||
217 | * @param array $languageFilter - a map of language related filters specifying languages query will be performed on. |
||
218 | * Also used to define which field languages are loaded for the returned content. |
||
219 | * Currently supports: <code>array("languages" => array(<language1>,..), "useAlwaysAvailable" => bool)</code> |
||
220 | * useAlwaysAvailable defaults to true to avoid exceptions on missing translations |
||
221 | * |
||
222 | * @return \eZ\Publish\SPI\Persistence\Content\ContentInfo |
||
223 | */ |
||
224 | public function findSingle(Criterion $filter, array $languageFilter = array()) |
||
253 | |||
254 | /** |
||
255 | * @see \eZ\Publish\SPI\Search\Handler::findLocations |
||
256 | */ |
||
257 | View Code Duplication | public function findLocations(LocationQuery $query, array $languageFilter = array()) |
|
301 | |||
302 | /** |
||
303 | * Suggests a list of values for the given prefix. |
||
304 | * |
||
305 | * @param string $prefix |
||
306 | * @param string[] $fieldPaths |
||
307 | * @param int $limit |
||
308 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $filter |
||
309 | * @throws NotImplementedException |
||
310 | */ |
||
311 | public function suggest($prefix, $fieldPaths = array(), $limit = 10, Criterion $filter = null) |
||
315 | |||
316 | /** |
||
317 | * Indexes a content object. |
||
318 | * |
||
319 | * @param \eZ\Publish\SPI\Persistence\Content $content |
||
320 | */ |
||
321 | public function indexContent(Content $content) |
||
327 | |||
328 | /** |
||
329 | * Bulk index list of content objects. |
||
330 | * |
||
331 | * @param \eZ\Publish\SPI\Persistence\Content[] $contentList |
||
332 | * @param callable $errorCallback (Content $content, NotFoundException $e) |
||
333 | */ |
||
334 | public function bulkIndex(array $contentList, callable $errorCallback) |
||
347 | |||
348 | /** |
||
349 | * @param \eZ\Publish\SPI\Persistence\Content\Location $location |
||
350 | */ |
||
351 | public function indexLocation(Location $location) |
||
355 | |||
356 | /** |
||
357 | * Deletes a content object from the index. |
||
358 | * |
||
359 | * @param int $contentId |
||
360 | * @param int|null $versionId |
||
361 | */ |
||
362 | public function deleteContent($contentId, $versionId = null) |
||
366 | |||
367 | /** |
||
368 | * Deletes a location from the index. |
||
369 | * |
||
370 | * @param mixed $locationId |
||
371 | * @param mixed $contentId |
||
372 | */ |
||
373 | public function deleteLocation($locationId, $contentId) |
||
377 | |||
378 | /** |
||
379 | * Purges all contents from the index. |
||
380 | */ |
||
381 | public function purgeIndex() |
||
385 | |||
386 | /** |
||
387 | * Commits the data to the index, making it available for search. |
||
388 | * |
||
389 | * @param bool $flush |
||
390 | */ |
||
391 | public function commit($flush = false) |
||
395 | } |
||
396 |
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.