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 |
||
31 | class SearchService implements SearchServiceInterface |
||
32 | { |
||
33 | /** |
||
34 | * @var \eZ\Publish\Core\Repository\Repository |
||
35 | */ |
||
36 | protected $repository; |
||
37 | |||
38 | /** |
||
39 | * @var \eZ\Publish\SPI\Search\Handler |
||
40 | */ |
||
41 | protected $searchHandler; |
||
42 | |||
43 | /** |
||
44 | * @var array |
||
45 | */ |
||
46 | protected $settings; |
||
47 | |||
48 | /** |
||
49 | * @var \eZ\Publish\Core\Repository\Helper\DomainMapper |
||
50 | */ |
||
51 | protected $domainMapper; |
||
52 | |||
53 | /** |
||
54 | * @var \eZ\Publish\API\Repository\PermissionCriterionResolver |
||
55 | */ |
||
56 | protected $permissionCriterionResolver; |
||
57 | |||
58 | /** |
||
59 | * Setups service with reference to repository object that created it & corresponding handler. |
||
60 | * |
||
61 | * @param \eZ\Publish\API\Repository\Repository $repository |
||
62 | * @param \eZ\Publish\SPI\Search\Handler $searchHandler |
||
63 | * @param \eZ\Publish\Core\Repository\Helper\DomainMapper $domainMapper |
||
64 | * @param \eZ\Publish\API\Repository\PermissionCriterionResolver $permissionCriterionResolver |
||
65 | * @param array $settings |
||
66 | */ |
||
67 | View Code Duplication | public function __construct( |
|
83 | |||
84 | /** |
||
85 | * Finds content objects for the given query. |
||
86 | * |
||
87 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if query is not valid |
||
88 | * |
||
89 | * @param \eZ\Publish\API\Repository\Values\Content\Query $query |
||
90 | * @param array $languageFilter Configuration for specifying prioritized languages query will be performed on. |
||
91 | * Currently supports: <code>array("languages" => array(<language1>,..), "useAlwaysAvailable" => bool)</code> |
||
92 | * useAlwaysAvailable defaults to true to avoid exceptions on missing translations. |
||
93 | * @param bool $filterOnUserPermissions if true only the objects which the user is allowed to read are returned. |
||
94 | * |
||
95 | * @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult |
||
96 | */ |
||
97 | public function findContent(Query $query, array $languageFilter = array(), $filterOnUserPermissions = true) |
||
114 | |||
115 | /** |
||
116 | * Finds contentInfo objects for the given query. |
||
117 | * |
||
118 | * @see SearchServiceInterface::findContentInfo() |
||
119 | * |
||
120 | * @since 5.4.5 |
||
121 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if query is not valid |
||
122 | * |
||
123 | * @param \eZ\Publish\API\Repository\Values\Content\Query $query |
||
124 | * @param array $languageFilter - a map of filters for the returned fields. |
||
125 | * Currently supports: <code>array("languages" => array(<language1>,..), "useAlwaysAvailable" => bool)</code> |
||
126 | * useAlwaysAvailable defaults to true to avoid exceptions on missing translations. |
||
127 | * @param bool $filterOnUserPermissions if true (default) only the objects which is the user allowed to read are returned. |
||
128 | * |
||
129 | * @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult |
||
130 | */ |
||
131 | public function findContentInfo(Query $query, array $languageFilter = array(), $filterOnUserPermissions = true) |
||
142 | |||
143 | /** |
||
144 | * Finds SPI content info objects for the given query. |
||
145 | * |
||
146 | * Internal for use by {@link findContent} and {@link findContentInfo}. |
||
147 | * |
||
148 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if query is not valid |
||
149 | * |
||
150 | * @param \eZ\Publish\API\Repository\Values\Content\Query $query |
||
151 | * @param array $languageFilter - a map of filters for the returned fields. |
||
152 | * Currently supports: <code>array("languages" => array(<language1>,..), "useAlwaysAvailable" => bool)</code> |
||
153 | * useAlwaysAvailable defaults to true to avoid exceptions on missing translations. |
||
154 | * @param bool $filterOnUserPermissions if true only the objects which is the user allowed to read are returned. |
||
155 | * |
||
156 | * @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult With "raw" SPI contentInfo objects in result |
||
157 | */ |
||
158 | protected function internalFindContentInfo(Query $query, array $languageFilter = array(), $filterOnUserPermissions = true) |
||
189 | |||
190 | /** |
||
191 | * Checks that $criteria does not contain Location criterions. |
||
192 | * |
||
193 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
194 | * |
||
195 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion[] $criteria |
||
196 | * @param string $argumentName |
||
197 | */ |
||
198 | protected function validateContentCriteria(array $criteria, $argumentName) |
||
212 | |||
213 | /** |
||
214 | * Checks that $query does not contain Location sort clauses. |
||
215 | * |
||
216 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
217 | * |
||
218 | * @param \eZ\Publish\API\Repository\Values\Content\Query $query |
||
219 | */ |
||
220 | protected function validateContentSortClauses(Query $query) |
||
228 | |||
229 | /** |
||
230 | * Performs a query for a single content object. |
||
231 | * |
||
232 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the object was not found by the query or due to permissions |
||
233 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if criterion is not valid |
||
234 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if there is more than one result matching the criterions |
||
235 | * |
||
236 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $filter |
||
237 | * @param array $languageFilter Configuration for specifying prioritized languages query will be performed on. |
||
238 | * Currently supports: <code>array("languages" => array(<language1>,..), "useAlwaysAvailable" => bool)</code> |
||
239 | * useAlwaysAvailable defaults to true to avoid exceptions on missing translations. |
||
240 | * @param bool $filterOnUserPermissions if true only the objects which is the user allowed to read are returned. |
||
241 | * |
||
242 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
243 | */ |
||
244 | public function findSingle(Criterion $filter, array $languageFilter = array(), $filterOnUserPermissions = true) |
||
262 | |||
263 | /** |
||
264 | * Suggests a list of values for the given prefix. |
||
265 | * |
||
266 | * @param string $prefix |
||
267 | * @param string[] $fieldPaths |
||
268 | * @param int $limit |
||
269 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $filter |
||
270 | */ |
||
271 | public function suggest($prefix, $fieldPaths = array(), $limit = 10, Criterion $filter = null) |
||
274 | |||
275 | /** |
||
276 | * Finds Locations for the given query. |
||
277 | * |
||
278 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if query is not valid |
||
279 | * |
||
280 | * @param \eZ\Publish\API\Repository\Values\Content\LocationQuery $query |
||
281 | * @param array $languageFilter Configuration for specifying prioritized languages query will be performed on. |
||
282 | * Currently supports: <code>array("languages" => array(<language1>,..), "useAlwaysAvailable" => bool)</code> |
||
283 | * useAlwaysAvailable defaults to true to avoid exceptions on missing translations |
||
284 | * @param bool $filterOnUserPermissions if true only the objects which is the user allowed to read are returned. |
||
285 | * |
||
286 | * @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult |
||
287 | */ |
||
288 | public function findLocations(LocationQuery $query, array $languageFilter = array(), $filterOnUserPermissions = true) |
||
323 | |||
324 | /** |
||
325 | * Adds content, read Permission criteria if needed and return false if no access at all. |
||
326 | * |
||
327 | * @uses \eZ\Publish\API\Repository\PermissionCriterionResolver::getPermissionsCriterion() |
||
328 | * |
||
329 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterion |
||
330 | * |
||
331 | * @return bool|\eZ\Publish\API\Repository\Values\Content\Query\Criterion |
||
332 | */ |
||
333 | View Code Duplication | protected function addPermissionsCriterion(Criterion &$criterion) |
|
354 | |||
355 | public function supports($capabilityFlag) |
||
363 | } |
||
364 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.