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 |
||
| 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 | View Code Duplication | 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) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Finds contentInfo objects for the given query. |
||
| 121 | * |
||
| 122 | * @see SearchServiceInterface::findContentInfo() |
||
| 123 | * |
||
| 124 | * @since 5.4.5 |
||
| 125 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if query is not valid |
||
| 126 | * |
||
| 127 | * @param \eZ\Publish\API\Repository\Values\Content\Query $query |
||
| 128 | * @param array $languageFilter - a map of filters for the returned fields. |
||
| 129 | * Currently supports: <code>array("languages" => array(<language1>,..), "useAlwaysAvailable" => bool)</code> |
||
| 130 | * useAlwaysAvailable defaults to true to avoid exceptions on missing translations. |
||
| 131 | * @param bool $filterOnUserPermissions if true (default) only the objects which is the user allowed to read are returned. |
||
| 132 | * |
||
| 133 | * @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult |
||
| 134 | */ |
||
| 135 | public function findContentInfo(Query $query, array $languageFilter = array(), $filterOnUserPermissions = true) |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Finds SPI content info objects for the given query. |
||
| 149 | * |
||
| 150 | * Internal for use by {@link findContent} and {@link findContentInfo}. |
||
| 151 | * |
||
| 152 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if query is not valid |
||
| 153 | * |
||
| 154 | * @param \eZ\Publish\API\Repository\Values\Content\Query $query |
||
| 155 | * @param array $languageFilter - a map of filters for the returned fields. |
||
| 156 | * Currently supports: <code>array("languages" => array(<language1>,..), "useAlwaysAvailable" => bool)</code> |
||
| 157 | * useAlwaysAvailable defaults to true to avoid exceptions on missing translations. |
||
| 158 | * @param bool $filterOnUserPermissions if true only the objects which is the user allowed to read are returned. |
||
| 159 | * |
||
| 160 | * @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult With "raw" SPI contentInfo objects in result |
||
| 161 | */ |
||
| 162 | protected function internalFindContentInfo(Query $query, array $languageFilter = array(), $filterOnUserPermissions = true) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Checks that $criteria does not contain Location criterions. |
||
| 196 | * |
||
| 197 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
| 198 | * |
||
| 199 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion[] $criteria |
||
| 200 | * @param string $argumentName |
||
| 201 | */ |
||
| 202 | protected function validateContentCriteria(array $criteria, $argumentName) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Checks that $query does not contain Location sort clauses. |
||
| 219 | * |
||
| 220 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
| 221 | * |
||
| 222 | * @param \eZ\Publish\API\Repository\Values\Content\Query $query |
||
| 223 | */ |
||
| 224 | protected function validateContentSortClauses(Query $query) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Performs a query for a single content object. |
||
| 235 | * |
||
| 236 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the object was not found by the query or due to permissions |
||
| 237 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if criterion is not valid |
||
| 238 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if there is more than one result matching the criterions |
||
| 239 | * |
||
| 240 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $filter |
||
| 241 | * @param array $languageFilter Configuration for specifying prioritized languages query will be performed on. |
||
| 242 | * Currently supports: <code>array("languages" => array(<language1>,..), "useAlwaysAvailable" => bool)</code> |
||
| 243 | * useAlwaysAvailable defaults to true to avoid exceptions on missing translations. |
||
| 244 | * @param bool $filterOnUserPermissions if true only the objects which is the user allowed to read are returned. |
||
| 245 | * |
||
| 246 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
| 247 | */ |
||
| 248 | public function findSingle(Criterion $filter, array $languageFilter = array(), $filterOnUserPermissions = true) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Suggests a list of values for the given prefix. |
||
| 269 | * |
||
| 270 | * @param string $prefix |
||
| 271 | * @param string[] $fieldPaths |
||
| 272 | * @param int $limit |
||
| 273 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $filter |
||
| 274 | */ |
||
| 275 | public function suggest($prefix, $fieldPaths = array(), $limit = 10, Criterion $filter = null) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Finds Locations for the given query. |
||
| 281 | * |
||
| 282 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if query is not valid |
||
| 283 | * |
||
| 284 | * @param \eZ\Publish\API\Repository\Values\Content\LocationQuery $query |
||
| 285 | * @param array $languageFilter Configuration for specifying prioritized languages query will be performed on. |
||
| 286 | * Currently supports: <code>array("languages" => array(<language1>,..), "useAlwaysAvailable" => bool)</code> |
||
| 287 | * useAlwaysAvailable defaults to true to avoid exceptions on missing translations |
||
| 288 | * @param bool $filterOnUserPermissions if true only the objects which is the user allowed to read are returned. |
||
| 289 | * |
||
| 290 | * @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult |
||
| 291 | */ |
||
| 292 | public function findLocations(LocationQuery $query, array $languageFilter = array(), $filterOnUserPermissions = true) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Adds content, read Permission criteria if needed and return false if no access at all. |
||
| 329 | * |
||
| 330 | * @uses \eZ\Publish\API\Repository\PermissionCriterionResolver::getPermissionsCriterion() |
||
| 331 | * |
||
| 332 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $criterion |
||
| 333 | * |
||
| 334 | * @return bool|\eZ\Publish\API\Repository\Values\Content\Query\Criterion |
||
| 335 | */ |
||
| 336 | View Code Duplication | protected function addPermissionsCriterion(Criterion &$criterion) |
|
| 357 | |||
| 358 | public function supports($capabilityFlag) |
||
| 366 | } |
||
| 367 |
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.