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 |
||
| 34 | class SearchService implements SearchServiceInterface |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * @var \eZ\Publish\Core\Repository\Repository |
||
| 38 | */ |
||
| 39 | protected $repository; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var \eZ\Publish\SPI\Search\Handler |
||
| 43 | */ |
||
| 44 | protected $searchHandler; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | protected $settings; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var \eZ\Publish\Core\Repository\Helper\DomainMapper |
||
| 53 | */ |
||
| 54 | protected $domainMapper; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var \eZ\Publish\API\Repository\PermissionCriterionResolver |
||
| 58 | */ |
||
| 59 | protected $permissionCriterionResolver; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var \eZ\Publish\Core\Search\Common\BackgroundIndexer |
||
| 63 | */ |
||
| 64 | protected $backgroundIndexer; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Setups service with reference to repository object that created it & corresponding handler. |
||
| 68 | * |
||
| 69 | * @param \eZ\Publish\API\Repository\Repository $repository |
||
| 70 | * @param \eZ\Publish\SPI\Search\Handler $searchHandler |
||
| 71 | * @param \eZ\Publish\Core\Repository\Helper\DomainMapper $domainMapper |
||
| 72 | * @param \eZ\Publish\API\Repository\PermissionCriterionResolver $permissionCriterionResolver |
||
| 73 | * @param \eZ\Publish\Core\Search\Common\BackgroundIndexer $backgroundIndexer |
||
| 74 | * @param array $settings |
||
| 75 | */ |
||
| 76 | public function __construct( |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Finds content objects for the given query. |
||
| 97 | * |
||
| 98 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if query is not valid |
||
| 99 | * |
||
| 100 | * @param \eZ\Publish\API\Repository\Values\Content\Query $query |
||
| 101 | * @param array $languageFilter Configuration for specifying prioritized languages query will be performed on. |
||
| 102 | * Currently supports: <code>array("languages" => array(<language1>,..), "useAlwaysAvailable" => bool)</code> |
||
| 103 | * useAlwaysAvailable defaults to true to avoid exceptions on missing translations. |
||
| 104 | * @param bool $filterOnUserPermissions if true only the objects which the user is allowed to read are returned. |
||
| 105 | * |
||
| 106 | * @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult |
||
| 107 | */ |
||
| 108 | public function findContent(Query $query, array $languageFilter = array(), $filterOnUserPermissions = true) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Finds contentInfo objects for the given query. |
||
| 139 | * |
||
| 140 | * @see SearchServiceInterface::findContentInfo() |
||
| 141 | * |
||
| 142 | * @since 5.4.5 |
||
| 143 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if query is not valid |
||
| 144 | * |
||
| 145 | * @param \eZ\Publish\API\Repository\Values\Content\Query $query |
||
| 146 | * @param array $languageFilter - a map of filters for the returned fields. |
||
| 147 | * Currently supports: <code>array("languages" => array(<language1>,..), "useAlwaysAvailable" => bool)</code> |
||
| 148 | * useAlwaysAvailable defaults to true to avoid exceptions on missing translations. |
||
| 149 | * @param bool $filterOnUserPermissions if true (default) only the objects which is the user allowed to read are returned. |
||
| 150 | * |
||
| 151 | * @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult |
||
| 152 | */ |
||
| 153 | public function findContentInfo(Query $query, array $languageFilter = array(), $filterOnUserPermissions = true) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Finds SPI content info objects for the given query. |
||
| 167 | * |
||
| 168 | * Internal for use by {@link findContent} and {@link findContentInfo}. |
||
| 169 | * |
||
| 170 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if query is not valid |
||
| 171 | * |
||
| 172 | * @param \eZ\Publish\API\Repository\Values\Content\Query $query |
||
| 173 | * @param array $languageFilter - a map of filters for the returned fields. |
||
| 174 | * Currently supports: <code>array("languages" => array(<language1>,..), "useAlwaysAvailable" => bool)</code> |
||
| 175 | * useAlwaysAvailable defaults to true to avoid exceptions on missing translations. |
||
| 176 | * @param bool $filterOnUserPermissions if true only the objects which is the user allowed to read are returned. |
||
| 177 | * |
||
| 178 | * @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult With "raw" SPI contentInfo objects in result |
||
| 179 | */ |
||
| 180 | protected function internalFindContentInfo(Query $query, array $languageFilter = array(), $filterOnUserPermissions = true) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Checks that $criteria does not contain Location criterions. |
||
| 214 | * |
||
| 215 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
| 216 | * |
||
| 217 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion[] $criteria |
||
| 218 | * @param string $argumentName |
||
| 219 | */ |
||
| 220 | protected function validateContentCriteria(array $criteria, $argumentName) |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Checks that $query does not contain Location sort clauses. |
||
| 237 | * |
||
| 238 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
| 239 | * |
||
| 240 | * @param \eZ\Publish\API\Repository\Values\Content\Query $query |
||
| 241 | */ |
||
| 242 | protected function validateContentSortClauses(Query $query) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Performs a query for a single content object. |
||
| 253 | * |
||
| 254 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the object was not found by the query or due to permissions |
||
| 255 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if criterion is not valid |
||
| 256 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if there is more than one result matching the criterions |
||
| 257 | * |
||
| 258 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $filter |
||
| 259 | * @param array $languageFilter Configuration for specifying prioritized languages query will be performed on. |
||
| 260 | * Currently supports: <code>array("languages" => array(<language1>,..), "useAlwaysAvailable" => bool)</code> |
||
| 261 | * useAlwaysAvailable defaults to true to avoid exceptions on missing translations. |
||
| 262 | * @param bool $filterOnUserPermissions if true only the objects which is the user allowed to read are returned. |
||
| 263 | * |
||
| 264 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
| 265 | */ |
||
| 266 | public function findSingle(Criterion $filter, array $languageFilter = array(), $filterOnUserPermissions = true) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Suggests a list of values for the given prefix. |
||
| 287 | * |
||
| 288 | * @param string $prefix |
||
| 289 | * @param string[] $fieldPaths |
||
| 290 | * @param int $limit |
||
| 291 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $filter |
||
| 292 | */ |
||
| 293 | public function suggest($prefix, $fieldPaths = array(), $limit = 10, Criterion $filter = null) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Finds Locations for the given query. |
||
| 299 | * |
||
| 300 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if query is not valid |
||
| 301 | * |
||
| 302 | * @param \eZ\Publish\API\Repository\Values\Content\LocationQuery $query |
||
| 303 | * @param array $languageFilter Configuration for specifying prioritized languages query will be performed on. |
||
| 304 | * Currently supports: <code>array("languages" => array(<language1>,..), "useAlwaysAvailable" => bool)</code> |
||
| 305 | * useAlwaysAvailable defaults to true to avoid exceptions on missing translations |
||
| 306 | * @param bool $filterOnUserPermissions if true only the objects which is the user allowed to read are returned. |
||
| 307 | * |
||
| 308 | * @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult |
||
| 309 | */ |
||
| 310 | public function findLocations(LocationQuery $query, array $languageFilter = array(), $filterOnUserPermissions = true) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Adds content, read Permission criteria if needed and return false if no access at all. |
||
| 359 | * |
||
| 360 | * @uses \eZ\Publish\API\Repository\PermissionCriterionResolver::getPermissionsCriterion() |
||
| 361 | * |
||
| 362 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterion |
||
| 363 | * |
||
| 364 | * @return bool|\eZ\Publish\API\Repository\Values\Content\Query\Criterion |
||
| 365 | */ |
||
| 366 | View Code Duplication | protected function addPermissionsCriterion(Criterion &$criterion) |
|
| 387 | |||
| 388 | public function supports($capabilityFlag) |
||
| 396 | } |
||
| 397 |
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.