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) |
||
106 | |||
107 | /** |
||
108 | * Sends $location and all its children to trash and returns the corresponding trash item. |
||
109 | * |
||
110 | * The current user may not have access to the returned trash item, check before using it. |
||
111 | * Content is left untouched. |
||
112 | * |
||
113 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to trash the given location |
||
114 | * |
||
115 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
116 | * |
||
117 | * @return null|\eZ\Publish\API\Repository\Values\Content\TrashItem null if location was deleted, otherwise TrashItem |
||
118 | */ |
||
119 | public function trash(Location $location) |
||
151 | |||
152 | /** |
||
153 | * Recovers the $trashedLocation at its original place if possible. |
||
154 | * |
||
155 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to recover the trash item at the parent location location |
||
156 | * |
||
157 | * If $newParentLocation is provided, $trashedLocation will be restored under it. |
||
158 | * |
||
159 | * @param \eZ\Publish\API\Repository\Values\Content\TrashItem $trashItem |
||
160 | * @param \eZ\Publish\API\Repository\Values\Content\Location $newParentLocation |
||
161 | * |
||
162 | * @return \eZ\Publish\API\Repository\Values\Content\Location the newly created or recovered location |
||
163 | */ |
||
164 | public function recover(APITrashItem $trashItem, Location $newParentLocation = null) |
||
217 | |||
218 | /** |
||
219 | * Empties trash. |
||
220 | * |
||
221 | * All locations contained in the trash will be removed. Content objects will be removed |
||
222 | * if all locations of the content are gone. |
||
223 | * |
||
224 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to empty the trash |
||
225 | */ |
||
226 | public function emptyTrash() |
||
245 | |||
246 | /** |
||
247 | * Deletes a trash item. |
||
248 | * |
||
249 | * The corresponding content object will be removed |
||
250 | * |
||
251 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to delete this trash item |
||
252 | * |
||
253 | * @param \eZ\Publish\API\Repository\Values\Content\TrashItem $trashItem |
||
254 | */ |
||
255 | public function deleteTrashItem(APITrashItem $trashItem) |
||
274 | |||
275 | /** |
||
276 | * Returns a collection of Trashed locations contained in the trash, which are readable by the current user. |
||
277 | * |
||
278 | * $query allows to filter/sort the elements to be contained in the collection. |
||
279 | * |
||
280 | * @param \eZ\Publish\API\Repository\Values\Content\Query $query |
||
281 | * |
||
282 | * @return \eZ\Publish\API\Repository\Values\Content\Trash\SearchResult |
||
283 | */ |
||
284 | public function findTrashItems(Query $query) |
||
324 | |||
325 | protected function buildDomainTrashItems(array $spiTrashItems): array |
||
342 | |||
343 | View Code Duplication | protected function buildDomainTrashItemObject(Trashed $spiTrashItem, Content $content): APITrashItem |
|
362 | |||
363 | /** |
||
364 | * @param int $timestamp |
||
365 | * |
||
366 | * @return \DateTime |
||
367 | */ |
||
368 | protected function getDateTime($timestamp) |
||
375 | } |
||
376 |
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.