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 | public function load($locationId) |
||
| 40 | |||
| 41 | /** |
||
| 42 | * {@inheritdoc} |
||
| 43 | */ |
||
| 44 | public function loadSubtreeIds($locationId) |
||
| 45 | { |
||
| 46 | $cacheItem = $this->cache->getItem("ez-location-subtree-${locationId}"); |
||
| 47 | if ($cacheItem->isHit()) { |
||
| 48 | return $cacheItem->get(); |
||
| 49 | } |
||
| 50 | |||
| 51 | $this->logger->logCall(__METHOD__, array('location' => $locationId)); |
||
| 52 | $locationIds = $this->persistenceHandler->locationHandler()->loadSubtreeIds($locationId); |
||
| 53 | |||
| 54 | $cacheItem->set($locationIds); |
||
| 55 | $cacheTags = ['location-'.$locationId, 'location-path-'.$locationId]; |
||
| 56 | foreach ($locationIds as $locationId) { |
||
| 57 | $cacheTags[] = 'location-'.$locationId; |
||
| 58 | $cacheTags[] = 'location-path-'.$locationId; |
||
| 59 | } |
||
| 60 | $cacheItem->tag($cacheTags); |
||
| 61 | $this->cache->save($cacheItem); |
||
| 62 | |||
| 63 | return $locationIds; |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * {@inheritdoc} |
||
| 68 | */ |
||
| 69 | public function loadLocationsByContent($contentId, $rootLocationId = null) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * {@inheritdoc} |
||
| 98 | */ |
||
| 99 | public function loadParentLocationsForDraftContent($contentId) |
||
| 119 | |||
| 120 | /** |
||
| 121 | * {@inheritdoc} |
||
| 122 | */ |
||
| 123 | public function loadByRemoteId($remoteId) |
||
| 124 | { |
||
| 125 | $cacheItem = $this->cache->getItem("ez-location-${remoteId}-by-remoteid"); |
||
| 126 | if ($cacheItem->isHit()) { |
||
| 127 | return $cacheItem->get(); |
||
| 128 | } |
||
| 129 | |||
| 130 | $this->logger->logCall(__METHOD__, array('location' => $remoteId)); |
||
| 131 | $location = $this->persistenceHandler->locationHandler()->loadByRemoteId($remoteId); |
||
| 132 | |||
| 133 | $cacheItem->set($location); |
||
| 134 | $cacheItem->tag($this->getCacheTags($location)); |
||
| 135 | $this->cache->save($cacheItem); |
||
| 136 | |||
| 137 | return $location; |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * {@inheritdoc} |
||
| 142 | */ |
||
| 143 | public function copySubtree($sourceId, $destinationParentId) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * {@inheritdoc} |
||
| 152 | */ |
||
| 153 | public function move($sourceId, $destinationParentId) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * {@inheritdoc} |
||
| 165 | */ |
||
| 166 | public function markSubtreeModified($locationId, $timestamp = null) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * {@inheritdoc} |
||
| 174 | */ |
||
| 175 | public function hide($locationId) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * {@inheritdoc} |
||
| 187 | */ |
||
| 188 | public function unHide($locationId) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * {@inheritdoc} |
||
| 200 | */ |
||
| 201 | public function swap($locationId1, $locationId2) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * {@inheritdoc} |
||
| 215 | */ |
||
| 216 | public function update(UpdateStruct $struct, $locationId) |
||
| 217 | { |
||
| 218 | $this->logger->logCall(__METHOD__, array('location' => $locationId, 'struct' => $struct)); |
||
| 219 | $this->persistenceHandler->locationHandler()->update($struct, $locationId); |
||
| 220 | |||
| 221 | $this->cache->invalidateTags(['location-'.$locationId]); |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * {@inheritdoc} |
||
| 226 | */ |
||
| 227 | View Code Duplication | public function create(CreateStruct $locationStruct) |
|
| 228 | { |
||
| 229 | $this->logger->logCall(__METHOD__, array('struct' => $locationStruct)); |
||
| 230 | $location = $this->persistenceHandler->locationHandler()->create($locationStruct); |
||
| 231 | |||
| 232 | // need to clear loadLocationsByContent and similar collections involving locations data |
||
| 233 | // also need to clear content info on main location changes |
||
| 234 | $this->cache->invalidateTags(['content-'.$locationStruct->contentId, 'role-assignment-group-list-'. $locationStruct->contentId]); |
||
| 235 | |||
| 236 | return $location; |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * {@inheritdoc} |
||
| 241 | */ |
||
| 242 | public function removeSubtree($locationId) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * {@inheritdoc} |
||
| 254 | */ |
||
| 255 | public function setSectionForSubtree($locationId, $sectionId) |
||
| 256 | { |
||
| 257 | $this->logger->logCall(__METHOD__, array('location' => $locationId, 'section' => $sectionId)); |
||
| 258 | $this->persistenceHandler->locationHandler()->setSectionForSubtree($locationId, $sectionId); |
||
| 259 | |||
| 260 | $this->cache->invalidateTags(['location-path-'.$locationId]); |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * {@inheritdoc} |
||
| 265 | */ |
||
| 266 | public function changeMainLocation($contentId, $locationId) |
||
| 267 | { |
||
| 268 | $this->logger->logCall(__METHOD__, array('location' => $locationId, 'content' => $contentId)); |
||
| 269 | $this->persistenceHandler->locationHandler()->changeMainLocation($contentId, $locationId); |
||
| 270 | |||
| 271 | $this->cache->invalidateTags(['content-'.$contentId]); |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Return relevant content and location tags so cache can be purged reliably. |
||
| 276 | * |
||
| 277 | * @param Location $location |
||
| 278 | * @param array $tags Optional, can be used to specify additional tags. |
||
| 279 | * |
||
| 280 | * @return array |
||
| 281 | */ |
||
| 282 | private function getCacheTags(Location $location, $tags = []) |
||
| 293 | } |
||
| 294 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: