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 |
||
| 27 | class TrashService implements APITrashService, Sessionable |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * @var \eZ\Publish\Core\REST\Client\LocationService |
||
| 31 | */ |
||
| 32 | private $locationService; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var \eZ\Publish\Core\REST\Client\HttpClient |
||
| 36 | */ |
||
| 37 | private $client; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var \eZ\Publish\Core\REST\Common\Input\Dispatcher |
||
| 41 | */ |
||
| 42 | private $inputDispatcher; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var \eZ\Publish\Core\REST\Common\Output\Visitor |
||
| 46 | */ |
||
| 47 | private $outputVisitor; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var \eZ\Publish\Core\REST\Common\RequestParser |
||
| 51 | */ |
||
| 52 | private $requestParser; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @param \eZ\Publish\Core\REST\Client\LocationService $locationService |
||
| 56 | * @param \eZ\Publish\Core\REST\Client\HttpClient $client |
||
| 57 | * @param \eZ\Publish\Core\REST\Common\Input\Dispatcher $inputDispatcher |
||
| 58 | * @param \eZ\Publish\Core\REST\Common\Output\Visitor $outputVisitor |
||
| 59 | * @param \eZ\Publish\Core\REST\Common\RequestParser $requestParser |
||
| 60 | */ |
||
| 61 | View Code Duplication | public function __construct(LocationService $locationService, HttpClient $client, Dispatcher $inputDispatcher, Visitor $outputVisitor, RequestParser $requestParser) |
|
| 69 | |||
| 70 | /** |
||
| 71 | * Set session ID. |
||
| 72 | * |
||
| 73 | * Only for testing |
||
| 74 | * |
||
| 75 | * @param mixed $id |
||
| 76 | */ |
||
| 77 | public function setSession($id) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Loads a trashed location object from its $id. |
||
| 86 | * |
||
| 87 | * Note that $id is identical to original location, which has been previously trashed |
||
| 88 | * |
||
| 89 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to read the trashed location |
||
| 90 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the location with the given id does not exist |
||
| 91 | * |
||
| 92 | * @param mixed $trashItemId |
||
| 93 | * |
||
| 94 | * @return \eZ\Publish\API\Repository\Values\Content\TrashItem |
||
| 95 | */ |
||
| 96 | public function loadTrashItem($trashItemId) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Sends $location and all its children to trash and returns the corresponding trash item. |
||
| 113 | * |
||
| 114 | * The current user may not have access to the returned trash item, check before using it. |
||
| 115 | * Content is left untouched. |
||
| 116 | * |
||
| 117 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to trash the given location |
||
| 118 | * |
||
| 119 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
| 120 | * |
||
| 121 | * @return \eZ\Publish\API\Repository\Values\Content\TrashItem |
||
| 122 | */ |
||
| 123 | public function trash(Location $location) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Recovers the $trashedLocation at its original place if possible. |
||
| 130 | * |
||
| 131 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to recover the trash item at the parent location location |
||
| 132 | * |
||
| 133 | * If $newParentLocation is provided, $trashedLocation will be restored under it. |
||
| 134 | * |
||
| 135 | * @param \eZ\Publish\API\Repository\Values\Content\TrashItem $trashItem |
||
| 136 | * @param \eZ\Publish\API\Repository\Values\Content\Location $newParentLocation |
||
| 137 | * |
||
| 138 | * @return \eZ\Publish\API\Repository\Values\Content\Location the newly created or recovered location |
||
| 139 | */ |
||
| 140 | public function recover(APITrashItem $trashItem, Location $newParentLocation = null) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Empties trash. |
||
| 147 | * |
||
| 148 | * All locations contained in the trash will be removed. Content objects will be removed |
||
| 149 | * if all locations of the content are gone. |
||
| 150 | * |
||
| 151 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to empty the trash |
||
| 152 | */ |
||
| 153 | View Code Duplication | public function emptyTrash() |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Deletes a trash item. |
||
| 174 | * |
||
| 175 | * The corresponding content object will be removed |
||
| 176 | * |
||
| 177 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to delete this trash item |
||
| 178 | * |
||
| 179 | * @param \eZ\Publish\API\Repository\Values\Content\TrashItem $trashItem |
||
| 180 | */ |
||
| 181 | View Code Duplication | public function deleteTrashItem(APITrashItem $trashItem) |
|
| 199 | |||
| 200 | /** |
||
| 201 | * Returns a collection of Trashed locations contained in the trash, which are readable by the current user. |
||
| 202 | * |
||
| 203 | * $query allows to filter/sort the elements to be contained in the collection. |
||
| 204 | * |
||
| 205 | * @param \eZ\Publish\API\Repository\Values\Content\Query $query |
||
| 206 | * |
||
| 207 | * @return \eZ\Publish\API\Repository\Values\Content\Trash\SearchResult |
||
| 208 | */ |
||
| 209 | View Code Duplication | public function findTrashItems(Query $query) |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Converts the Location value object to TrashItem value object. |
||
| 235 | * |
||
| 236 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
| 237 | * |
||
| 238 | * @return \eZ\Publish\API\Repository\Values\Content\TrashItem |
||
| 239 | */ |
||
| 240 | View Code Duplication | protected function buildTrashItem(Location $location) |
|
| 258 | |||
| 259 | /** |
||
| 260 | * Checks if Content is in trash when we can't rely only on locationId (ie. because ContentId is all we have, |
||
| 261 | * and Content in trash is not part of a tree anymore, so does not have mainLocationId. |
||
| 262 | * |
||
| 263 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
| 264 | * |
||
| 265 | * @return bool |
||
| 266 | */ |
||
| 267 | public function isContentInTrash($contentId) |
||
| 271 | } |
||
| 272 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.