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 |
||
| 31 | class TrashService implements TrashServiceInterface |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * @var \eZ\Publish\API\Repository\Repository|\eZ\Publish\Core\Repository\Repository |
||
| 35 | */ |
||
| 36 | protected $repository; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var \eZ\Publish\SPI\Persistence\Handler |
||
| 40 | */ |
||
| 41 | protected $persistenceHandler; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | protected $settings; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var \eZ\Publish\Core\Repository\Helper\NameSchemaService |
||
| 50 | */ |
||
| 51 | protected $nameSchemaService; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Setups service with reference to repository object that created it & corresponding handler. |
||
| 55 | * |
||
| 56 | * @param \eZ\Publish\API\Repository\Repository $repository |
||
| 57 | * @param \eZ\Publish\SPI\Persistence\Handler $handler |
||
| 58 | * @param \eZ\Publish\Core\Repository\Helper\NameSchemaService $nameSchemaService |
||
| 59 | * @param array $settings |
||
| 60 | */ |
||
| 61 | public function __construct( |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Loads a trashed location object from its $id. |
||
| 78 | * |
||
| 79 | * Note that $id is identical to original location, which has been previously trashed |
||
| 80 | * |
||
| 81 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to read the trashed location |
||
| 82 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the location with the given id does not exist |
||
| 83 | * |
||
| 84 | * @param mixed $trashItemId |
||
| 85 | * |
||
| 86 | * @return \eZ\Publish\API\Repository\Values\Content\TrashItem |
||
| 87 | */ |
||
| 88 | public function loadTrashItem($trashItemId) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Sends $location and all its children to trash and returns the corresponding trash item. |
||
| 105 | * |
||
| 106 | * Content is left untouched. |
||
| 107 | * |
||
| 108 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to trash the given location |
||
| 109 | * |
||
| 110 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
| 111 | * |
||
| 112 | * @return null|\eZ\Publish\API\Repository\Values\Content\TrashItem null if location was deleted, otherwise TrashItem |
||
| 113 | */ |
||
| 114 | public function trash(Location $location) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Recovers the $trashedLocation at its original place if possible. |
||
| 150 | * |
||
| 151 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to recover the trash item at the parent location location |
||
| 152 | * |
||
| 153 | * If $newParentLocation is provided, $trashedLocation will be restored under it. |
||
| 154 | * |
||
| 155 | * @param \eZ\Publish\API\Repository\Values\Content\TrashItem $trashItem |
||
| 156 | * @param \eZ\Publish\API\Repository\Values\Content\Location $newParentLocation |
||
| 157 | * |
||
| 158 | * @return \eZ\Publish\API\Repository\Values\Content\Location the newly created or recovered location |
||
| 159 | */ |
||
| 160 | public function recover(APITrashItem $trashItem, Location $newParentLocation = null) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Empties trash. |
||
| 211 | * |
||
| 212 | * All locations contained in the trash will be removed. Content objects will be removed |
||
| 213 | * if all locations of the content are gone. |
||
| 214 | * |
||
| 215 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to empty the trash |
||
| 216 | */ |
||
| 217 | View Code Duplication | public function emptyTrash() |
|
| 233 | |||
| 234 | /** |
||
| 235 | * Deletes a trash item. |
||
| 236 | * |
||
| 237 | * The corresponding content object will be removed |
||
| 238 | * |
||
| 239 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to delete this trash item |
||
| 240 | * |
||
| 241 | * @param \eZ\Publish\API\Repository\Values\Content\TrashItem $trashItem |
||
| 242 | */ |
||
| 243 | View Code Duplication | public function deleteTrashItem(APITrashItem $trashItem) |
|
| 262 | |||
| 263 | /** |
||
| 264 | * Returns a collection of Trashed locations contained in the trash. |
||
| 265 | * |
||
| 266 | * $query allows to filter/sort the elements to be contained in the collection. |
||
| 267 | * |
||
| 268 | * @param \eZ\Publish\API\Repository\Values\Content\Query $query |
||
| 269 | * |
||
| 270 | * @return \eZ\Publish\API\Repository\Values\Content\SearchResult |
||
| 271 | */ |
||
| 272 | public function findTrashItems(Query $query) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Builds the domain TrashItem object from provided persistence trash item. |
||
| 320 | * |
||
| 321 | * @param \eZ\Publish\SPI\Persistence\Content\Location\Trashed $spiTrashItem |
||
| 322 | * |
||
| 323 | * @return \eZ\Publish\API\Repository\Values\Content\TrashItem |
||
| 324 | */ |
||
| 325 | View Code Duplication | protected function buildDomainTrashItemObject(Trashed $spiTrashItem) |
|
| 343 | |||
| 344 | /** |
||
| 345 | * @param int $timestamp |
||
| 346 | * |
||
| 347 | * @return \DateTime |
||
| 348 | */ |
||
| 349 | protected function getDateTime($timestamp) |
||
| 356 | } |
||
| 357 |
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.