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\API\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 | * Content is left untouched. |
||
106 | * |
||
107 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to trash the given location |
||
108 | * |
||
109 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
110 | * |
||
111 | * @return null|\eZ\Publish\API\Repository\Values\Content\TrashItem null if location was deleted, otherwise TrashItem |
||
112 | */ |
||
113 | public function trash(Location $location) |
||
137 | |||
138 | /** |
||
139 | * Recovers the $trashedLocation at its original place if possible. |
||
140 | * |
||
141 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to recover the trash item at the parent location location |
||
142 | * |
||
143 | * If $newParentLocation is provided, $trashedLocation will be restored under it. |
||
144 | * |
||
145 | * @param \eZ\Publish\API\Repository\Values\Content\TrashItem $trashItem |
||
146 | * @param \eZ\Publish\API\Repository\Values\Content\Location $newParentLocation |
||
147 | * |
||
148 | * @return \eZ\Publish\API\Repository\Values\Content\Location the newly created or recovered location |
||
149 | */ |
||
150 | public function recover(APITrashItem $trashItem, Location $newParentLocation = null) |
||
198 | |||
199 | /** |
||
200 | * Empties trash. |
||
201 | * |
||
202 | * All locations contained in the trash will be removed. Content objects will be removed |
||
203 | * if all locations of the content are gone. |
||
204 | * |
||
205 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to empty the trash |
||
206 | */ |
||
207 | View Code Duplication | public function emptyTrash() |
|
223 | |||
224 | /** |
||
225 | * Deletes a trash item. |
||
226 | * |
||
227 | * The corresponding content object will be removed |
||
228 | * |
||
229 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to delete this trash item |
||
230 | * |
||
231 | * @param \eZ\Publish\API\Repository\Values\Content\TrashItem $trashItem |
||
232 | */ |
||
233 | View Code Duplication | public function deleteTrashItem(APITrashItem $trashItem) |
|
252 | |||
253 | /** |
||
254 | * Returns a collection of Trashed locations contained in the trash. |
||
255 | * Only items that the current user has read access to are included. |
||
256 | * |
||
257 | * $query allows to filter/sort the elements to be contained in the collection. |
||
258 | * |
||
259 | * @param \eZ\Publish\API\Repository\Values\Content\Query $query |
||
260 | * |
||
261 | * @return \eZ\Publish\API\Repository\Values\Content\SearchResult |
||
262 | */ |
||
263 | public function findTrashItems(Query $query) |
||
312 | |||
313 | /** |
||
314 | * Builds the domain TrashItem object from provided persistence trash item. |
||
315 | * |
||
316 | * @param \eZ\Publish\SPI\Persistence\Content\Location\Trashed $spiTrashItem |
||
317 | * |
||
318 | * @return \eZ\Publish\API\Repository\Values\Content\TrashItem |
||
319 | */ |
||
320 | View Code Duplication | protected function buildDomainTrashItemObject(Trashed $spiTrashItem) |
|
338 | |||
339 | /** |
||
340 | * @param int $timestamp |
||
341 | * |
||
342 | * @return \DateTime |
||
343 | */ |
||
344 | protected function getDateTime($timestamp) |
||
351 | } |
||
352 |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.