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