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 |
||
19 | class LocationHandler extends AbstractHandler implements LocationHandlerInterface |
||
20 | { |
||
21 | /** |
||
22 | * {@inheritdoc} |
||
23 | */ |
||
24 | View Code Duplication | public function load($locationId, array $translations = null, bool $useAlwaysAvailable = true) |
|
25 | { |
||
26 | $translationsKey = $this->getCacheTranslationKey($translations, $useAlwaysAvailable); |
||
27 | $cacheItem = $this->cache->getItem("ez-location-${locationId}-${translationsKey}"); |
||
28 | if ($cacheItem->isHit()) { |
||
29 | return $cacheItem->get(); |
||
30 | } |
||
31 | |||
32 | $this->logger->logCall( |
||
33 | __METHOD__, |
||
34 | ['location' => $locationId, 'translations' => $translations, 'always-available' => $useAlwaysAvailable] |
||
35 | ); |
||
36 | $location = $this->persistenceHandler->locationHandler()->load($locationId, $translations, $useAlwaysAvailable); |
||
|
|||
37 | |||
38 | $cacheItem->set($location); |
||
39 | $cacheItem->tag($this->getCacheTags($location)); |
||
40 | $this->cache->save($cacheItem); |
||
41 | |||
42 | return $location; |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * {@inheritdoc} |
||
47 | */ |
||
48 | public function loadSubtreeIds($locationId) |
||
49 | { |
||
50 | $cacheItem = $this->cache->getItem("ez-location-subtree-${locationId}"); |
||
51 | if ($cacheItem->isHit()) { |
||
52 | return $cacheItem->get(); |
||
53 | } |
||
54 | |||
55 | $this->logger->logCall(__METHOD__, array('location' => $locationId)); |
||
56 | $locationIds = $this->persistenceHandler->locationHandler()->loadSubtreeIds($locationId); |
||
57 | |||
58 | $cacheItem->set($locationIds); |
||
59 | $cacheTags = ['location-' . $locationId, 'location-path-' . $locationId]; |
||
60 | foreach ($locationIds as $id) { |
||
61 | $cacheTags[] = 'location-' . $id; |
||
62 | $cacheTags[] = 'location-path-' . $id; |
||
63 | } |
||
64 | $cacheItem->tag($cacheTags); |
||
65 | $this->cache->save($cacheItem); |
||
66 | |||
67 | return $locationIds; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * {@inheritdoc} |
||
72 | */ |
||
73 | public function loadLocationsByContent($contentId, $rootLocationId = null) |
||
74 | { |
||
75 | if ($rootLocationId) { |
||
76 | $cacheItem = $this->cache->getItem("ez-content-locations-${contentId}-root-${rootLocationId}"); |
||
77 | $cacheTags = ['content-' . $contentId, 'location-' . $rootLocationId, 'location-path-' . $rootLocationId]; |
||
78 | } else { |
||
79 | $cacheItem = $this->cache->getItem("ez-content-locations-${contentId}"); |
||
80 | $cacheTags = ['content-' . $contentId]; |
||
81 | } |
||
82 | |||
83 | if ($cacheItem->isHit()) { |
||
84 | return $cacheItem->get(); |
||
85 | } |
||
86 | |||
87 | $this->logger->logCall(__METHOD__, array('content' => $contentId, 'root' => $rootLocationId)); |
||
88 | $locations = $this->persistenceHandler->locationHandler()->loadLocationsByContent($contentId, $rootLocationId); |
||
89 | |||
90 | $cacheItem->set($locations); |
||
91 | foreach ($locations as $location) { |
||
92 | $cacheTags = $this->getCacheTags($location, $cacheTags); |
||
93 | } |
||
94 | $cacheItem->tag($cacheTags); |
||
95 | $this->cache->save($cacheItem); |
||
96 | |||
97 | return $locations; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * {@inheritdoc} |
||
102 | */ |
||
103 | View Code Duplication | public function loadParentLocationsForDraftContent($contentId) |
|
104 | { |
||
105 | $cacheItem = $this->cache->getItem("ez-content-locations-${contentId}-parentForDraft"); |
||
106 | if ($cacheItem->isHit()) { |
||
107 | return $cacheItem->get(); |
||
108 | } |
||
109 | |||
110 | $this->logger->logCall(__METHOD__, array('content' => $contentId)); |
||
111 | $locations = $this->persistenceHandler->locationHandler()->loadParentLocationsForDraftContent($contentId); |
||
112 | |||
113 | $cacheItem->set($locations); |
||
114 | $cacheTags = ['content-' . $contentId]; |
||
115 | foreach ($locations as $location) { |
||
116 | $cacheTags = $this->getCacheTags($location, $cacheTags); |
||
117 | } |
||
118 | $cacheItem->tag($cacheTags); |
||
119 | $this->cache->save($cacheItem); |
||
120 | |||
121 | return $locations; |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * {@inheritdoc} |
||
126 | */ |
||
127 | View Code Duplication | public function loadByRemoteId($remoteId, array $translations = null, bool $useAlwaysAvailable = true) |
|
128 | { |
||
129 | $translationsKey = $this->getCacheTranslationKey($translations, $useAlwaysAvailable); |
||
130 | $cacheItem = $this->cache->getItem("ez-location-remoteid-${remoteId}-${translationsKey}"); |
||
131 | if ($cacheItem->isHit()) { |
||
132 | return $cacheItem->get(); |
||
133 | } |
||
134 | |||
135 | $this->logger->logCall( |
||
136 | __METHOD__, |
||
137 | ['location' => $remoteId, 'translations' => $translations, 'always-available' => $useAlwaysAvailable] |
||
138 | ); |
||
139 | $location = $this->persistenceHandler->locationHandler()->loadByRemoteId($remoteId, $translations, $useAlwaysAvailable); |
||
140 | |||
141 | $cacheItem->set($location); |
||
142 | $cacheItem->tag($this->getCacheTags($location)); |
||
143 | $this->cache->save($cacheItem); |
||
144 | |||
145 | return $location; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * {@inheritdoc} |
||
150 | */ |
||
151 | View Code Duplication | public function copySubtree($sourceId, $destinationParentId, $newOwnerId = null) |
|
161 | |||
162 | /** |
||
163 | * {@inheritdoc} |
||
164 | */ |
||
165 | public function move($sourceId, $destinationParentId) |
||
174 | |||
175 | /** |
||
176 | * {@inheritdoc} |
||
177 | */ |
||
178 | public function markSubtreeModified($locationId, $timestamp = null) |
||
183 | |||
184 | /** |
||
185 | * {@inheritdoc} |
||
186 | */ |
||
187 | public function hide($locationId) |
||
196 | |||
197 | /** |
||
198 | * {@inheritdoc} |
||
199 | */ |
||
200 | public function unHide($locationId) |
||
209 | |||
210 | /** |
||
211 | * {@inheritdoc} |
||
212 | */ |
||
213 | public function swap($locationId1, $locationId2) |
||
229 | |||
230 | /** |
||
231 | * {@inheritdoc} |
||
232 | */ |
||
233 | public function update(UpdateStruct $struct, $locationId) |
||
240 | |||
241 | /** |
||
242 | * {@inheritdoc} |
||
243 | */ |
||
244 | public function create(CreateStruct $locationStruct) |
||
255 | |||
256 | /** |
||
257 | * {@inheritdoc} |
||
258 | */ |
||
259 | public function removeSubtree($locationId) |
||
268 | |||
269 | /** |
||
270 | * {@inheritdoc} |
||
271 | */ |
||
272 | public function setSectionForSubtree($locationId, $sectionId) |
||
279 | |||
280 | /** |
||
281 | * {@inheritdoc} |
||
282 | */ |
||
283 | public function changeMainLocation($contentId, $locationId) |
||
290 | |||
291 | /** |
||
292 | * Get the total number of all existing Locations. Can be combined with loadAllLocations. |
||
293 | * |
||
294 | * @return int |
||
295 | */ |
||
296 | public function countAllLocations() |
||
302 | |||
303 | /** |
||
304 | * Bulk-load all existing Locations, constrained by $limit and $offset to paginate results. |
||
305 | * |
||
306 | * @param int $offset |
||
307 | * @param int $limit |
||
308 | * |
||
309 | * @return \eZ\Publish\SPI\Persistence\Content\Location[] |
||
310 | */ |
||
311 | public function loadAllLocations($offset, $limit) |
||
317 | |||
318 | /** |
||
319 | * Return relevant content and location tags so cache can be purged reliably. |
||
320 | * |
||
321 | * @param \eZ\Publish\SPI\Persistence\Content\Location $location |
||
322 | * @param array $tags Optional, can be used to specify additional tags. |
||
323 | * |
||
324 | * @return array |
||
325 | */ |
||
326 | private function getCacheTags(Location $location, $tags = []) |
||
338 | |||
339 | private function getCacheTranslationKey(array $translations = null, bool $useAlwaysAvailable = true): string |
||
350 | } |
||
351 |
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.