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 TrashService 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 TrashService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class TrashService implements TrashServiceInterface |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * @var \eZ\Publish\Core\Repository\Repository |
||
| 36 | */ |
||
| 37 | protected $repository; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var \eZ\Publish\SPI\Persistence\Handler |
||
| 41 | */ |
||
| 42 | protected $persistenceHandler; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var array |
||
| 46 | */ |
||
| 47 | protected $settings; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var \eZ\Publish\Core\Repository\Helper\NameSchemaService |
||
| 51 | */ |
||
| 52 | protected $nameSchemaService; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Setups service with reference to repository object that created it & corresponding handler. |
||
| 56 | * |
||
| 57 | * @param \eZ\Publish\API\Repository\Repository $repository |
||
| 58 | * @param \eZ\Publish\SPI\Persistence\Handler $handler |
||
| 59 | * @param \eZ\Publish\Core\Repository\Helper\NameSchemaService $nameSchemaService |
||
| 60 | * @param array $settings |
||
| 61 | */ |
||
| 62 | public function __construct( |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Loads a trashed location object from its $id. |
||
| 79 | * |
||
| 80 | * Note that $id is identical to original location, which has been previously trashed |
||
| 81 | * |
||
| 82 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to read the trashed location |
||
| 83 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the location with the given id does not exist |
||
| 84 | * |
||
| 85 | * @param mixed $trashItemId |
||
| 86 | * |
||
| 87 | * @return \eZ\Publish\API\Repository\Values\Content\TrashItem |
||
| 88 | */ |
||
| 89 | public function loadTrashItem($trashItemId) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Sends $location and all its children to trash and returns the corresponding trash item. |
||
| 106 | * |
||
| 107 | * The current user may not have access to the returned trash item, check before using it. |
||
| 108 | * Content is left untouched. |
||
| 109 | * |
||
| 110 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to trash the given location |
||
| 111 | * |
||
| 112 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
| 113 | * |
||
| 114 | * @return null|\eZ\Publish\API\Repository\Values\Content\TrashItem null if location was deleted, otherwise TrashItem |
||
| 115 | */ |
||
| 116 | public function trash(Location $location) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Recovers the $trashedLocation at its original place if possible. |
||
| 152 | * |
||
| 153 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to recover the trash item at the parent location location |
||
| 154 | * |
||
| 155 | * If $newParentLocation is provided, $trashedLocation will be restored under it. |
||
| 156 | * |
||
| 157 | * @param \eZ\Publish\API\Repository\Values\Content\TrashItem $trashItem |
||
| 158 | * @param \eZ\Publish\API\Repository\Values\Content\Location $newParentLocation |
||
| 159 | * |
||
| 160 | * @return \eZ\Publish\API\Repository\Values\Content\Location the newly created or recovered location |
||
| 161 | */ |
||
| 162 | public function recover(APITrashItem $trashItem, Location $newParentLocation = null) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Empties trash. |
||
| 213 | * |
||
| 214 | * All locations contained in the trash will be removed. Content objects will be removed |
||
| 215 | * if all locations of the content are gone. |
||
| 216 | * |
||
| 217 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to empty the trash |
||
| 218 | */ |
||
| 219 | View Code Duplication | public function emptyTrash() |
|
| 235 | |||
| 236 | /** |
||
| 237 | * Deletes a trash item. |
||
| 238 | * |
||
| 239 | * The corresponding content object will be removed |
||
| 240 | * |
||
| 241 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to delete this trash item |
||
| 242 | * |
||
| 243 | * @param \eZ\Publish\API\Repository\Values\Content\TrashItem $trashItem |
||
| 244 | */ |
||
| 245 | View Code Duplication | public function deleteTrashItem(APITrashItem $trashItem) |
|
| 264 | |||
| 265 | /** |
||
| 266 | * Returns a collection of Trashed locations contained in the trash, which are readable by the current user. |
||
| 267 | * |
||
| 268 | * $query allows to filter/sort the elements to be contained in the collection. |
||
| 269 | * |
||
| 270 | * @param \eZ\Publish\API\Repository\Values\Content\Query $query |
||
| 271 | * |
||
| 272 | * @return \eZ\Publish\API\Repository\Values\Content\Trash\SearchResult |
||
| 273 | */ |
||
| 274 | public function findTrashItems(Query $query) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Builds the domain TrashItem object from provided persistence trash item. |
||
| 325 | * |
||
| 326 | * @param \eZ\Publish\SPI\Persistence\Content\Location\Trashed $spiTrashItem |
||
| 327 | * |
||
| 328 | * @return \eZ\Publish\API\Repository\Values\Content\TrashItem |
||
| 329 | */ |
||
| 330 | View Code Duplication | protected function buildDomainTrashItemObject(Trashed $spiTrashItem) |
|
| 348 | |||
| 349 | /** |
||
| 350 | * @param int $timestamp |
||
| 351 | * |
||
| 352 | * @return \DateTime |
||
| 353 | */ |
||
| 354 | protected function getDateTime($timestamp) |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Checks if Content is in trash when we can't rely only on locationId (ie. because ContentId is all we have, |
||
| 364 | * and Content in trash is not part of a tree anymore, so does not have mainLocationId. |
||
| 365 | * |
||
| 366 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
| 367 | * |
||
| 368 | * @return bool |
||
| 369 | */ |
||
| 370 | public function isContentInTrash($contentId) |
||
| 380 | } |
||
| 381 |
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.