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:
1 | <?php |
||
30 | class LocationService implements LocationServiceInterface |
||
31 | { |
||
32 | /** |
||
33 | * Aggregated service. |
||
34 | * |
||
35 | * @var \eZ\Publish\API\Repository\LocationService |
||
36 | */ |
||
37 | protected $service; |
||
38 | |||
39 | /** |
||
40 | * SignalDispatcher. |
||
41 | * |
||
42 | * @var \eZ\Publish\Core\SignalSlot\SignalDispatcher |
||
43 | */ |
||
44 | protected $signalDispatcher; |
||
45 | |||
46 | /** |
||
47 | * Constructor. |
||
48 | * |
||
49 | * Construct service object from aggregated service and signal |
||
50 | * dispatcher |
||
51 | * |
||
52 | * @param \eZ\Publish\API\Repository\LocationService $service |
||
53 | * @param \eZ\Publish\Core\SignalSlot\SignalDispatcher $signalDispatcher |
||
54 | */ |
||
55 | public function __construct(LocationServiceInterface $service, SignalDispatcher $signalDispatcher) |
||
60 | |||
61 | /** |
||
62 | * Copies the subtree starting from $subtree as a new subtree of $targetLocation. |
||
63 | * |
||
64 | * Only the items on which the user has read access are copied. |
||
65 | * |
||
66 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed copy the subtree to the given parent location |
||
67 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user does not have read access to the whole source subtree |
||
68 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the target location is a sub location of the given location |
||
69 | * |
||
70 | * @param \eZ\Publish\API\Repository\Values\Content\Location $subtree - the subtree denoted by the location to copy |
||
71 | * @param \eZ\Publish\API\Repository\Values\Content\Location $targetParentLocation - the target parent location for the copy operation |
||
72 | * |
||
73 | * @return \eZ\Publish\API\Repository\Values\Content\Location The newly created location of the copied subtree |
||
74 | */ |
||
75 | public function copySubtree(Location $subtree, Location $targetParentLocation) |
||
90 | |||
91 | /** |
||
92 | * {@inheritdoc} |
||
93 | */ |
||
94 | public function loadLocation($locationId, array $prioritizedLanguages = null, bool $useAlwaysAvailable = null) |
||
98 | |||
99 | /** |
||
100 | * {@inheritdoc} |
||
101 | */ |
||
102 | public function loadLocationList(array $locationIds, array $prioritizedLanguages = null, bool $useAlwaysAvailable = null): iterable |
||
106 | |||
107 | /** |
||
108 | * {@inheritdoc} |
||
109 | */ |
||
110 | public function loadLocationByRemoteId($remoteId, array $prioritizedLanguages = null, bool $useAlwaysAvailable = null) |
||
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | public function loadLocations(ContentInfo $contentInfo, Location $rootLocation = null, array $prioritizedLanguages = null) |
||
122 | |||
123 | /** |
||
124 | * {@inheritdoc} |
||
125 | */ |
||
126 | public function loadLocationChildren(Location $location, $offset = 0, $limit = 25, array $prioritizedLanguages = null) |
||
130 | |||
131 | /** |
||
132 | * {@inheritdoc} |
||
133 | */ |
||
134 | public function loadParentLocationsForDraftContent(VersionInfo $versionInfo, array $prioritizedLanguages = null) |
||
138 | |||
139 | /** |
||
140 | * Returns the number of children which are readable by the current user of a location object. |
||
141 | * |
||
142 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
143 | * |
||
144 | * @return int |
||
145 | */ |
||
146 | public function getLocationChildCount(Location $location) |
||
150 | |||
151 | /** |
||
152 | * Creates the new $location in the content repository for the given content. |
||
153 | * |
||
154 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to create this location |
||
155 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the content is already below the specified parent |
||
156 | * or the parent is a sub location of the location of the content |
||
157 | * or if set the remoteId exists already |
||
158 | * |
||
159 | * @param \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo |
||
160 | * @param \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct $locationCreateStruct |
||
161 | * |
||
162 | * @return \eZ\Publish\API\Repository\Values\Content\Location the newly created Location |
||
163 | */ |
||
164 | public function createLocation(ContentInfo $contentInfo, LocationCreateStruct $locationCreateStruct) |
||
179 | |||
180 | /** |
||
181 | * Updates $location in the content repository. |
||
182 | * |
||
183 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to update this location |
||
184 | * @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if if set the remoteId exists already |
||
185 | * |
||
186 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
187 | * @param \eZ\Publish\API\Repository\Values\Content\LocationUpdateStruct $locationUpdateStruct |
||
188 | * |
||
189 | * @return \eZ\Publish\API\Repository\Values\Content\Location the updated Location |
||
190 | */ |
||
191 | public function updateLocation(Location $location, LocationUpdateStruct $locationUpdateStruct) |
||
192 | { |
||
193 | $returnValue = $this->service->updateLocation($location, $locationUpdateStruct); |
||
194 | $this->signalDispatcher->emit( |
||
195 | new UpdateLocationSignal( |
||
196 | array( |
||
197 | 'contentId' => $location->contentId, |
||
198 | 'locationId' => $location->id, |
||
199 | 'parentLocationId' => $location->parentLocationId, |
||
200 | ) |
||
201 | ) |
||
202 | ); |
||
203 | |||
204 | return $returnValue; |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * Swaps the contents held by $location1 and $location2. |
||
209 | * |
||
210 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to swap content |
||
211 | * |
||
212 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location1 |
||
213 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location2 |
||
214 | */ |
||
215 | public function swapLocation(Location $location1, Location $location2) |
||
233 | |||
234 | /** |
||
235 | * Hides the $location and marks invisible all descendants of $location. |
||
236 | * |
||
237 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to hide this location |
||
238 | * |
||
239 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
240 | * |
||
241 | * @return \eZ\Publish\API\Repository\Values\Content\Location $location, with updated hidden value |
||
242 | */ |
||
243 | View Code Duplication | public function hideLocation(Location $location) |
|
259 | |||
260 | /** |
||
261 | * Unhides the $location. |
||
262 | * |
||
263 | * This method and marks visible all descendants of $locations |
||
264 | * until a hidden location is found. |
||
265 | * |
||
266 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to unhide this location |
||
267 | * |
||
268 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
269 | * |
||
270 | * @return \eZ\Publish\API\Repository\Values\Content\Location $location, with updated hidden value |
||
271 | */ |
||
272 | View Code Duplication | public function unhideLocation(Location $location) |
|
288 | |||
289 | /** |
||
290 | * Moves the subtree to $newParentLocation. |
||
291 | * |
||
292 | * If a user has the permission to move the location to a target location |
||
293 | * he can do it regardless of an existing descendant on which the user has no permission. |
||
294 | * |
||
295 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user user is not allowed to move this location to the target |
||
296 | * |
||
297 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
298 | * @param \eZ\Publish\API\Repository\Values\Content\Location $newParentLocation |
||
299 | */ |
||
300 | public function moveSubtree(Location $location, Location $newParentLocation) |
||
315 | |||
316 | /** |
||
317 | * Deletes $location and all its descendants. |
||
318 | * |
||
319 | * @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If the current user is not allowed to delete this location or a descendant |
||
320 | * |
||
321 | * @param \eZ\Publish\API\Repository\Values\Content\Location $location |
||
322 | */ |
||
323 | View Code Duplication | public function deleteLocation(Location $location) |
|
336 | |||
337 | /** |
||
338 | * Instantiates a new location create class. |
||
339 | * |
||
340 | * @param mixed $parentLocationId the parent under which the new location should be created |
||
341 | * @param eZ\Publish\API\Repository\Values\ContentType\ContentType|null $contentType |
||
342 | * |
||
343 | * @return \eZ\Publish\API\Repository\Values\Content\LocationCreateStruct |
||
344 | */ |
||
345 | public function newLocationCreateStruct($parentLocationId, ContentType $contentType = null) |
||
349 | |||
350 | /** |
||
351 | * Instantiates a new location update class. |
||
352 | * |
||
353 | * @return \eZ\Publish\API\Repository\Values\Content\LocationUpdateStruct |
||
354 | */ |
||
355 | public function newLocationUpdateStruct() |
||
359 | |||
360 | /** |
||
361 | * Get the total number of all existing Locations. Can be combined with loadAllLocations. |
||
362 | * |
||
363 | * @see loadAllLocations |
||
364 | * |
||
365 | * @return int Total number of Locations |
||
366 | */ |
||
367 | public function getAllLocationsCount(): int |
||
371 | |||
372 | /** |
||
373 | * Bulk-load all existing Locations, constrained by $limit and $offset to paginate results. |
||
374 | * |
||
375 | * @param int $limit |
||
376 | * @param int $offset |
||
377 | * |
||
378 | * @return \eZ\Publish\API\Repository\Values\Content\Location[] |
||
379 | */ |
||
380 | public function loadAllLocations(int $offset = 0, int $limit = 25): array |
||
384 | } |
||
385 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.