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 UrlAliasHandler extends AbstractHandler implements UrlAliasHandlerInterface |
||
20 | { |
||
21 | /** |
||
22 | * Constant used for storing not found results for lookup(). |
||
23 | */ |
||
24 | const NOT_FOUND = 0; |
||
25 | |||
26 | /** |
||
27 | * {@inheritdoc} |
||
28 | */ |
||
29 | View Code Duplication | public function publishUrlAliasForLocation( |
|
59 | |||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | */ |
||
63 | View Code Duplication | public function createCustomUrlAlias($locationId, $path, $forwarding = false, $languageCode = null, $alwaysAvailable = false) |
|
88 | |||
89 | /** |
||
90 | * {@inheritdoc} |
||
91 | */ |
||
92 | public function createGlobalUrlAlias($resource, $path, $forwarding = false, $languageCode = null, $alwaysAvailable = false) |
||
93 | { |
||
94 | $this->logger->logCall( |
||
95 | __METHOD__, |
||
96 | array( |
||
97 | 'resource' => $resource, |
||
98 | 'path' => $path, |
||
99 | 'forwarding' => $forwarding, |
||
100 | 'language' => $languageCode, |
||
101 | 'alwaysAvailable' => $alwaysAvailable, |
||
102 | ) |
||
103 | ); |
||
104 | |||
105 | $urlAlias = $this->persistenceHandler->urlAliasHandler()->createGlobalUrlAlias( |
||
106 | $resource, |
||
107 | $path, |
||
108 | $forwarding, |
||
109 | $languageCode, |
||
110 | $alwaysAvailable |
||
111 | ); |
||
112 | |||
113 | $this->cache->invalidateTags(['urlAlias-notFound']); |
||
114 | |||
115 | return $urlAlias; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * {@inheritdoc} |
||
120 | */ |
||
121 | public function listGlobalURLAliases($languageCode = null, $offset = 0, $limit = -1) |
||
127 | |||
128 | /** |
||
129 | * {@inheritdoc} |
||
130 | */ |
||
131 | public function listURLAliasesForLocation($locationId, $custom = false) |
||
151 | |||
152 | /** |
||
153 | * {@inheritdoc} |
||
154 | */ |
||
155 | public function removeURLAliases(array $urlAliases) |
||
175 | |||
176 | /** |
||
177 | * {@inheritdoc} |
||
178 | */ |
||
179 | public function lookup($url) |
||
207 | |||
208 | /** |
||
209 | * {@inheritdoc} |
||
210 | */ |
||
211 | public function loadUrlAlias($id) |
||
212 | { |
||
213 | $cacheItem = $this->cache->getItem('ez-urlAlias-' . $id); |
||
214 | if ($cacheItem->isHit()) { |
||
215 | return $cacheItem->get(); |
||
216 | } |
||
217 | |||
218 | $this->logger->logCall(__METHOD__, array('alias' => $id)); |
||
219 | $urlAlias = $this->persistenceHandler->urlAliasHandler()->loadUrlAlias($id); |
||
220 | |||
221 | $cacheItem->set($urlAlias); |
||
222 | $cacheItem->tag($this->getCacheTags($urlAlias)); |
||
223 | $this->cache->save($cacheItem); |
||
224 | |||
225 | return $urlAlias; |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * {@inheritdoc} |
||
230 | */ |
||
231 | View Code Duplication | public function locationMoved($locationId, $oldParentId, $newParentId) |
|
248 | |||
249 | /** |
||
250 | * {@inheritdoc} |
||
251 | */ |
||
252 | View Code Duplication | public function locationCopied($locationId, $newLocationId, $newParentId) |
|
272 | |||
273 | /** |
||
274 | * {@inheritdoc} |
||
275 | */ |
||
276 | public function locationDeleted($locationId) |
||
277 | { |
||
278 | $this->logger->logCall(__METHOD__, array('location' => $locationId)); |
||
279 | $return = $this->persistenceHandler->urlAliasHandler()->locationDeleted($locationId); |
||
280 | |||
281 | $this->cache->invalidateTags(['urlAlias-location-' . $locationId, 'urlAlias-location-path-' . $locationId]); |
||
282 | |||
283 | return $return; |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * {@inheritdoc} |
||
288 | */ |
||
289 | public function locationSwapped($location1Id, $location1ParentId, $location2Id, $location2ParentId) |
||
319 | |||
320 | /** |
||
321 | * {@inheritdoc} |
||
322 | */ |
||
323 | public function translationRemoved(array $locationIds, $languageCode) |
||
340 | |||
341 | /** |
||
342 | * {@inheritdoc} |
||
343 | */ |
||
344 | View Code Duplication | public function archiveUrlAliasesForDeletedTranslations($locationId, $parentLocationId, array $languageCodes) |
|
345 | { |
||
346 | $this->logger->logCall( |
||
347 | __METHOD__, |
||
348 | [ |
||
349 | 'locationId' => $locationId, |
||
350 | 'parentLocationId' => $parentLocationId, |
||
351 | 'languageCodes' => implode(',', $languageCodes), |
||
352 | ] |
||
353 | ); |
||
354 | |||
355 | $this->persistenceHandler->urlAliasHandler()->archiveUrlAliasesForDeletedTranslations( |
||
356 | $locationId, |
||
357 | $parentLocationId, |
||
358 | $languageCodes |
||
359 | ); |
||
360 | |||
361 | $this->cache->invalidateTags(['urlAlias-location-' . $locationId, 'urlAlias-location-path-' . $locationId]); |
||
362 | } |
||
363 | |||
364 | /** |
||
365 | * Return relevant UrlAlias and optionally UrlAlias location tags so cache can be purged reliably. |
||
366 | * |
||
367 | * For use when generating cache, not on invalidation. |
||
368 | * |
||
369 | * @param \eZ\Publish\SPI\Persistence\Content\UrlAlias $urlAlias |
||
370 | * @param array $tags Optional, can be used to specify other tags. |
||
371 | * |
||
372 | * @return array |
||
373 | */ |
||
374 | private function getCacheTags(UrlAlias $urlAlias, array $tags = []) |
||
388 | } |
||
389 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.