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:
Complex classes like ContentHandler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ContentHandler, and based on these observations, apply Extract Interface, too.
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 | $draft = $this->persistenceHandler->contentHandler()->createDraftFromVersion($contentId, $srcVersion, $userId); |
||
45 | $this->cache->invalidateTags(["content-$contentId-version-list"]); |
||
46 | |||
47 | return $draft; |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * {@inheritdoc} |
||
52 | */ |
||
53 | public function copy($contentId, $versionNo = null) |
||
59 | |||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | */ |
||
63 | public function load($contentId, $versionNo, array $translations = null) |
||
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | public function loadContentInfo($contentId) |
||
98 | |||
99 | View Code Duplication | public function loadContentInfoList(array $contentIds) |
|
122 | |||
123 | /** |
||
124 | * {@inheritdoc} |
||
125 | */ |
||
126 | public function loadContentInfoByRemoteId($remoteId) |
||
141 | |||
142 | /** |
||
143 | * {@inheritdoc} |
||
144 | */ |
||
145 | public function loadVersionInfo($contentId, $versionNo) |
||
160 | |||
161 | /** |
||
162 | * {@inheritdoc} |
||
163 | */ |
||
164 | public function loadDraftsForUser($userId) |
||
170 | |||
171 | /** |
||
172 | * {@inheritdoc} |
||
173 | */ |
||
174 | public function setStatus($contentId, $status, $versionNo) |
||
188 | |||
189 | /** |
||
190 | * {@inheritdoc} |
||
191 | */ |
||
192 | public function updateMetadata($contentId, MetadataUpdateStruct $struct) |
||
200 | |||
201 | /** |
||
202 | * {@inheritdoc} |
||
203 | */ |
||
204 | public function updateContent($contentId, $versionNo, UpdateStruct $struct) |
||
212 | |||
213 | /** |
||
214 | * {@inheritdoc} |
||
215 | */ |
||
216 | public function deleteContent($contentId) |
||
243 | |||
244 | /** |
||
245 | * {@inheritdoc} |
||
246 | */ |
||
247 | public function deleteVersion($contentId, $versionNo) |
||
255 | |||
256 | /** |
||
257 | * {@inheritdoc} |
||
258 | */ |
||
259 | public function listVersions($contentId, $status = null, $limit = -1) |
||
275 | |||
276 | /** |
||
277 | * {@inheritdoc} |
||
278 | */ |
||
279 | public function addRelation(RelationCreateStruct $relation) |
||
285 | |||
286 | /** |
||
287 | * {@inheritdoc} |
||
288 | */ |
||
289 | public function removeRelation($relationId, $type) |
||
294 | |||
295 | /** |
||
296 | * {@inheritdoc} |
||
297 | */ |
||
298 | public function loadRelations($sourceContentId, $sourceContentVersionNo = null, $type = null) |
||
311 | |||
312 | /** |
||
313 | * {@inheritdoc} |
||
314 | */ |
||
315 | public function loadReverseRelations($destinationContentId, $type = null) |
||
321 | |||
322 | /** |
||
323 | * {@inheritdoc} |
||
324 | */ |
||
325 | public function publish($contentId, $versionNo, MetadataUpdateStruct $struct) |
||
333 | |||
334 | /** |
||
335 | * {@inheritdoc} |
||
336 | */ |
||
337 | public function removeTranslationFromContent($contentId, $languageCode) |
||
341 | |||
342 | /** |
||
343 | * {@inheritdoc} |
||
344 | */ |
||
345 | public function deleteTranslationFromContent($contentId, $languageCode) |
||
346 | { |
||
347 | $this->logger->logCall( |
||
348 | __METHOD__, |
||
349 | [ |
||
350 | 'contentId' => $contentId, |
||
351 | 'languageCode' => $languageCode, |
||
352 | ] |
||
353 | ); |
||
354 | |||
355 | $this->persistenceHandler->contentHandler()->deleteTranslationFromContent($contentId, $languageCode); |
||
356 | $this->cache->invalidateTags(['content-' . $contentId]); |
||
357 | } |
||
358 | |||
359 | /** |
||
360 | * {@inheritdoc} |
||
361 | */ |
||
362 | public function deleteTranslationFromDraft($contentId, $versionNo, $languageCode) |
||
377 | |||
378 | /** |
||
379 | * Return relevant content and location tags so cache can be purged reliably. |
||
380 | * |
||
381 | * For use when generating cache, not on invalidation. |
||
382 | * |
||
383 | * @param \eZ\Publish\SPI\Persistence\Content\ContentInfo $contentInfo |
||
384 | * @param bool $withFields Set to true if item contains fields which should be expired on relation or type updates. |
||
385 | * @param array $tags Optional, can be used to specify other tags. |
||
386 | * |
||
387 | * @return array |
||
388 | */ |
||
389 | private function getCacheTags(ContentInfo $contentInfo, $withFields = false, array $tags = []) |
||
410 | } |
||
411 |
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.