Complex classes like EntityContent 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 EntityContent, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
50 | abstract class EntityContent extends AbstractContent { |
||
51 | |||
52 | /** |
||
53 | * Flag for use with prepareSave(), indicating that no pre-save validation should be applied. |
||
54 | * Can be passed in via EditEntity::attemptSave, EntityStore::saveEntity, |
||
55 | * as well as WikiPage::doEditContent() |
||
56 | * |
||
57 | * @note: must not collide with the EDIT_XXX flags defined by MediaWiki core in Defines.php. |
||
58 | */ |
||
59 | const EDIT_IGNORE_CONSTRAINTS = 1024; |
||
60 | |||
61 | /** |
||
62 | * @see Content::isValid() |
||
63 | * |
||
64 | * @return bool True if this content object is valid for saving. False if there is no entity, or |
||
65 | * the entity does not have an ID set. |
||
66 | */ |
||
67 | public function isValid() { |
||
79 | |||
80 | /** |
||
81 | * @see EntityContent::isCountable |
||
82 | * |
||
83 | * @param bool|null $hasLinks |
||
84 | * |
||
85 | * @return bool True if this is not a redirect and the item is not empty. |
||
86 | */ |
||
87 | public function isCountable( $hasLinks = null ) { |
||
90 | |||
91 | /** |
||
92 | * Returns the EntityRedirect represented by this EntityContent, or null if this |
||
93 | * EntityContent is not a redirect. |
||
94 | * |
||
95 | * @note This default implementation will fail if isRedirect() is true. |
||
96 | * Subclasses that support redirects must override getEntityRedirect(). |
||
97 | * |
||
98 | * @throws LogicException |
||
99 | * @return EntityRedirect|null |
||
100 | */ |
||
101 | public function getEntityRedirect() { |
||
108 | |||
109 | /** |
||
110 | * Returns the entity contained by this entity content. |
||
111 | * Deriving classes typically have a more specific get method as |
||
112 | * for greater clarity and type hinting. |
||
113 | * |
||
114 | * @throws MWException when it's a redirect (targets will never be resolved) |
||
115 | * @throws LogicException if the content object is empty and does not contain an entity. |
||
116 | * @return EntityDocument |
||
117 | */ |
||
118 | abstract public function getEntity(); |
||
119 | |||
120 | /** |
||
121 | * Returns a holder for the entity contained in this EntityContent object. |
||
122 | * |
||
123 | * @throws MWException when it's a redirect (targets will never be resolved) |
||
124 | * @return EntityHolder|null |
||
125 | */ |
||
126 | abstract protected function getEntityHolder(); |
||
127 | |||
128 | /** |
||
129 | * @throws RuntimeException if the content object is empty or no entity ID is set |
||
130 | * @return EntityId |
||
131 | */ |
||
132 | public function getEntityId(): EntityId { |
||
147 | |||
148 | /** |
||
149 | * Returns a ParserOutput object containing the HTML. |
||
150 | * |
||
151 | * @note this calls ParserOutput::recordOption( 'userlang' ) to split the cache |
||
152 | * by user language, and ParserOutput::recordOption( 'wb' ) to split the cache on |
||
153 | * EntityHandler::PARSER_VERSION. |
||
154 | * |
||
155 | * @see Content::getParserOutput |
||
156 | * |
||
157 | * @param Title $title |
||
158 | * @param int|null $revisionId |
||
159 | * @param ParserOptions|null $options |
||
160 | * @param bool $generateHtml |
||
161 | * |
||
162 | * @return ParserOutput |
||
163 | */ |
||
164 | public function getParserOutput( |
||
187 | |||
188 | /** |
||
189 | * @note Will fail if this EntityContent does not represent a redirect. |
||
190 | * |
||
191 | * @param bool $generateHtml |
||
192 | * |
||
193 | * @return ParserOutput |
||
194 | */ |
||
195 | protected function getParserOutputForRedirect( $generateHtml ) { |
||
216 | |||
217 | /** |
||
218 | * @note Will fail if this EntityContent represents a redirect. |
||
219 | * |
||
220 | * @param int|null $revisionId |
||
221 | * @param ParserOptions $options |
||
222 | * @param bool $generateHtml |
||
223 | * |
||
224 | * @return ParserOutput |
||
225 | */ |
||
226 | protected function getParserOutputFromEntityView( |
||
250 | |||
251 | private function getValidUserLanguage( Language $language ) { |
||
258 | |||
259 | /** |
||
260 | * @param int|null $revisionId |
||
261 | * |
||
262 | * @return EntityRevision |
||
263 | */ |
||
264 | private function getEntityRevision( $revisionId = null ) { |
||
276 | |||
277 | /** |
||
278 | * @return string A string representing the content in a way useful for building a full text |
||
279 | * search index. |
||
280 | */ |
||
281 | public function getTextForSearchIndex() { |
||
295 | |||
296 | /** |
||
297 | * @return string Returns the string representation of the redirect |
||
298 | * represented by this EntityContent (if any). |
||
299 | * |
||
300 | * @note Will fail if this EntityContent is not a redirect. |
||
301 | */ |
||
302 | protected function getRedirectText() { |
||
306 | |||
307 | /** |
||
308 | * Get the keys within this Contents Entity JSON that should be removed for |
||
309 | * text passed to edit filters. |
||
310 | * |
||
311 | * @return string[] Keys to ignore |
||
312 | */ |
||
313 | abstract protected function getIgnoreKeysForFilters(); |
||
314 | |||
315 | /** |
||
316 | * @return string A string representing the content in a way useful for content filtering as |
||
317 | * performed by extensions like AbuseFilter. |
||
318 | */ |
||
319 | public function getTextForFilters() { |
||
333 | |||
334 | /** |
||
335 | * @return string The wikitext to include when another page includes this content, or false if |
||
336 | * the content is not includable in a wikitext page. |
||
337 | */ |
||
338 | public function getWikitextForTransclusion() { |
||
341 | |||
342 | /** |
||
343 | * Returns a textual representation of the content suitable for use in edit summaries and log messages. |
||
344 | * |
||
345 | * @param int $maxLength maximum length of the summary text |
||
346 | * @return string |
||
347 | * @throws MWException |
||
348 | */ |
||
349 | public function getTextForSummary( $maxLength = 250 ) { |
||
379 | |||
380 | /** |
||
381 | * Returns an array structure for the redirect represented by this EntityContent, if any. |
||
382 | * |
||
383 | * @note This may or may not be consistent with what EntityContentCodec does. |
||
384 | * It it intended to be used primarily for diffing. |
||
385 | */ |
||
386 | private function getRedirectData() { |
||
400 | |||
401 | /** |
||
402 | * @see Content::getNativeData |
||
403 | * |
||
404 | * @note Avoid relying on this method! It bypasses EntityContentCodec, and does |
||
405 | * not make any guarantees about the structure of the array returned. |
||
406 | * |
||
407 | * @return array|EntityDocument An undefined data structure representing the content. This is |
||
408 | * not guaranteed to conform to any serialization structure used in the database or externally. |
||
409 | */ |
||
410 | public function getNativeData() { |
||
423 | |||
424 | /** |
||
425 | * returns the content's nominal size in bogo-bytes. |
||
426 | * |
||
427 | * @return int |
||
428 | */ |
||
429 | public function getSize() { |
||
432 | |||
433 | /** |
||
434 | * Both contents will be considered equal if they have the same ID and equal Entity data. If |
||
435 | * one of the contents is considered "new", then matching IDs is not a criteria for them to be |
||
436 | * considered equal. |
||
437 | * |
||
438 | * @see Content::equals |
||
439 | * |
||
440 | * @param Content|null $that |
||
441 | * |
||
442 | * @return bool |
||
443 | */ |
||
444 | public function equals( Content $that = null ) { |
||
483 | |||
484 | /** |
||
485 | * @return EntityDocument |
||
486 | */ |
||
487 | private function makeEmptyEntity() { |
||
491 | |||
492 | /** |
||
493 | * Returns a diff between this EntityContent and the given EntityContent. |
||
494 | * |
||
495 | * @param self $toContent |
||
496 | * |
||
497 | * @return EntityContentDiff |
||
498 | */ |
||
499 | public function getDiff( EntityContent $toContent ) { |
||
520 | |||
521 | /** |
||
522 | * Returns a patched copy of this Content object. |
||
523 | * |
||
524 | * @param EntityContentDiff $patch |
||
525 | * |
||
526 | * @throws PatcherException |
||
527 | * @return self |
||
528 | */ |
||
529 | public function getPatchedCopy( EntityContentDiff $patch ) { |
||
560 | |||
561 | /** |
||
562 | * @param Diff $redirectPatch |
||
563 | * |
||
564 | * @return EntityRedirect|null |
||
565 | */ |
||
566 | private function getPatchedRedirect( Diff $redirectPatch ) { |
||
586 | |||
587 | /** |
||
588 | * @return bool True if this is not a redirect and the page is empty. |
||
589 | */ |
||
590 | public function isEmpty() { |
||
598 | |||
599 | /** |
||
600 | * @see Content::copy |
||
601 | * |
||
602 | * @return self |
||
603 | */ |
||
604 | public function copy() { |
||
619 | |||
620 | /** |
||
621 | * @see Content::prepareSave |
||
622 | * |
||
623 | * @param WikiPage $page |
||
624 | * @param int $flags |
||
625 | * @param int $baseRevId |
||
626 | * @param User $user |
||
627 | * |
||
628 | * @return Status |
||
629 | */ |
||
630 | public function prepareSave( WikiPage $page, $flags, $baseRevId, User $user ) { |
||
647 | |||
648 | /** |
||
649 | * Apply the given validators. |
||
650 | * |
||
651 | * @param EntityValidator[] $validators |
||
652 | * |
||
653 | * @return Status |
||
654 | */ |
||
655 | private function applyValidators( array $validators ) { |
||
670 | |||
671 | /** |
||
672 | * Registers any properties returned by getEntityPageProperties() |
||
673 | * in $output. |
||
674 | * |
||
675 | * @param ParserOutput $output |
||
676 | */ |
||
677 | private function applyEntityPageProperties( ParserOutput $output ) { |
||
687 | |||
688 | /** |
||
689 | * Returns a map of properties about the entity, to be recorded in |
||
690 | * MediaWiki's page_props table. The idea is to allow efficient lookups |
||
691 | * of entities based on such properties. |
||
692 | * |
||
693 | * @return array A map from property names to property values. |
||
694 | */ |
||
695 | public function getEntityPageProperties() { |
||
698 | |||
699 | } |
||
700 |