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 |
||
| 30 | class TrashService implements TrashServiceInterface |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * @var \eZ\Publish\Core\Repository\Repository |
||
| 34 | */ |
||
| 35 | protected $repository; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var \eZ\Publish\SPI\Persistence\Handler |
||
| 39 | */ |
||
| 40 | protected $persistenceHandler; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | protected $settings; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var \eZ\Publish\Core\Repository\Helper\NameSchemaService |
||
| 49 | */ |
||
| 50 | protected $nameSchemaService; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Setups service with reference to repository object that created it & corresponding handler. |
||
| 54 | * |
||
| 55 | * @param \eZ\Publish\API\Repository\Repository $repository |
||
| 56 | * @param \eZ\Publish\SPI\Persistence\Handler $handler |
||
| 57 | * @param \eZ\Publish\Core\Repository\Helper\NameSchemaService $nameSchemaService |
||
| 58 | * @param array $settings |
||
| 59 | */ |
||
| 60 | public function __construct( |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Loads a trashed location object from its $id. |
||
| 77 | * |
||
| 78 | * Note that $id is identical to original location, which has been previously trashed |
||
| 79 | * |
||
| 80 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to read the trashed location |
||
| 81 | * @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException - if the location with the given id does not exist |
||
| 82 | * |
||
| 83 | * @param mixed $trashItemId |
||
| 84 | * |
||
| 85 | * @return \eZ\Publish\API\Repository\Values\Content\TrashItem |
||
| 86 | */ |
||
| 87 | public function loadTrashItem($trashItemId) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Sends $location and all its children to trash and returns the corresponding trash item. |
||
| 104 | * |
||
| 105 | * The current user may not have access to the returned trash item, check before using it. |
||
| 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) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Empties trash. |
||
| 216 | * |
||
| 217 | * All locations contained in the trash will be removed. Content objects will be removed |
||
| 218 | * if all locations of the content are gone. |
||
| 219 | * |
||
| 220 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to empty the trash |
||
| 221 | */ |
||
| 222 | public function emptyTrash() |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Deletes a trash item. |
||
| 244 | * |
||
| 245 | * The corresponding content object will be removed |
||
| 246 | * |
||
| 247 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to delete this trash item |
||
| 248 | * |
||
| 249 | * @param \eZ\Publish\API\Repository\Values\Content\TrashItem $trashItem |
||
| 250 | */ |
||
| 251 | public function deleteTrashItem(APITrashItem $trashItem) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Returns a collection of Trashed locations contained in the trash, which are readable by the current user. |
||
| 273 | * |
||
| 274 | * $query allows to filter/sort the elements to be contained in the collection. |
||
| 275 | * |
||
| 276 | * @param \eZ\Publish\API\Repository\Values\Content\Query $query |
||
| 277 | * |
||
| 278 | * @return \eZ\Publish\API\Repository\Values\Content\SearchResult |
||
| 279 | */ |
||
| 280 | public function findTrashItems(Query $query) |
||
| 281 | { |
||
| 282 | if ($query->filter !== null && !$query->filter instanceof Criterion) { |
||
| 283 | throw new InvalidArgumentValue('query->filter', $query->filter, 'Query'); |
||
| 284 | } |
||
| 285 | |||
| 286 | if ($query->sortClauses !== null) { |
||
| 287 | if (!is_array($query->sortClauses)) { |
||
| 288 | throw new InvalidArgumentValue('query->sortClauses', $query->sortClauses, 'Query'); |
||
| 289 | } |
||
| 290 | |||
| 291 | foreach ($query->sortClauses as $sortClause) { |
||
| 292 | if (!$sortClause instanceof SortClause) { |
||
| 293 | throw new InvalidArgumentValue('query->sortClauses', 'only instances of SortClause class are allowed'); |
||
| 294 | } |
||
| 295 | } |
||
| 296 | } |
||
| 297 | |||
| 298 | View Code Duplication | if ($query->offset !== null && !is_numeric($query->offset)) { |
|
| 299 | throw new InvalidArgumentValue('query->offset', $query->offset, 'Query'); |
||
| 300 | } |
||
| 301 | |||
| 302 | View Code Duplication | if ($query->limit !== null && !is_numeric($query->limit)) { |
|
| 303 | throw new InvalidArgumentValue('query->limit', $query->limit, 'Query'); |
||
| 304 | } |
||
| 305 | |||
| 306 | $spiTrashItems = $this->persistenceHandler->trashHandler()->findTrashItems( |
||
| 307 | $query->filter !== null ? $query->filter : null, |
||
| 308 | $query->offset !== null && $query->offset > 0 ? (int)$query->offset : 0, |
||
| 309 | $query->limit !== null && $query->limit >= 1 ? (int)$query->limit : null, |
||
| 310 | $query->sortClauses !== null ? $query->sortClauses : null |
||
| 311 | ); |
||
| 312 | |||
| 313 | $trashItems = array(); |
||
| 314 | foreach ($spiTrashItems as $spiTrashItem) { |
||
| 315 | try { |
||
| 316 | $trashItems[] = $this->buildDomainTrashItemObject($spiTrashItem); |
||
| 317 | } catch (UnauthorizedException $e) { |
||
| 318 | // Do nothing, thus exclude items the current user doesn't have read access to. |
||
| 319 | } |
||
| 320 | } |
||
| 321 | |||
| 322 | $searchResult = new SearchResult(); |
||
| 323 | $searchResult->count = count($trashItems); |
||
| 324 | $searchResult->items = $trashItems; |
||
| 325 | $searchResult->query = $query; |
||
| 326 | |||
| 327 | return $searchResult; |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Builds the domain TrashItem object from provided persistence trash item. |
||
| 332 | * |
||
| 333 | * @param \eZ\Publish\SPI\Persistence\Content\Location\Trashed $spiTrashItem |
||
| 334 | * |
||
| 335 | * @return \eZ\Publish\API\Repository\Values\Content\TrashItem |
||
| 336 | */ |
||
| 337 | View Code Duplication | protected function buildDomainTrashItemObject(Trashed $spiTrashItem) |
|
| 355 | |||
| 356 | /** |
||
| 357 | * @param int $timestamp |
||
| 358 | * |
||
| 359 | * @return \DateTime |
||
| 360 | */ |
||
| 361 | protected function getDateTime($timestamp) |
||
| 368 | } |
||
| 369 |
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.