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\Core\Repository\PermissionsCriterionHandler |
||
| 54 | */ |
||
| 55 | protected $permissionsCriterionHandler; |
||
| 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\Core\Repository\PermissionsCriterionHandler $permissionsCriterionHandler |
||
| 64 | * @param array $settings |
||
| 65 | */ |
||
| 66 | public function __construct( |
||
| 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) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Finds contentInfo objects for the given query. |
||
| 124 | * |
||
| 125 | * @see SearchServiceInterface::findContentInfo() |
||
| 126 | * |
||
| 127 | * @since 5.4.5 |
||
| 128 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if query is not valid |
||
| 129 | * |
||
| 130 | * @param \eZ\Publish\API\Repository\Values\Content\Query $query |
||
| 131 | * @param array $languageFilter - a map of filters for the returned fields. |
||
| 132 | * Currently supports: <code>array("languages" => array(<language1>,..), "useAlwaysAvailable" => bool)</code> |
||
| 133 | * useAlwaysAvailable defaults to true to avoid exceptions on missing translations. |
||
| 134 | * @param bool $filterOnUserPermissions if true (default) only the objects which is the user allowed to read are returned. |
||
| 135 | * |
||
| 136 | * @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult |
||
| 137 | */ |
||
| 138 | public function findContentInfo(Query $query, array $languageFilter = array(), $filterOnUserPermissions = true) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Finds SPI content info objects for the given query. |
||
| 152 | * |
||
| 153 | * Internal for use by {@link findContent} and {@link findContentInfo}. |
||
| 154 | * |
||
| 155 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if query is not valid |
||
| 156 | * |
||
| 157 | * @param \eZ\Publish\API\Repository\Values\Content\Query $query |
||
| 158 | * @param array $languageFilter - a map of filters for the returned fields. |
||
| 159 | * Currently supports: <code>array("languages" => array(<language1>,..), "useAlwaysAvailable" => bool)</code> |
||
| 160 | * useAlwaysAvailable defaults to true to avoid exceptions on missing translations. |
||
| 161 | * @param bool $filterOnUserPermissions if true only the objects which is the user allowed to read are returned. |
||
| 162 | * |
||
| 163 | * @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult With "raw" SPI contentInfo objects in result |
||
| 164 | */ |
||
| 165 | protected function internalFindContentInfo(Query $query, array $languageFilter = array(), $filterOnUserPermissions = true) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Checks that $criteria does not contain Location criterions. |
||
| 199 | * |
||
| 200 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
| 201 | * |
||
| 202 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion[] $criteria |
||
| 203 | * @param string $argumentName |
||
| 204 | */ |
||
| 205 | protected function validateContentCriteria(array $criteria, $argumentName) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Checks that $query does not contain Location sort clauses. |
||
| 222 | * |
||
| 223 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
| 224 | * |
||
| 225 | * @param \eZ\Publish\API\Repository\Values\Content\Query $query |
||
| 226 | */ |
||
| 227 | protected function validateContentSortClauses(Query $query) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Performs a query for a single content object. |
||
| 238 | * |
||
| 239 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the object was not found by the query or due to permissions |
||
| 240 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if criterion is not valid |
||
| 241 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if there is more than one result matching the criterions |
||
| 242 | * |
||
| 243 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $filter |
||
| 244 | * @param array $languageFilter Configuration for specifying prioritized languages query will be performed on. |
||
| 245 | * Currently supports: <code>array("languages" => array(<language1>,..), "useAlwaysAvailable" => bool)</code> |
||
| 246 | * useAlwaysAvailable defaults to true to avoid exceptions on missing translations. |
||
| 247 | * @param bool $filterOnUserPermissions if true only the objects which is the user allowed to read are returned. |
||
| 248 | * |
||
| 249 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
| 250 | */ |
||
| 251 | public function findSingle(Criterion $filter, array $languageFilter = array(), $filterOnUserPermissions = true) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Suggests a list of values for the given prefix. |
||
| 272 | * |
||
| 273 | * @param string $prefix |
||
| 274 | * @param string[] $fieldPaths |
||
| 275 | * @param int $limit |
||
| 276 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $filter |
||
| 277 | */ |
||
| 278 | public function suggest($prefix, $fieldPaths = array(), $limit = 10, Criterion $filter = null) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Finds Locations for the given query. |
||
| 284 | * |
||
| 285 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if query is not valid |
||
| 286 | * |
||
| 287 | * @param \eZ\Publish\API\Repository\Values\Content\LocationQuery $query |
||
| 288 | * @param array $languageFilter Configuration for specifying prioritized languages query will be performed on. |
||
| 289 | * Currently supports: <code>array("languages" => array(<language1>,..), "useAlwaysAvailable" => bool)</code> |
||
| 290 | * useAlwaysAvailable defaults to true to avoid exceptions on missing translations |
||
| 291 | * @param bool $filterOnUserPermissions if true only the objects which is the user allowed to read are returned. |
||
| 292 | * |
||
| 293 | * @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult |
||
| 294 | */ |
||
| 295 | public function findLocations(LocationQuery $query, array $languageFilter = array(), $filterOnUserPermissions = true) |
||
| 338 | } |
||
| 339 |
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.