Complex classes like EntityHandler 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 EntityHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 41 | abstract class EntityHandler extends ContentHandler { |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Added to parser options for EntityContent. |
||
| 45 | * |
||
| 46 | * Bump the version when making incompatible changes |
||
| 47 | * to parser output. |
||
| 48 | */ |
||
| 49 | const PARSER_VERSION = 3; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var FieldDefinitions |
||
| 53 | */ |
||
| 54 | protected $fieldDefinitions; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var EntityContentDataCodec |
||
| 58 | */ |
||
| 59 | protected $contentCodec; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var EntityConstraintProvider |
||
| 63 | */ |
||
| 64 | protected $constraintProvider; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var ValidatorErrorLocalizer |
||
| 68 | */ |
||
| 69 | private $errorLocalizer; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var EntityIdParser |
||
| 73 | */ |
||
| 74 | private $entityIdParser; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var callable|null Callback to determine whether a serialized |
||
| 78 | * blob needs to be re-serialized on export. |
||
| 79 | */ |
||
| 80 | private $legacyExportFormatDetector; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @param string $modelId |
||
| 84 | * @param mixed $unused @todo Get rid of me |
||
| 85 | * @param EntityContentDataCodec $contentCodec |
||
| 86 | * @param EntityConstraintProvider $constraintProvider |
||
| 87 | * @param ValidatorErrorLocalizer $errorLocalizer |
||
| 88 | * @param EntityIdParser $entityIdParser |
||
| 89 | * @param FieldDefinitions $fieldDefinitions |
||
| 90 | * @param callable|null $legacyExportFormatDetector Callback to determine whether a serialized |
||
| 91 | * blob needs to be re-serialized on export. The callback must take two parameters, |
||
| 92 | * the blob an the serialization format. It must return true if re-serialization is needed. |
||
| 93 | * False positives are acceptable, false negatives are not. |
||
| 94 | * |
||
| 95 | */ |
||
| 96 | public function __construct( |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Returns the callback used to determine whether a serialized blob needs |
||
| 124 | * to be re-serialized on export (or null of re-serialization is disabled). |
||
| 125 | * |
||
| 126 | * @return callable|null |
||
| 127 | */ |
||
| 128 | public function getLegacyExportFormatDetector() { |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Handle the fact that a given page does not contain an Entity, even though it could. |
||
| 134 | * Per default, this behaves similarly to Article::showMissingArticle: it shows |
||
| 135 | * a message to the user. |
||
| 136 | * |
||
| 137 | * @see Article::showMissingArticle |
||
| 138 | * |
||
| 139 | * @param Title $title The title of the page that potentially could, but does not, |
||
| 140 | * contain an entity. |
||
| 141 | * @param IContextSource $context Context to use for reporting. In particular, output |
||
| 142 | * will be written to $context->getOutput(). |
||
| 143 | */ |
||
| 144 | public function showMissingEntity( Title $title, IContextSource $context ) { |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @see ContentHandler::getDiffEngineClass |
||
| 160 | * |
||
| 161 | * @return string |
||
| 162 | */ |
||
| 163 | protected function getDiffEngineClass() { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Get EntityValidators for on-save validation. |
||
| 169 | * |
||
| 170 | * @see getValidationErrorLocalizer() |
||
| 171 | * |
||
| 172 | * @param bool $forCreation Whether the entity is created (true) or updated (false). |
||
| 173 | * |
||
| 174 | * @return EntityValidator[] |
||
| 175 | */ |
||
| 176 | public function getOnSaveValidators( $forCreation, EntityId $entityId ) { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Error localizer for use together with getOnSaveValidators(). |
||
| 188 | * |
||
| 189 | * @see getOnSaveValidators() |
||
| 190 | * |
||
| 191 | * @return ValidatorErrorLocalizer |
||
| 192 | */ |
||
| 193 | public function getValidationErrorLocalizer() { |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @see ContentHandler::makeEmptyContent |
||
| 199 | * |
||
| 200 | * @return EntityContent |
||
| 201 | */ |
||
| 202 | public function makeEmptyContent() { |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Returns an empty Entity object of the type supported by this handler. |
||
| 208 | * This is intended to provide a baseline for diffing and related operations. |
||
| 209 | * |
||
| 210 | * @note The Entity returned here will not have an ID set, and is thus not |
||
| 211 | * suitable for use in an EntityContent object. |
||
| 212 | * |
||
| 213 | * @return EntityDocument |
||
| 214 | */ |
||
| 215 | abstract public function makeEmptyEntity(); |
||
| 216 | |||
| 217 | /** |
||
| 218 | * @param EntityRedirect $redirect Unused in this default implementation. |
||
| 219 | * |
||
| 220 | * @return EntityContent|null Either a new EntityContent representing the given EntityRedirect, |
||
| 221 | * or null if the entity type does not support redirects. Always null in this default |
||
| 222 | * implementation. |
||
| 223 | */ |
||
| 224 | public function makeEntityRedirectContent( EntityRedirect $redirect ) { |
||
| 227 | |||
| 228 | /** |
||
| 229 | * None of the Entity content models support categories. |
||
| 230 | * |
||
| 231 | * @return bool Always false. |
||
| 232 | */ |
||
| 233 | public function supportsCategories() { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @see ContentHandler::getAutosummary |
||
| 239 | * |
||
| 240 | * We never want to use MediaWiki's autosummaries, used e.g. for new page creation. Override this |
||
| 241 | * to make sure they never overwrite our autosummaries (which look like the automatic summary |
||
| 242 | * prefixes with a section title, and so could be overwritten). |
||
| 243 | * |
||
| 244 | * @param Content|null $oldContent |
||
| 245 | * @param Content|null $newContent |
||
| 246 | * @param int $flags |
||
| 247 | * |
||
| 248 | * @return string Empty string |
||
| 249 | */ |
||
| 250 | public function getAutosummary( |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @see ContentHandler::makeRedirectContent |
||
| 260 | * |
||
| 261 | * @warning Always throws an MWException, since an EntityRedirects needs to know it's own |
||
| 262 | * ID in addition to the target ID. We have no way to guess that in makeRedirectContent(). |
||
| 263 | * Use makeEntityRedirectContent() instead. |
||
| 264 | * |
||
| 265 | * @see makeEntityRedirectContent() |
||
| 266 | * |
||
| 267 | * @param Title $title |
||
| 268 | * @param string $text |
||
| 269 | * |
||
| 270 | * @throws MWException Always. |
||
| 271 | * @return EntityContent|null |
||
| 272 | */ |
||
| 273 | public function makeRedirectContent( Title $title, $text = '' ) { |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @see ContentHandler::exportTransform |
||
| 280 | * |
||
| 281 | * @param string $blob |
||
| 282 | * @param string|null $format |
||
| 283 | * |
||
| 284 | * @return string |
||
| 285 | */ |
||
| 286 | public function exportTransform( $blob, $format = null ) { |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @param EntityHolder $entityHolder |
||
| 305 | * |
||
| 306 | * @return EntityContent |
||
| 307 | */ |
||
| 308 | public function makeEntityContent( EntityHolder $entityHolder ) { |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @param EntityHolder|null $entityHolder |
||
| 314 | * |
||
| 315 | * @return EntityContent |
||
| 316 | */ |
||
| 317 | abstract protected function newEntityContent( EntityHolder $entityHolder = null ); |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Parses the given ID string into an EntityId for the type of entity |
||
| 321 | * supported by this EntityHandler. If the string is not a valid |
||
| 322 | * serialization of the correct type of entity ID, an exception is thrown. |
||
| 323 | * |
||
| 324 | * @param string $id String representation the entity ID |
||
| 325 | * |
||
| 326 | * @return EntityId |
||
| 327 | * @throws InvalidArgumentException |
||
| 328 | */ |
||
| 329 | abstract public function makeEntityId( $id ); |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @return string |
||
| 333 | */ |
||
| 334 | public function getDefaultFormat() { |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @param Content $content |
||
| 340 | * @param string|null $format |
||
| 341 | * |
||
| 342 | * @throws InvalidArgumentException |
||
| 343 | * @throws MWContentSerializationException |
||
| 344 | * @return string |
||
| 345 | */ |
||
| 346 | public function serializeContent( Content $content, $format = null ) { |
||
| 361 | |||
| 362 | /** |
||
| 363 | * @see ContentHandler::unserializeContent |
||
| 364 | * |
||
| 365 | * @param string $blob |
||
| 366 | * @param string|null $format |
||
| 367 | * |
||
| 368 | * @throws MWContentSerializationException |
||
| 369 | * @return EntityContent |
||
| 370 | */ |
||
| 371 | public function unserializeContent( $blob, $format = null ) { |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Returns the ID of the entity contained by the page of the given title. |
||
| 391 | * |
||
| 392 | * @warning This should not really be needed and may just go away! |
||
| 393 | * |
||
| 394 | * @param Title $target |
||
| 395 | * |
||
| 396 | * @throws EntityIdParsingException |
||
| 397 | * @return EntityId |
||
| 398 | */ |
||
| 399 | public function getIdForTitle( Title $target ) { |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Returns the appropriate page Title for the given EntityId. |
||
| 405 | * |
||
| 406 | * @warning This should not really be needed and may just go away! |
||
| 407 | * |
||
| 408 | * @see EntityTitleStoreLookup::getTitleForId |
||
| 409 | * |
||
| 410 | * @param EntityId $id |
||
| 411 | * |
||
| 412 | * @throws InvalidArgumentException if $id refers to an entity of the wrong type. |
||
| 413 | * @return Title |
||
| 414 | */ |
||
| 415 | public function getTitleForId( EntityId $id ) { |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Returns the appropriate page Titles for the given EntityIds |
||
| 426 | * |
||
| 427 | * @param EntityId[] $ids |
||
| 428 | * @return Title[] Array of Title objects indexed by the entity id serializations |
||
| 429 | */ |
||
| 430 | public function getTitlesForIds( array $ids ) { |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Returns the namespace that is to be used for this kind of entities. |
||
| 447 | * |
||
| 448 | * @return int |
||
| 449 | */ |
||
| 450 | final public function getEntityNamespace() { |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Returns the slot that is to be used for this kind of entities. |
||
| 465 | * |
||
| 466 | * @return string the role name of the slot |
||
| 467 | */ |
||
| 468 | final public function getEntitySlotRole() { |
||
| 473 | |||
| 474 | /** |
||
| 475 | * @see ContentHandler::canBeUsedOn |
||
| 476 | * |
||
| 477 | * This implementation returns true if and only if the given title's namespace |
||
| 478 | * is the same as the one returned by $this->getEntityNamespace(). |
||
| 479 | * |
||
| 480 | * @param Title $title |
||
| 481 | * |
||
| 482 | * @return bool true if $title represents a page in the appropriate entity namespace. |
||
| 483 | */ |
||
| 484 | public function canBeUsedOn( Title $title ) { |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Returns true to indicate that the parser cache can be used for data items. |
||
| 495 | * |
||
| 496 | * @note The html representation of entities depends on the user language, so |
||
| 497 | * EntityContent::getParserOutput needs to make sure ParserOutput::recordOption( 'userlang' ) |
||
| 498 | * is called to split the cache by user language. |
||
| 499 | * |
||
| 500 | * @see ContentHandler::isParserCacheSupported |
||
| 501 | * |
||
| 502 | * @return bool Always true in this default implementation. |
||
| 503 | */ |
||
| 504 | public function isParserCacheSupported() { |
||
| 507 | |||
| 508 | /** |
||
| 509 | * @see ContentHandler::getPageViewLanguage |
||
| 510 | * |
||
| 511 | * This implementation returns the user language, because entities get rendered in |
||
| 512 | * the user's language. The PageContentLanguage hook is bypassed. |
||
| 513 | * |
||
| 514 | * @param Title $title the page to determine the language for. |
||
| 515 | * @param Content|null $content the page's content, if you have it handy, to avoid reloading it. |
||
| 516 | * |
||
| 517 | * @return Language The page's language |
||
| 518 | */ |
||
| 519 | public function getPageViewLanguage( Title $title, Content $content = null ) { |
||
| 524 | |||
| 525 | /** |
||
| 526 | * @see ContentHandler::getPageLanguage |
||
| 527 | * |
||
| 528 | * This implementation unconditionally returns the wiki's content language. |
||
| 529 | * The PageContentLanguage hook is bypassed. |
||
| 530 | * |
||
| 531 | * @note Ideally, this would return 'mul' to indicate multilingual content. But MediaWiki |
||
| 532 | * currently doesn't support that. |
||
| 533 | * |
||
| 534 | * @note in several places in mediawiki, most importantly the parser cache, getPageLanguage |
||
| 535 | * is used in places where getPageViewLanguage would be more appropriate. |
||
| 536 | * |
||
| 537 | * @param Title $title the page to determine the language for. |
||
| 538 | * @param Content|null $content the page's content, if you have it handy, to avoid reloading it. |
||
| 539 | * |
||
| 540 | * @return Language The page's language |
||
| 541 | */ |
||
| 542 | public function getPageLanguage( Title $title, Content $content = null ) { |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Returns the name of the special page responsible for creating a page |
||
| 548 | * for this type of entity content. |
||
| 549 | * Returns null if there is no such special page. |
||
| 550 | * |
||
| 551 | * @return string|null Always null in this default implementation. |
||
| 552 | */ |
||
| 553 | public function getSpecialPageForCreation() { |
||
| 556 | |||
| 557 | /** |
||
| 558 | * @see ContentHandler::getUndoContent |
||
| 559 | * |
||
| 560 | * @param Revision|Content $latest The current text |
||
| 561 | * @param Revision|Content $newer The revision to undo |
||
| 562 | * @param Revision|Content $older Must be an earlier revision than $newer |
||
| 563 | * @param bool $undoIsLatest Set to true if $newer is from the current revision (since 1.32) |
||
| 564 | * |
||
| 565 | * @return EntityContent|bool Content on success, false on failure |
||
| 566 | */ |
||
| 567 | public function getUndoContent( $latest, $newer, $older, $undoIsLatest = false ) { |
||
| 607 | |||
| 608 | /** |
||
| 609 | * Returns the entity type ID for the kind of entity managed by this EntityContent implementation. |
||
| 610 | * |
||
| 611 | * @return string |
||
| 612 | */ |
||
| 613 | abstract public function getEntityType(); |
||
| 614 | |||
| 615 | /** |
||
| 616 | * Whether IDs can automatically be assigned to entities |
||
| 617 | * of the kind supported by this EntityHandler. |
||
| 618 | * |
||
| 619 | * @return bool |
||
| 620 | */ |
||
| 621 | public function allowAutomaticIds() { |
||
| 624 | |||
| 625 | /** |
||
| 626 | * Whether the given custom ID is valid for creating a new entity |
||
| 627 | * of the kind supported by this EntityHandler. |
||
| 628 | * |
||
| 629 | * Implementations are not required to check if an entity with the given ID already exists. |
||
| 630 | * If this method returns true, this means that an entity with the given ID could be |
||
| 631 | * created (or already existed) at the time the method was called. There is no guarantee |
||
| 632 | * that this continues to be true after the method call returned. Callers must be careful |
||
| 633 | * to handle race conditions. |
||
| 634 | * |
||
| 635 | * @note For entity types that cannot be created with custom IDs (that is, |
||
| 636 | * entity types that are defined to use automatic IDs), this should always |
||
| 637 | * return false. |
||
| 638 | * |
||
| 639 | * @see EntityStore::canCreateWithCustomId() |
||
| 640 | * |
||
| 641 | * @param EntityId $id |
||
| 642 | * |
||
| 643 | * @return bool |
||
| 644 | */ |
||
| 645 | public function canCreateWithCustomId( EntityId $id ) { |
||
| 648 | |||
| 649 | /** |
||
| 650 | * @param SearchEngine $engine |
||
| 651 | * @return \SearchIndexField[] List of fields this content handler can provide. |
||
| 652 | */ |
||
| 653 | public function getFieldsForSearchIndex( SearchEngine $engine ) { |
||
| 665 | |||
| 666 | /** |
||
| 667 | * @param WikiPage $page |
||
| 668 | * @param ParserOutput $output |
||
| 669 | * @param SearchEngine $engine |
||
| 670 | * |
||
| 671 | * @return array Wikibase fields data, map of name=>value for fields |
||
| 672 | */ |
||
| 673 | public function getDataForSearchIndex( |
||
| 692 | |||
| 693 | /** |
||
| 694 | * Produce page output suitable for indexing. |
||
| 695 | * Does not include HTML. |
||
| 696 | * |
||
| 697 | * @param WikiPage $page |
||
| 698 | * @param ParserCache|null $cache |
||
| 699 | * @return bool|ParserOutput|null |
||
| 700 | */ |
||
| 701 | public function getParserOutputForIndexing( WikiPage $page, ParserCache $cache = null ) { |
||
| 720 | |||
| 721 | } |
||
| 722 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: