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 |
||
| 23 | class ContentHandler extends AbstractHandler implements ContentHandlerInterface |
||
| 24 | { |
||
| 25 | const ALL_TRANSLATIONS_KEY = '0'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * {@inheritdoc} |
||
| 29 | */ |
||
| 30 | public function create(CreateStruct $struct) |
||
| 31 | { |
||
| 32 | // Cached on demand when published or loaded |
||
| 33 | $this->logger->logCall(__METHOD__, array('struct' => $struct)); |
||
| 34 | |||
| 35 | return $this->persistenceHandler->contentHandler()->create($struct); |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * {@inheritdoc} |
||
| 40 | */ |
||
| 41 | public function createDraftFromVersion($contentId, $srcVersion, $userId) |
||
| 42 | { |
||
| 43 | $this->logger->logCall(__METHOD__, array('content' => $contentId, 'version' => $srcVersion, 'user' => $userId)); |
||
| 44 | |||
| 45 | return $this->persistenceHandler->contentHandler()->createDraftFromVersion($contentId, $srcVersion, $userId); |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * {@inheritdoc} |
||
| 50 | */ |
||
| 51 | public function copy($contentId, $versionNo = null) |
||
| 52 | { |
||
| 53 | $this->logger->logCall(__METHOD__, array('content' => $contentId, 'version' => $versionNo)); |
||
| 54 | |||
| 55 | return $this->persistenceHandler->contentHandler()->copy($contentId, $versionNo); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * {@inheritdoc} |
||
| 60 | */ |
||
| 61 | public function load($contentId, $version, array $translations = null) |
||
| 62 | { |
||
| 63 | $translationsKey = empty($translations) ? self::ALL_TRANSLATIONS_KEY : implode('|', $translations); |
||
| 64 | $cache = $this->cache->getItem('content', $contentId, $version, $translationsKey); |
||
| 65 | $content = $cache->get(); |
||
| 66 | if ($cache->isMiss()) { |
||
| 67 | $this->logger->logCall(__METHOD__, array('content' => $contentId, 'version' => $version, 'translations' => $translations)); |
||
| 68 | $content = $this->persistenceHandler->contentHandler()->load($contentId, $version, $translations); |
||
|
|
|||
| 69 | $cache->set($content)->save(); |
||
| 70 | } |
||
| 71 | |||
| 72 | return $content; |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * {@inheritdoc} |
||
| 77 | */ |
||
| 78 | View Code Duplication | public function loadContentInfo($contentId) |
|
| 79 | { |
||
| 80 | $cache = $this->cache->getItem('content', 'info', $contentId); |
||
| 81 | $contentInfo = $cache->get(); |
||
| 82 | if ($cache->isMiss()) { |
||
| 83 | $this->logger->logCall(__METHOD__, array('content' => $contentId)); |
||
| 84 | $cache->set($contentInfo = $this->persistenceHandler->contentHandler()->loadContentInfo($contentId))->save(); |
||
| 85 | } |
||
| 86 | |||
| 87 | return $contentInfo; |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * {@inheritdoc} |
||
| 92 | */ |
||
| 93 | View Code Duplication | public function loadContentInfoByRemoteId($remoteId) |
|
| 94 | { |
||
| 95 | $cache = $this->cache->getItem('content', 'info', 'remoteId', $remoteId); |
||
| 96 | $contentInfo = $cache->get(); |
||
| 97 | if ($cache->isMiss()) { |
||
| 98 | $this->logger->logCall(__METHOD__, array('content' => $remoteId)); |
||
| 99 | $cache->set($contentInfo = $this->persistenceHandler->contentHandler()->loadContentInfoByRemoteId($remoteId))->save(); |
||
| 100 | } |
||
| 101 | |||
| 102 | return $contentInfo; |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * {@inheritdoc} |
||
| 107 | */ |
||
| 108 | public function loadVersionInfo($contentId, $versionNo) |
||
| 109 | { |
||
| 110 | $this->logger->logCall(__METHOD__, array('content' => $contentId, 'version' => $versionNo)); |
||
| 111 | |||
| 112 | return $this->persistenceHandler->contentHandler()->loadVersionInfo($contentId, $versionNo); |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * {@inheritdoc} |
||
| 117 | */ |
||
| 118 | public function loadDraftsForUser($userId) |
||
| 119 | { |
||
| 120 | $this->logger->logCall(__METHOD__, array('user' => $userId)); |
||
| 121 | |||
| 122 | return $this->persistenceHandler->contentHandler()->loadDraftsForUser($userId); |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * {@inheritdoc} |
||
| 127 | */ |
||
| 128 | public function setStatus($contentId, $status, $version) |
||
| 129 | { |
||
| 130 | $this->logger->logCall(__METHOD__, array('content' => $contentId, 'status' => $status, 'version' => $version)); |
||
| 131 | $return = $this->persistenceHandler->contentHandler()->setStatus($contentId, $status, $version); |
||
| 132 | |||
| 133 | $this->cache->clear('content', $contentId, $version); |
||
| 134 | if ($status === VersionInfo::STATUS_PUBLISHED) { |
||
| 135 | $this->cache->clear('content', 'info', $contentId); |
||
| 136 | $this->cache->clear('content', 'info', 'remoteId'); |
||
| 137 | } |
||
| 138 | |||
| 139 | return $return; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * {@inheritdoc} |
||
| 144 | */ |
||
| 145 | public function updateMetadata($contentId, MetadataUpdateStruct $struct) |
||
| 146 | { |
||
| 147 | $this->logger->logCall(__METHOD__, array('content' => $contentId, 'struct' => $struct)); |
||
| 148 | |||
| 149 | $contentInfo = $this->persistenceHandler->contentHandler()->updateMetadata($contentId, $struct); |
||
| 150 | |||
| 151 | $this->cache->clear('content', $contentId, $contentInfo->currentVersionNo); |
||
| 152 | $this->cache->clear('content', 'info', $contentId); |
||
| 153 | |||
| 154 | if ($struct->remoteId) { |
||
| 155 | // remote id changed |
||
| 156 | $this->cache->clear('content', 'info', 'remoteId'); |
||
| 157 | } else { |
||
| 158 | $this->cache->clear('content', 'info', 'remoteId', $contentInfo->remoteId); |
||
| 159 | } |
||
| 160 | |||
| 161 | return $contentInfo; |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * {@inheritdoc} |
||
| 166 | */ |
||
| 167 | View Code Duplication | public function updateContent($contentId, $versionNo, UpdateStruct $struct) |
|
| 168 | { |
||
| 169 | $this->logger->logCall(__METHOD__, array('content' => $contentId, 'version' => $versionNo, 'struct' => $struct)); |
||
| 170 | $content = $this->persistenceHandler->contentHandler()->updateContent($contentId, $versionNo, $struct); |
||
| 171 | $this->cache->clear('content', $contentId, $versionNo); |
||
| 172 | |||
| 173 | return $content; |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * {@inheritdoc} |
||
| 178 | */ |
||
| 179 | public function deleteContent($contentId) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * {@inheritdoc} |
||
| 211 | */ |
||
| 212 | public function deleteVersion($contentId, $versionNo) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * {@inheritdoc} |
||
| 227 | */ |
||
| 228 | public function listVersions($contentId, $status = null, $limit = -1) |
||
| 229 | { |
||
| 230 | $this->logger->logCall(__METHOD__, array('content' => $contentId, 'status' => $status)); |
||
| 231 | |||
| 232 | return $this->persistenceHandler->contentHandler()->listVersions($contentId, $status, $limit); |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * {@inheritdoc} |
||
| 237 | */ |
||
| 238 | public function addRelation(RelationCreateStruct $relation) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * {@inheritdoc} |
||
| 247 | */ |
||
| 248 | public function removeRelation($relationId, $type) |
||
| 253 | |||
| 254 | /** |
||
| 255 | * {@inheritdoc} |
||
| 256 | */ |
||
| 257 | public function loadRelations($sourceContentId, $sourceContentVersionNo = null, $type = null) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * {@inheritdoc} |
||
| 273 | */ |
||
| 274 | public function loadReverseRelations($destinationContentId, $type = null) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * {@inheritdoc} |
||
| 283 | */ |
||
| 284 | public function publish($contentId, $versionNo, MetadataUpdateStruct $struct) |
||
| 303 | |||
| 304 | /** |
||
| 305 | * {@inheritdoc} |
||
| 306 | */ |
||
| 307 | public function removeTranslationFromContent($contentId, $languageCode) |
||
| 308 | { |
||
| 309 | $this->deleteTranslationFromContent($contentId, $languageCode); |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * {@inheritdoc} |
||
| 314 | */ |
||
| 315 | public function deleteTranslationFromContent($contentId, $languageCode) |
||
| 316 | { |
||
| 330 | |||
| 331 | /** |
||
| 332 | * {@inheritdoc} |
||
| 333 | */ |
||
| 334 | View Code Duplication | public function deleteTranslationFromDraft($contentId, $versionNo, $languageCode) |
|
| 349 | } |
||
| 350 |
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.