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:
Complex classes like SearchService often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SearchService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
33 | class SearchService implements SearchServiceInterface |
||
34 | { |
||
35 | /** |
||
36 | * @var \eZ\Publish\Core\Repository\Repository |
||
37 | */ |
||
38 | protected $repository; |
||
39 | |||
40 | /** |
||
41 | * @var \eZ\Publish\SPI\Search\Handler |
||
42 | */ |
||
43 | protected $searchHandler; |
||
44 | |||
45 | /** |
||
46 | * @var array |
||
47 | */ |
||
48 | protected $settings; |
||
49 | |||
50 | /** |
||
51 | * @var \eZ\Publish\Core\Repository\Helper\DomainMapper |
||
52 | */ |
||
53 | protected $domainMapper; |
||
54 | |||
55 | /** |
||
56 | * @var \eZ\Publish\API\Repository\PermissionCriterionResolver |
||
57 | */ |
||
58 | protected $permissionCriterionResolver; |
||
59 | |||
60 | /** |
||
61 | * @var \eZ\Publish\Core\Search\Common\BackgroundIndexer |
||
62 | */ |
||
63 | protected $backgroundIndexer; |
||
64 | |||
65 | /** |
||
66 | * Setups service with reference to repository object that created it & corresponding handler. |
||
67 | * |
||
68 | * @param \eZ\Publish\API\Repository\Repository $repository |
||
69 | * @param \eZ\Publish\SPI\Search\Handler $searchHandler |
||
70 | * @param \eZ\Publish\Core\Repository\Helper\DomainMapper $domainMapper |
||
71 | * @param \eZ\Publish\API\Repository\PermissionCriterionResolver $permissionCriterionResolver |
||
72 | * @param \eZ\Publish\Core\Search\Common\BackgroundIndexer $backgroundIndexer |
||
73 | * @param array $settings |
||
74 | */ |
||
75 | View Code Duplication | public function __construct( |
|
93 | |||
94 | /** |
||
95 | * Finds content objects for the given query. |
||
96 | * |
||
97 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if query is not valid |
||
98 | * |
||
99 | * @param \eZ\Publish\API\Repository\Values\Content\Query $query |
||
100 | * @param array $languageFilter Configuration for specifying prioritized languages query will be performed on. |
||
101 | * Currently supports: <code>array("languages" => array(<language1>,..), "useAlwaysAvailable" => bool)</code> |
||
102 | * useAlwaysAvailable defaults to true to avoid exceptions on missing translations. |
||
103 | * @param bool $filterOnUserPermissions if true only the objects which the user is allowed to read are returned. |
||
104 | * |
||
105 | * @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult |
||
106 | */ |
||
107 | public function findContent(Query $query, array $languageFilter = array(), $filterOnUserPermissions = true) |
||
135 | |||
136 | /** |
||
137 | * Finds contentInfo objects for the given query. |
||
138 | * |
||
139 | * @see SearchServiceInterface::findContentInfo() |
||
140 | * |
||
141 | * @since 5.4.5 |
||
142 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if query is not valid |
||
143 | * |
||
144 | * @param \eZ\Publish\API\Repository\Values\Content\Query $query |
||
145 | * @param array $languageFilter - a map of filters for the returned fields. |
||
146 | * Currently supports: <code>array("languages" => array(<language1>,..), "useAlwaysAvailable" => bool)</code> |
||
147 | * useAlwaysAvailable defaults to true to avoid exceptions on missing translations. |
||
148 | * @param bool $filterOnUserPermissions if true (default) only the objects which is the user allowed to read are returned. |
||
149 | * |
||
150 | * @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult |
||
151 | */ |
||
152 | public function findContentInfo(Query $query, array $languageFilter = array(), $filterOnUserPermissions = true) |
||
163 | |||
164 | /** |
||
165 | * Finds SPI content info objects for the given query. |
||
166 | * |
||
167 | * Internal for use by {@link findContent} and {@link findContentInfo}. |
||
168 | * |
||
169 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if query is not valid |
||
170 | * |
||
171 | * @param \eZ\Publish\API\Repository\Values\Content\Query $query |
||
172 | * @param array $languageFilter - a map of filters for the returned fields. |
||
173 | * Currently supports: <code>array("languages" => array(<language1>,..), "useAlwaysAvailable" => bool)</code> |
||
174 | * useAlwaysAvailable defaults to true to avoid exceptions on missing translations. |
||
175 | * @param bool $filterOnUserPermissions if true only the objects which is the user allowed to read are returned. |
||
176 | * |
||
177 | * @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult With "raw" SPI contentInfo objects in result |
||
178 | */ |
||
179 | protected function internalFindContentInfo(Query $query, array $languageFilter = array(), $filterOnUserPermissions = true) |
||
210 | |||
211 | /** |
||
212 | * Checks that $criteria does not contain Location criterions. |
||
213 | * |
||
214 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
215 | * |
||
216 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion[] $criteria |
||
217 | * @param string $argumentName |
||
218 | */ |
||
219 | protected function validateContentCriteria(array $criteria, $argumentName) |
||
233 | |||
234 | /** |
||
235 | * Checks that $query does not contain Location sort clauses. |
||
236 | * |
||
237 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
238 | * |
||
239 | * @param \eZ\Publish\API\Repository\Values\Content\Query $query |
||
240 | */ |
||
241 | protected function validateContentSortClauses(Query $query) |
||
249 | |||
250 | /** |
||
251 | * Performs a query for a single content object. |
||
252 | * |
||
253 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the object was not found by the query or due to permissions |
||
254 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if criterion is not valid |
||
255 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if there is more than one result matching the criterions |
||
256 | * |
||
257 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $filter |
||
258 | * @param array $languageFilter Configuration for specifying prioritized languages query will be performed on. |
||
259 | * Currently supports: <code>array("languages" => array(<language1>,..), "useAlwaysAvailable" => bool)</code> |
||
260 | * useAlwaysAvailable defaults to true to avoid exceptions on missing translations. |
||
261 | * @param bool $filterOnUserPermissions if true only the objects which is the user allowed to read are returned. |
||
262 | * |
||
263 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
264 | */ |
||
265 | public function findSingle(Criterion $filter, array $languageFilter = array(), $filterOnUserPermissions = true) |
||
283 | |||
284 | /** |
||
285 | * Suggests a list of values for the given prefix. |
||
286 | * |
||
287 | * @param string $prefix |
||
288 | * @param string[] $fieldPaths |
||
289 | * @param int $limit |
||
290 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $filter |
||
291 | */ |
||
292 | public function suggest($prefix, $fieldPaths = array(), $limit = 10, Criterion $filter = null) |
||
295 | |||
296 | /** |
||
297 | * Finds Locations for the given query. |
||
298 | * |
||
299 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if query is not valid |
||
300 | * |
||
301 | * @param \eZ\Publish\API\Repository\Values\Content\LocationQuery $query |
||
302 | * @param array $languageFilter Configuration for specifying prioritized languages query will be performed on. |
||
303 | * Currently supports: <code>array("languages" => array(<language1>,..), "useAlwaysAvailable" => bool)</code> |
||
304 | * useAlwaysAvailable defaults to true to avoid exceptions on missing translations |
||
305 | * @param bool $filterOnUserPermissions if true only the objects which is the user allowed to read are returned. |
||
306 | * |
||
307 | * @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult |
||
308 | */ |
||
309 | public function findLocations(LocationQuery $query, array $languageFilter = array(), $filterOnUserPermissions = true) |
||
355 | |||
356 | /** |
||
357 | * Adds content, read Permission criteria if needed and return false if no access at all. |
||
358 | * |
||
359 | * @uses \eZ\Publish\API\Repository\PermissionCriterionResolver::getPermissionsCriterion() |
||
360 | * |
||
361 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterion |
||
362 | * |
||
363 | * @return bool|\eZ\Publish\API\Repository\Values\Content\Query\Criterion |
||
364 | */ |
||
365 | View Code Duplication | protected function addPermissionsCriterion(Criterion &$criterion) |
|
386 | } |
||
387 |
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.