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