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 |
||
23 | class Handler implements SearchHandlerInterface |
||
24 | { |
||
25 | /** |
||
26 | * @var \eZ\Publish\Core\Search\Elasticsearch\Content\Gateway |
||
27 | */ |
||
28 | protected $gateway; |
||
29 | |||
30 | /** |
||
31 | * @var \eZ\Publish\Core\Search\Elasticsearch\Content\Gateway |
||
32 | */ |
||
33 | protected $locationGateway; |
||
34 | |||
35 | /** |
||
36 | * @var \eZ\Publish\Core\Search\Elasticsearch\Content\MapperInterface |
||
37 | */ |
||
38 | protected $mapper; |
||
39 | |||
40 | /** |
||
41 | * Search result extractor. |
||
42 | * |
||
43 | * @var \eZ\Publish\Core\Search\Elasticsearch\Content\Extractor |
||
44 | */ |
||
45 | protected $extractor; |
||
46 | |||
47 | /** |
||
48 | * Identifier of Content document type in the search backend. |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $contentDocumentTypeIdentifier; |
||
53 | |||
54 | /** |
||
55 | * Identifier of Location document type in the search backend. |
||
56 | * |
||
57 | * @var string |
||
58 | */ |
||
59 | protected $locationDocumentTypeIdentifier; |
||
60 | |||
61 | public function __construct( |
||
76 | |||
77 | /** |
||
78 | * Finds content objects for the given query. |
||
79 | * |
||
80 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if Query criterion is not applicable to its target |
||
81 | * |
||
82 | * @param \eZ\Publish\API\Repository\Values\Content\Query $query |
||
83 | * @param array $languageFilter - a map of language related filters specifying languages query will be performed on. |
||
84 | * Also used to define which field languages are loaded for the returned content. |
||
85 | * Currently supports: <code>array("languages" => array(<language1>,..), "useAlwaysAvailable" => bool)</code> |
||
86 | * useAlwaysAvailable defaults to true to avoid exceptions on missing translations |
||
87 | * |
||
88 | * @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult |
||
89 | */ |
||
90 | View Code Duplication | public function findContent(Query $query, array $languageFilter = array()) |
|
99 | |||
100 | /** |
||
101 | * Performs a query for a single content object. |
||
102 | * |
||
103 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the object was not found by the query or due to permissions |
||
104 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if Criterion is not applicable to its target |
||
105 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if there is more than than one result matching the criterions |
||
106 | * |
||
107 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $filter |
||
108 | * @param array $languageFilter - a map of language related filters specifying languages query will be performed on. |
||
109 | * Also used to define which field languages are loaded for the returned content. |
||
110 | * Currently supports: <code>array("languages" => array(<language1>,..), "useAlwaysAvailable" => bool)</code> |
||
111 | * useAlwaysAvailable defaults to true to avoid exceptions on missing translations |
||
112 | * |
||
113 | * @return \eZ\Publish\SPI\Persistence\Content |
||
114 | */ |
||
115 | public function findSingle(Criterion $filter, array $languageFilter = array()) |
||
137 | |||
138 | /** |
||
139 | * Finds Locations for the given $query. |
||
140 | * |
||
141 | * @param \eZ\Publish\API\Repository\Values\Content\LocationQuery $query |
||
142 | * @param array $languageFilter - a map of language related filters specifying languages query will be performed on. |
||
143 | * Also used to define which field languages are loaded for the returned content. |
||
144 | * Currently supports: <code>array("languages" => array(<language1>,..), "useAlwaysAvailable" => bool)</code> |
||
145 | * useAlwaysAvailable defaults to true to avoid exceptions on missing translations |
||
146 | * |
||
147 | * @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult |
||
148 | */ |
||
149 | View Code Duplication | public function findLocations(LocationQuery $query, array $languageFilter = array()) |
|
158 | |||
159 | /** |
||
160 | * Suggests a list of values for the given prefix. |
||
161 | * |
||
162 | * @param string $prefix |
||
163 | * @param string[] $fieldPaths |
||
164 | * @param int $limit |
||
165 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $filter |
||
166 | */ |
||
167 | public function suggest($prefix, $fieldPaths = array(), $limit = 10, Criterion $filter = null) |
||
171 | |||
172 | /** |
||
173 | * Indexes a content object. |
||
174 | * |
||
175 | * @param \eZ\Publish\SPI\Persistence\Content $content |
||
176 | */ |
||
177 | public function indexContent(Content $content) |
||
183 | |||
184 | /** |
||
185 | * Indexes several content objects. |
||
186 | * |
||
187 | * @todo: This function and setCommit() is needed for Persistence\Solr for test speed but not part |
||
188 | * of interface for the reason described in Solr\Content\Search\Gateway\Native::bulkIndexContent |
||
189 | * Short: Bulk handling should be properly designed before added to the interface. |
||
190 | * |
||
191 | * @param \eZ\Publish\SPI\Persistence\Content[] $contentObjects |
||
192 | */ |
||
193 | public function bulkIndexContent(array $contentObjects) |
||
202 | |||
203 | /** |
||
204 | * Indexes a Location in the index storage. |
||
205 | * |
||
206 | * @param \eZ\Publish\SPI\Persistence\Content\Location $location |
||
207 | */ |
||
208 | public function indexLocation(Location $location) |
||
214 | |||
215 | /** |
||
216 | * Indexes several Locations. |
||
217 | * |
||
218 | * @todo: This function and setCommit() is needed for Persistence\Solr for test speed but not part |
||
219 | * of interface for the reason described in Solr\Content\Search\Gateway\Native::bulkIndexContent |
||
220 | * Short: Bulk handling should be properly designed before added to the interface. |
||
221 | * |
||
222 | * @param \eZ\Publish\SPI\Persistence\Content\Location[] $locations |
||
223 | */ |
||
224 | public function bulkIndexLocations(array $locations) |
||
233 | |||
234 | /** |
||
235 | * Deletes a content object from the index. |
||
236 | * |
||
237 | * @param int $contentId |
||
238 | * @param int|null $versionId |
||
239 | */ |
||
240 | public function deleteContent($contentId, $versionId = null) |
||
252 | |||
253 | /** |
||
254 | * Deletes a location from the index. |
||
255 | * |
||
256 | * @todo When we support Location-less Content, we will have to reindex instead of removing |
||
257 | * @todo Should we not already support the above? |
||
258 | * @todo The subtree could potentially be huge, so this implementation should scroll reindex |
||
259 | * |
||
260 | * @param mixed $locationId |
||
261 | * @param mixed $contentId @todo Make use of this, or remove if not needed. |
||
262 | */ |
||
263 | public function deleteLocation($locationId, $contentId) |
||
325 | |||
326 | /** |
||
327 | * Purges all contents from the index. |
||
328 | * |
||
329 | * @todo: Make this public API? |
||
330 | */ |
||
331 | public function purgeIndex() |
||
336 | |||
337 | /** |
||
338 | * Set if index/delete actions should commit or if several actions is to be expected. |
||
339 | * |
||
340 | * This should be set to false before group of actions and true before the last one |
||
341 | * |
||
342 | * @param bool $commit |
||
343 | */ |
||
344 | public function setCommit($commit) |
||
348 | |||
349 | public function flush() |
||
353 | } |
||
354 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: