Complex classes like User 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 User, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | class User extends Field implements PreviewableFieldInterface, EagerLoadingFieldInterface |
||
| 38 | { |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @inheritdoc |
||
| 42 | */ |
||
| 43 | public static function displayName(): string |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @inheritdoc |
||
| 50 | */ |
||
| 51 | public static function hasContentColumn(): bool |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @inheritdoc |
||
| 58 | */ |
||
| 59 | public static function supportedTranslationMethods(): array |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @inheritdoc |
||
| 69 | */ |
||
| 70 | protected static function elementType(): string |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Returns the default [[selectionLabel]] value. |
||
| 77 | * |
||
| 78 | * @return string The default selection label |
||
| 79 | */ |
||
| 80 | public static function defaultSelectionLabel(): string |
||
| 84 | |||
| 85 | // Properties |
||
| 86 | // ========================================================================= |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var string|string[]|null The source keys that this field can |
||
| 90 | * relate elements from (used if [[allowMultipleSources]] is set to true) |
||
| 91 | */ |
||
| 92 | public $sources = '*'; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var string|null The source key that this field can |
||
| 96 | * relate elements from (used if [[allowMultipleSources]] is set to false) |
||
| 97 | */ |
||
| 98 | public $source; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @var int|null The site that this field should relate elements from |
||
| 102 | */ |
||
| 103 | public $targetSiteId; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @var string|null The view mode |
||
| 107 | */ |
||
| 108 | public $viewMode; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @var int|null The maximum number of relations this field can have (used if [[allowLimit]] is set to true) |
||
| 112 | */ |
||
| 113 | public $limit; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @var string|null The label that should be used on the selection input |
||
| 117 | */ |
||
| 118 | public $selectionLabel; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @var int Whether each site should get its own unique set of relations |
||
| 122 | */ |
||
| 123 | public $localizeRelations = false; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @var bool Whether to allow multiple source selection in the settings |
||
| 127 | */ |
||
| 128 | public $allowMultipleSources = true; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @var bool Whether to allow the Limit setting |
||
| 132 | */ |
||
| 133 | public $allowLimit = true; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @var bool Whether to allow the “Large Thumbnails” view mode |
||
| 137 | */ |
||
| 138 | protected $allowLargeThumbsView = false; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @var string Temlpate to use for settings rendering |
||
| 142 | */ |
||
| 143 | protected $settingsTemplate = '_components/fieldtypes/elementfieldsettings'; |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @var string Template to use for field rendering |
||
| 147 | */ |
||
| 148 | protected $inputTemplate = '_includes/forms/elementSelect'; |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @var string|null The JS class that should be initialized for the input |
||
| 152 | */ |
||
| 153 | protected $inputJsClass; |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @var bool Whether the elements have a custom sort order |
||
| 157 | */ |
||
| 158 | protected $sortable = true; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @var bool Whether existing relations should be made translatable after the field is saved |
||
| 162 | */ |
||
| 163 | private $makeExistingRelationsTranslatable = false; |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @inheritdoc |
||
| 167 | */ |
||
| 168 | public function init() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @inheritdoc |
||
| 187 | */ |
||
| 188 | public function settingsAttributes(): array |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @inheritdoc |
||
| 204 | */ |
||
| 205 | public function getSettingsHtml() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @inheritdoc |
||
| 214 | */ |
||
| 215 | public function getElementValidationRules(): array |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @inheritdoc |
||
| 241 | */ |
||
| 242 | public function normalizeValue($value, ElementInterface $element = null) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @inheritdoc |
||
| 289 | */ |
||
| 290 | public function modifyElementsQuery(ElementQueryInterface $query, $value) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @inheritdoc |
||
| 312 | */ |
||
| 313 | public function getIsTranslatable(ElementInterface $element = null): bool |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @inheritdoc |
||
| 320 | */ |
||
| 321 | public function getInputHtml($value, ElementInterface $element = null): string |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @inheritdoc |
||
| 336 | */ |
||
| 337 | public function getSearchKeywords($value, ElementInterface $element): string |
||
| 348 | |||
| 349 | /** |
||
| 350 | * @inheritdoc |
||
| 351 | */ |
||
| 352 | public function getStaticHtml($value, ElementInterface $element): string |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @inheritdoc |
||
| 377 | */ |
||
| 378 | public function getTableAttributeHtml($value, ElementInterface $element): string |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @inheritdoc |
||
| 397 | */ |
||
| 398 | public function getEagerLoadingMap(array $sourceElements) |
||
| 439 | |||
| 440 | // Events |
||
| 441 | // ------------------------------------------------------------------------- |
||
| 442 | |||
| 443 | /** |
||
| 444 | * @inheritdoc |
||
| 445 | */ |
||
| 446 | public function beforeSave(bool $isNew): bool |
||
| 461 | |||
| 462 | /** |
||
| 463 | * @inheritdoc |
||
| 464 | */ |
||
| 465 | public function afterSave(bool $isNew) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * @inheritdoc |
||
| 478 | */ |
||
| 479 | public function beforeElementSave(ElementInterface $element, bool $isNew): bool |
||
| 502 | |||
| 503 | /** |
||
| 504 | * @inheritdoc |
||
| 505 | */ |
||
| 506 | public function afterElementSave(ElementInterface $element, bool $isNew) |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Normalizes the available sources into select input options. |
||
| 526 | * |
||
| 527 | * @return array |
||
| 528 | */ |
||
| 529 | public function getSourceOptions(): array |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Returns the HTML for the Target Site setting. |
||
| 553 | * |
||
| 554 | * @return string|null |
||
| 555 | */ |
||
| 556 | public function getTargetSiteFieldHtml() |
||
| 597 | |||
| 598 | /** |
||
| 599 | * Returns the HTML for the View Mode setting. |
||
| 600 | * |
||
| 601 | * @return string|null |
||
| 602 | */ |
||
| 603 | public function getViewModeFieldHtml() |
||
| 628 | |||
| 629 | // Protected Methods |
||
| 630 | // ========================================================================= |
||
| 631 | |||
| 632 | /** |
||
| 633 | * Returns an array of variables that should be passed to the input template. |
||
| 634 | * |
||
| 635 | * @param ElementQueryInterface|array|null $value |
||
| 636 | * @param ElementInterface|null $element |
||
| 637 | * |
||
| 638 | * @return array |
||
| 639 | */ |
||
| 640 | protected function inputTemplateVariables($value = null, ElementInterface $element = null): array |
||
| 675 | |||
| 676 | /** |
||
| 677 | * Returns an array of the source keys the field should be able to select elements from. |
||
| 678 | * |
||
| 679 | * @return array|string |
||
| 680 | */ |
||
| 681 | protected function inputSources() |
||
| 691 | |||
| 692 | /** |
||
| 693 | * Returns any additional criteria parameters limiting which elements the field should be able to select. |
||
| 694 | * |
||
| 695 | * @return array |
||
| 696 | */ |
||
| 697 | protected function inputSelectionCriteria(): array |
||
| 701 | |||
| 702 | /** |
||
| 703 | * Returns the site ID that target elements should have. |
||
| 704 | * |
||
| 705 | * @param ElementInterface|null $element |
||
| 706 | * |
||
| 707 | * @return int |
||
| 708 | */ |
||
| 709 | protected function targetSiteId(ElementInterface $element = null): int |
||
| 724 | |||
| 725 | /** |
||
| 726 | * Returns the field’s supported view modes. |
||
| 727 | * |
||
| 728 | * @return array |
||
| 729 | */ |
||
| 730 | protected function supportedViewModes(): array |
||
| 742 | |||
| 743 | /** |
||
| 744 | * Returns the field’s current view mode. |
||
| 745 | * |
||
| 746 | * @return string |
||
| 747 | */ |
||
| 748 | protected function viewMode(): string |
||
| 759 | |||
| 760 | /** |
||
| 761 | * Returns the sources that should be available to choose from within the field's settings |
||
| 762 | * |
||
| 763 | * @return array |
||
| 764 | */ |
||
| 765 | protected function availableSources(): array |
||
| 769 | } |
||
| 770 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: