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 |
||
24 | class ContentHandler extends AbstractInMemoryPersistenceHandler implements ContentHandlerInterface |
||
25 | { |
||
26 | const ALL_TRANSLATIONS_KEY = '0'; |
||
27 | |||
28 | /** @var callable */ |
||
29 | private $getContentInfoTags; |
||
30 | |||
31 | /** @var callable */ |
||
32 | private $getContentInfoKeys; |
||
33 | |||
34 | /** @var callable */ |
||
35 | private $getContentTags; |
||
36 | |||
37 | protected function init(): void |
||
38 | { |
||
39 | $this->getContentInfoTags = function (ContentInfo $info, array $tags = []) { |
||
40 | $tags[] = 'content-' . $info->id; |
||
41 | |||
42 | if ($info->mainLocationId) { |
||
43 | $locations = $this->persistenceHandler->locationHandler()->loadLocationsByContent($info->id); |
||
44 | foreach ($locations as $location) { |
||
45 | $tags[] = 'location-' . $location->id; |
||
46 | foreach (explode('/', trim($location->pathString, '/')) as $pathId) { |
||
47 | $tags[] = 'location-path-' . $pathId; |
||
48 | } |
||
49 | } |
||
50 | } |
||
51 | |||
52 | return $tags; |
||
53 | }; |
||
54 | $this->getContentInfoKeys = function (ContentInfo $info) { |
||
55 | return [ |
||
56 | 'ez-content-info-' . $info->id, |
||
57 | 'ez-content-info-byRemoteId-' . $this->escapeForCacheKey($info->remoteId), |
||
58 | ]; |
||
59 | }; |
||
60 | |||
61 | $this->getContentTags = function (Content $content) { |
||
62 | $versionInfo = $content->versionInfo; |
||
63 | $tags = [ |
||
64 | 'content-fields-' . $versionInfo->contentInfo->id, |
||
65 | 'content-fields-type-' . $versionInfo->contentInfo->contentTypeId, |
||
66 | ]; |
||
67 | |||
68 | return $this->getCacheTagsForVersion($versionInfo, $tags); |
||
69 | }; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * {@inheritdoc} |
||
74 | */ |
||
75 | public function create(CreateStruct $struct) |
||
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | */ |
||
86 | public function createDraftFromVersion($contentId, $srcVersion, $userId) |
||
94 | |||
95 | /** |
||
96 | * {@inheritdoc} |
||
97 | */ |
||
98 | public function copy($contentId, $versionNo = null, $newOwnerId = null) |
||
99 | { |
||
100 | $this->logger->logCall(__METHOD__, [ |
||
101 | 'content' => $contentId, |
||
102 | 'version' => $versionNo, |
||
103 | 'newOwner' => $newOwnerId, |
||
104 | ]); |
||
105 | |||
106 | return $this->persistenceHandler->contentHandler()->copy($contentId, $versionNo, $newOwnerId); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * {@inheritdoc} |
||
111 | */ |
||
112 | public function load($contentId, $versionNo = null, array $translations = null) |
||
132 | |||
133 | public function loadContentList(array $contentIds, array $translations = null): array |
||
152 | |||
153 | /** |
||
154 | * {@inheritdoc} |
||
155 | */ |
||
156 | public function loadContentInfo($contentId) |
||
170 | |||
171 | public function loadContentInfoList(array $contentIds) |
||
185 | |||
186 | /** |
||
187 | * {@inheritdoc} |
||
188 | */ |
||
189 | public function loadContentInfoByRemoteId($remoteId) |
||
203 | |||
204 | /** |
||
205 | * {@inheritdoc} |
||
206 | */ |
||
207 | public function loadVersionInfo($contentId, $versionNo = null) |
||
225 | |||
226 | /** |
||
227 | * {@inheritdoc} |
||
228 | */ |
||
229 | public function countDraftsForUser(int $userId): int |
||
235 | |||
236 | /** |
||
237 | * {@inheritdoc} |
||
238 | */ |
||
239 | public function loadDraftsForUser($userId) |
||
245 | |||
246 | /** |
||
247 | * {@inheritdoc} |
||
248 | */ |
||
249 | public function loadDraftListForUser(int $userId, int $offset = 0, int $limit = -1): array |
||
255 | |||
256 | /** |
||
257 | * {@inheritdoc} |
||
258 | */ |
||
259 | public function setStatus($contentId, $status, $versionNo) |
||
272 | |||
273 | /** |
||
274 | * {@inheritdoc} |
||
275 | */ |
||
276 | public function updateMetadata($contentId, MetadataUpdateStruct $struct) |
||
284 | |||
285 | /** |
||
286 | * {@inheritdoc} |
||
287 | */ |
||
288 | public function updateContent($contentId, $versionNo, UpdateStruct $struct) |
||
296 | |||
297 | /** |
||
298 | * {@inheritdoc} |
||
299 | */ |
||
300 | public function deleteContent($contentId) |
||
328 | |||
329 | /** |
||
330 | * {@inheritdoc} |
||
331 | */ |
||
332 | public function deleteVersion($contentId, $versionNo) |
||
340 | |||
341 | /** |
||
342 | * {@inheritdoc} |
||
343 | */ |
||
344 | public function listVersions($contentId, $status = null, $limit = -1) |
||
365 | |||
366 | /** |
||
367 | * {@inheritdoc} |
||
368 | */ |
||
369 | public function addRelation(RelationCreateStruct $relation) |
||
375 | |||
376 | /** |
||
377 | * {@inheritdoc} |
||
378 | */ |
||
379 | public function removeRelation($relationId, $type) |
||
384 | |||
385 | /** |
||
386 | * {@inheritdoc} |
||
387 | */ |
||
388 | public function loadRelations($sourceContentId, $sourceContentVersionNo = null, $type = null) |
||
401 | |||
402 | /** |
||
403 | * {@inheritdoc} |
||
404 | */ |
||
405 | public function countReverseRelations(int $destinationContentId, ?int $type = null): int |
||
411 | |||
412 | /** |
||
413 | * {@inheritdoc} |
||
414 | */ |
||
415 | public function loadReverseRelations($destinationContentId, $type = null) |
||
421 | |||
422 | /** |
||
423 | * {@inheritdoc} |
||
424 | */ |
||
425 | public function loadReverseRelationList( |
||
445 | |||
446 | /** |
||
447 | * {@inheritdoc} |
||
448 | */ |
||
449 | public function publish($contentId, $versionNo, MetadataUpdateStruct $struct) |
||
457 | |||
458 | /** |
||
459 | * {@inheritdoc} |
||
460 | */ |
||
461 | public function removeTranslationFromContent($contentId, $languageCode) |
||
465 | |||
466 | /** |
||
467 | * {@inheritdoc} |
||
468 | */ |
||
469 | public function deleteTranslationFromContent($contentId, $languageCode) |
||
482 | |||
483 | /** |
||
484 | * {@inheritdoc} |
||
485 | */ |
||
486 | public function deleteTranslationFromDraft($contentId, $versionNo, $languageCode) |
||
501 | |||
502 | /** |
||
503 | * Return relevant content and location tags so cache can be purged reliably. |
||
504 | * |
||
505 | * For use when generating cache, not on invalidation. |
||
506 | * |
||
507 | * @param \eZ\Publish\SPI\Persistence\Content\VersionInfo $versionInfo |
||
508 | * @param array $tags Optional, can be used to specify other tags. |
||
509 | * |
||
510 | * @return array |
||
511 | */ |
||
512 | private function getCacheTagsForVersion(VersionInfo $versionInfo, array $tags = []): array |
||
520 | |||
521 | private function getCacheTagsForContent(Content $content): array |
||
531 | } |
||
532 |
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.