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 |
||
| 31 | class SearchService implements SearchServiceInterface |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * @var \eZ\Publish\Core\Repository\Repository |
||
| 35 | */ |
||
| 36 | protected $repository; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var \eZ\Publish\SPI\Search\Handler |
||
| 40 | */ |
||
| 41 | protected $searchHandler; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | protected $settings; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var \eZ\Publish\Core\Repository\Helper\DomainMapper |
||
| 50 | */ |
||
| 51 | protected $domainMapper; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var \eZ\Publish\Core\Repository\PermissionsCriterionHandler |
||
| 55 | */ |
||
| 56 | protected $permissionsCriterionHandler; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var \eZ\Publish\Core\Search\Common\BackgroundIndexer |
||
| 60 | */ |
||
| 61 | protected $backgroundIndexer; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Setups service with reference to repository object that created it & corresponding handler. |
||
| 65 | * |
||
| 66 | * @param \eZ\Publish\API\Repository\Repository $repository |
||
| 67 | * @param \eZ\Publish\SPI\Search\Handler $searchHandler |
||
| 68 | * @param \eZ\Publish\Core\Repository\Helper\DomainMapper $domainMapper |
||
| 69 | * @param \eZ\Publish\Core\Repository\PermissionsCriterionHandler $permissionsCriterionHandler |
||
| 70 | * @param \eZ\Publish\Core\Search\Common\BackgroundIndexer $backgroundIndexer |
||
| 71 | * @param array $settings |
||
| 72 | */ |
||
| 73 | public function __construct( |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Finds content objects for the given query. |
||
| 94 | * |
||
| 95 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if query is not valid |
||
| 96 | * |
||
| 97 | * @param \eZ\Publish\API\Repository\Values\Content\Query $query |
||
| 98 | * @param array $languageFilter Configuration for specifying prioritized languages query will be performed on. |
||
| 99 | * Currently supports: <code>array("languages" => array(<language1>,..), "useAlwaysAvailable" => bool)</code> |
||
| 100 | * useAlwaysAvailable defaults to true to avoid exceptions on missing translations. |
||
| 101 | * @param bool $filterOnUserPermissions if true only the objects which the user is allowed to read are returned. |
||
| 102 | * |
||
| 103 | * @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult |
||
| 104 | */ |
||
| 105 | public function findContent(Query $query, array $languageFilter = array(), $filterOnUserPermissions = true) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Finds contentInfo objects for the given query. |
||
| 136 | * |
||
| 137 | * @see SearchServiceInterface::findContentInfo() |
||
| 138 | * |
||
| 139 | * @since 5.4.5 |
||
| 140 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if query is not valid |
||
| 141 | * |
||
| 142 | * @param \eZ\Publish\API\Repository\Values\Content\Query $query |
||
| 143 | * @param array $languageFilter - a map of filters for the returned fields. |
||
| 144 | * Currently supports: <code>array("languages" => array(<language1>,..), "useAlwaysAvailable" => bool)</code> |
||
| 145 | * useAlwaysAvailable defaults to true to avoid exceptions on missing translations. |
||
| 146 | * @param bool $filterOnUserPermissions if true (default) only the objects which is the user allowed to read are returned. |
||
| 147 | * |
||
| 148 | * @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult |
||
| 149 | */ |
||
| 150 | public function findContentInfo(Query $query, array $languageFilter = array(), $filterOnUserPermissions = true) |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Finds SPI content info objects for the given query. |
||
| 164 | * |
||
| 165 | * Internal for use by {@link findContent} and {@link findContentInfo}. |
||
| 166 | * |
||
| 167 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if query is not valid |
||
| 168 | * |
||
| 169 | * @param \eZ\Publish\API\Repository\Values\Content\Query $query |
||
| 170 | * @param array $languageFilter - a map of filters for the returned fields. |
||
| 171 | * Currently supports: <code>array("languages" => array(<language1>,..), "useAlwaysAvailable" => bool)</code> |
||
| 172 | * useAlwaysAvailable defaults to true to avoid exceptions on missing translations. |
||
| 173 | * @param bool $filterOnUserPermissions if true only the objects which is the user allowed to read are returned. |
||
| 174 | * |
||
| 175 | * @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult With "raw" SPI contentInfo objects in result |
||
| 176 | */ |
||
| 177 | protected function internalFindContentInfo(Query $query, array $languageFilter = array(), $filterOnUserPermissions = true) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Checks that $criteria does not contain Location criterions. |
||
| 211 | * |
||
| 212 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
| 213 | * |
||
| 214 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion[] $criteria |
||
| 215 | * @param string $argumentName |
||
| 216 | */ |
||
| 217 | protected function validateContentCriteria(array $criteria, $argumentName) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Checks that $query does not contain Location sort clauses. |
||
| 234 | * |
||
| 235 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
||
| 236 | * |
||
| 237 | * @param \eZ\Publish\API\Repository\Values\Content\Query $query |
||
| 238 | */ |
||
| 239 | protected function validateContentSortClauses(Query $query) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Performs a query for a single content object. |
||
| 250 | * |
||
| 251 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the object was not found by the query or due to permissions |
||
| 252 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if criterion is not valid |
||
| 253 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if there is more than one result matching the criterions |
||
| 254 | * |
||
| 255 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $filter |
||
| 256 | * @param array $languageFilter Configuration for specifying prioritized languages query will be performed on. |
||
| 257 | * Currently supports: <code>array("languages" => array(<language1>,..), "useAlwaysAvailable" => bool)</code> |
||
| 258 | * useAlwaysAvailable defaults to true to avoid exceptions on missing translations. |
||
| 259 | * @param bool $filterOnUserPermissions if true only the objects which is the user allowed to read are returned. |
||
| 260 | * |
||
| 261 | * @return \eZ\Publish\API\Repository\Values\Content\Content |
||
| 262 | */ |
||
| 263 | public function findSingle(Criterion $filter, array $languageFilter = array(), $filterOnUserPermissions = true) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Suggests a list of values for the given prefix. |
||
| 284 | * |
||
| 285 | * @param string $prefix |
||
| 286 | * @param string[] $fieldPaths |
||
| 287 | * @param int $limit |
||
| 288 | * @param \eZ\Publish\API\Repository\Values\Content\Query\Criterion $filter |
||
| 289 | */ |
||
| 290 | public function suggest($prefix, $fieldPaths = array(), $limit = 10, Criterion $filter = null) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Finds Locations for the given query. |
||
| 296 | * |
||
| 297 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if query is not valid |
||
| 298 | * |
||
| 299 | * @param \eZ\Publish\API\Repository\Values\Content\LocationQuery $query |
||
| 300 | * @param array $languageFilter Configuration for specifying prioritized languages query will be performed on. |
||
| 301 | * Currently supports: <code>array("languages" => array(<language1>,..), "useAlwaysAvailable" => bool)</code> |
||
| 302 | * useAlwaysAvailable defaults to true to avoid exceptions on missing translations |
||
| 303 | * @param bool $filterOnUserPermissions if true only the objects which is the user allowed to read are returned. |
||
| 304 | * |
||
| 305 | * @return \eZ\Publish\API\Repository\Values\Content\Search\SearchResult |
||
| 306 | */ |
||
| 307 | public function findLocations(LocationQuery $query, array $languageFilter = array(), $filterOnUserPermissions = true) |
||
| 353 | } |
||
| 354 |
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.