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) |
|
| 25 | { |
||
| 26 | $cache = $this->cache->getItem('location', $locationId); |
||
| 27 | $location = $cache->get(); |
||
| 28 | if ($cache->isMiss()) { |
||
| 29 | $this->logger->logCall(__METHOD__, array('location' => $locationId)); |
||
| 30 | $cache->set($location = $this->persistenceHandler->locationHandler()->load($locationId))->save(); |
||
| 31 | } |
||
| 32 | |||
| 33 | return $location; |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * {@inheritdoc} |
||
| 38 | */ |
||
| 39 | View Code Duplication | public function loadSubtreeIds($locationId) |
|
| 40 | { |
||
| 41 | $cache = $this->cache->getItem('location', 'subtree', $locationId); |
||
| 42 | $locationIds = $cache->get(); |
||
| 43 | |||
| 44 | if ($cache->isMiss()) { |
||
| 45 | $this->logger->logCall(__METHOD__, array('location' => $locationId)); |
||
| 46 | $cache->set( |
||
| 47 | $locationIds = $this->persistenceHandler->locationHandler()->loadSubtreeIds($locationId) |
||
| 48 | )->save(); |
||
| 49 | } |
||
| 50 | |||
| 51 | return $locationIds; |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * {@inheritdoc} |
||
| 56 | */ |
||
| 57 | View Code Duplication | public function loadLocationsByContent($contentId, $rootLocationId = null) |
|
| 58 | { |
||
| 59 | if ($rootLocationId) { |
||
|
|
|||
| 60 | $cache = $this->cache->getItem('content', 'locations', $contentId, 'root', $rootLocationId); |
||
| 61 | } else { |
||
| 62 | $cache = $this->cache->getItem('content', 'locations', $contentId); |
||
| 63 | } |
||
| 64 | $locationIds = $cache->get(); |
||
| 65 | if ($cache->isMiss()) { |
||
| 66 | $this->logger->logCall(__METHOD__, array('content' => $contentId, 'root' => $rootLocationId)); |
||
| 67 | $locations = $this->persistenceHandler->locationHandler()->loadLocationsByContent($contentId, $rootLocationId); |
||
| 68 | |||
| 69 | $locationIds = array(); |
||
| 70 | foreach ($locations as $location) { |
||
| 71 | $locationIds[] = $location->id; |
||
| 72 | } |
||
| 73 | |||
| 74 | $cache->set($locationIds)->save(); |
||
| 75 | } else { |
||
| 76 | $locations = array(); |
||
| 77 | foreach ($locationIds as $locationId) { |
||
| 78 | $locations[] = $this->load($locationId); |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | return $locations; |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * {@inheritdoc} |
||
| 87 | */ |
||
| 88 | public function loadParentLocationsForDraftContent($contentId) |
||
| 89 | { |
||
| 90 | $cache = $this->cache->getItem('content', 'locations', $contentId, 'parentLocationsForDraftContent'); |
||
| 91 | $locationIds = $cache->get(); |
||
| 92 | if ($cache->isMiss()) { |
||
| 93 | $this->logger->logCall(__METHOD__, array('content' => $contentId)); |
||
| 94 | $locations = $this->persistenceHandler->locationHandler()->loadParentLocationsForDraftContent($contentId); |
||
| 95 | |||
| 96 | $locationIds = array(); |
||
| 97 | foreach ($locations as $location) { |
||
| 98 | $locationIds[] = $location->id; |
||
| 99 | } |
||
| 100 | |||
| 101 | $cache->set($locationIds)->save(); |
||
| 102 | } else { |
||
| 103 | $locations = array(); |
||
| 104 | foreach ($locationIds as $locationId) { |
||
| 105 | $locations[] = $this->load($locationId); |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | return $locations; |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * {@inheritdoc} |
||
| 114 | */ |
||
| 115 | public function loadByRemoteId($remoteId) |
||
| 116 | { |
||
| 117 | $this->logger->logCall(__METHOD__, array('location' => $remoteId)); |
||
| 118 | |||
| 119 | return $this->persistenceHandler->locationHandler()->loadByRemoteId($remoteId); |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * {@inheritdoc} |
||
| 124 | */ |
||
| 125 | public function copySubtree($sourceId, $destinationParentId) |
||
| 126 | { |
||
| 127 | $this->logger->logCall(__METHOD__, array('source' => $sourceId, 'destination' => $destinationParentId)); |
||
| 128 | |||
| 129 | return $this->persistenceHandler->locationHandler()->copySubtree($sourceId, $destinationParentId); |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * {@inheritdoc} |
||
| 134 | */ |
||
| 135 | public function move($sourceId, $destinationParentId) |
||
| 136 | { |
||
| 137 | $this->logger->logCall(__METHOD__, array('source' => $sourceId, 'destination' => $destinationParentId)); |
||
| 138 | $return = $this->persistenceHandler->locationHandler()->move($sourceId, $destinationParentId); |
||
| 139 | |||
| 140 | $this->cache->clear('location');//TIMBER! (path[Identification]String) |
||
| 141 | $this->cache->clear('user', 'role', 'assignments', 'byGroup'); |
||
| 142 | $this->cache->clear('content');//TIMBER! |
||
| 143 | |||
| 144 | return $return; |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * {@inheritdoc} |
||
| 149 | */ |
||
| 150 | public function markSubtreeModified($locationId, $timestamp = null) |
||
| 151 | { |
||
| 152 | $this->logger->logCall(__METHOD__, array('location' => $locationId, 'time' => $timestamp)); |
||
| 153 | $this->persistenceHandler->locationHandler()->markSubtreeModified($locationId, $timestamp); |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * {@inheritdoc} |
||
| 158 | */ |
||
| 159 | public function hide($locationId) |
||
| 160 | { |
||
| 161 | $this->logger->logCall(__METHOD__, array('location' => $locationId)); |
||
| 162 | $return = $this->persistenceHandler->locationHandler()->hide($locationId); |
||
| 163 | |||
| 164 | $this->cache->clear('location');//TIMBER! (visibility) |
||
| 165 | |||
| 166 | return $return; |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * {@inheritdoc} |
||
| 171 | */ |
||
| 172 | public function unHide($locationId) |
||
| 173 | { |
||
| 174 | $this->logger->logCall(__METHOD__, array('location' => $locationId)); |
||
| 175 | $return = $this->persistenceHandler->locationHandler()->unHide($locationId); |
||
| 176 | |||
| 177 | $this->cache->clear('location');//TIMBER! (visibility) |
||
| 178 | |||
| 179 | return $return; |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * {@inheritdoc} |
||
| 184 | */ |
||
| 185 | public function swap($locationId1, $locationId2) |
||
| 186 | { |
||
| 187 | $this->logger->logCall(__METHOD__, array('location1' => $locationId1, 'location2' => $locationId2)); |
||
| 188 | $locationHandler = $this->persistenceHandler->locationHandler(); |
||
| 189 | |||
| 190 | $return = $locationHandler->swap($locationId1, $locationId2); |
||
| 191 | |||
| 192 | $this->cache->clear('location', $locationId1); |
||
| 193 | $this->cache->clear('location', $locationId2); |
||
| 194 | $this->cache->clear('location', 'subtree'); |
||
| 195 | $this->cache->clear('content', 'locations'); |
||
| 196 | $this->cache->clear('user', 'role', 'assignments', 'byGroup'); |
||
| 197 | |||
| 198 | // This ensures that Content cache is updated for possible main Location change |
||
| 199 | $location1 = $this->load($locationId1); |
||
| 200 | $location2 = $this->load($locationId2); |
||
| 201 | $this->cache->clear('content', $location1->contentId); |
||
| 202 | $this->cache->clear('content', $location2->contentId); |
||
| 203 | $this->cache->clear('content', 'info', $location1->contentId); |
||
| 204 | $this->cache->clear('content', 'info', $location2->contentId); |
||
| 205 | |||
| 206 | return $return; |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * {@inheritdoc} |
||
| 211 | */ |
||
| 212 | public function update(UpdateStruct $struct, $locationId) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * {@inheritdoc} |
||
| 222 | */ |
||
| 223 | public function create(CreateStruct $locationStruct) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * {@inheritdoc} |
||
| 241 | */ |
||
| 242 | public function removeSubtree($locationId) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * {@inheritdoc} |
||
| 256 | */ |
||
| 257 | public function setSectionForSubtree($locationId, $sectionId) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * {@inheritdoc} |
||
| 266 | */ |
||
| 267 | public function changeMainLocation($contentId, $locationId) |
||
| 275 | } |
||
| 276 |
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: