Complex classes like ElementTrait 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 ElementTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | trait ElementTrait |
||
| 21 | { |
||
| 22 | use BaseTrait { |
||
| 23 | settings as baseSettings; |
||
| 24 | attributes as baseAttributes; |
||
| 25 | } |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var int |
||
| 29 | */ |
||
| 30 | private $elementId; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var Element|null |
||
| 34 | */ |
||
| 35 | private $element; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var string|string[]|null The source keys that this field can relate elements from (used if |
||
| 39 | * [[allowMultipleSources]] is set to true) |
||
| 40 | */ |
||
| 41 | public $sources = '*'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string|null The source key that this field can relate elements from (used if |
||
| 45 | * [[allowMultipleSources]] is set to false) |
||
| 46 | */ |
||
| 47 | public $source; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var int|null The site that this field should relate elements from |
||
| 51 | */ |
||
| 52 | public $targetSiteId; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var bool Whether to allow multiple source selection in the settings |
||
| 56 | */ |
||
| 57 | public $allowMultipleSources = true; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var string|null The label that should be used on the selection input |
||
| 61 | */ |
||
| 62 | public $selectionLabel; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var int Whether each site should get its own unique set of relations |
||
| 66 | */ |
||
| 67 | public $localizeRelations = false; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var bool Whether to allow the “Large Thumbnails” view mode |
||
| 71 | */ |
||
| 72 | protected $allowLargeThumbsView = false; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var string|null The view mode |
||
| 76 | */ |
||
| 77 | public $viewMode; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var string|null The JS class that should be initialized for the input |
||
| 81 | */ |
||
| 82 | protected $inputJsClass; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Returns the element class associated with this link type. |
||
| 86 | * |
||
| 87 | * @return string |
||
| 88 | */ |
||
| 89 | abstract protected static function elementType(): string; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @return string |
||
| 93 | */ |
||
| 94 | abstract public function getElementText(): string; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @return string |
||
| 98 | */ |
||
| 99 | abstract public function getElementUrl(): string; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @inheritdoc |
||
| 103 | */ |
||
| 104 | public function settings(): array |
||
| 105 | { |
||
| 106 | return array_merge( |
||
| 107 | $this->baseSettings(), |
||
|
|
|||
| 108 | [ |
||
| 109 | 'sources', |
||
| 110 | 'targetSiteId', |
||
| 111 | 'viewMode', |
||
| 112 | 'selectionLabel', |
||
| 113 | 'localizeRelations' |
||
| 114 | ] |
||
| 115 | ); |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @inheritdoc |
||
| 120 | */ |
||
| 121 | public function attributes() |
||
| 122 | { |
||
| 123 | return array_merge( |
||
| 124 | $this->baseAttributes(), |
||
| 125 | [ |
||
| 126 | 'elementId' |
||
| 127 | ] |
||
| 128 | ); |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @inheritdoc |
||
| 133 | */ |
||
| 134 | public function getUrl(): string |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @inheritdoc |
||
| 141 | */ |
||
| 142 | public function getText() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @return int|null |
||
| 152 | */ |
||
| 153 | public function getElementId() |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @param $elementId |
||
| 160 | */ |
||
| 161 | public function setElementId($elementId) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @return ElementInterface|Element|null |
||
| 178 | */ |
||
| 179 | protected function getElement() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @return ElementInterface|null |
||
| 190 | */ |
||
| 191 | protected function lookupElement() |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Normalizes the available sources into select input options. |
||
| 206 | * |
||
| 207 | * @return array |
||
| 208 | */ |
||
| 209 | public function getSourceOptions(): array |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Returns the sources that should be available to choose from within the field's settings |
||
| 233 | * |
||
| 234 | * @return array |
||
| 235 | */ |
||
| 236 | protected function availableSources(): array |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Returns the HTML for the View Mode setting. |
||
| 243 | * |
||
| 244 | * @return string|null |
||
| 245 | * @throws \yii\base\Exception |
||
| 246 | */ |
||
| 247 | public function getViewModeFieldHtml() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Returns the field’s supported view modes. |
||
| 279 | * |
||
| 280 | * @return array |
||
| 281 | */ |
||
| 282 | protected function supportedViewModes(): array |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Returns the site ID that target elements should have. |
||
| 297 | * |
||
| 298 | * @param ElementInterface|null $element |
||
| 299 | * @return int |
||
| 300 | * @throws \craft\errors\SiteNotFoundException |
||
| 301 | */ |
||
| 302 | protected function targetSiteId(ElementInterface $element = null): int |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Returns the default [[selectionLabel]] value. |
||
| 322 | * |
||
| 323 | * @return string The default selection label |
||
| 324 | */ |
||
| 325 | public static function defaultSelectionLabel(): string |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Returns an array of variables that should be passed to the input template. |
||
| 332 | * |
||
| 333 | * @param Link $field |
||
| 334 | * @param ElementInterface|null $element |
||
| 335 | * @return array |
||
| 336 | * @throws \craft\errors\SiteNotFoundException |
||
| 337 | */ |
||
| 338 | protected function inputTemplateVariables( |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Returns any additional criteria parameters limiting which elements the field should be able to select. |
||
| 368 | * |
||
| 369 | * @return array |
||
| 370 | */ |
||
| 371 | protected function inputSelectionCriteria(): array |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Returns the field’s current view mode. |
||
| 378 | * |
||
| 379 | * @return string |
||
| 380 | */ |
||
| 381 | protected function viewMode(): string |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Returns an array of the source keys the field should be able to select elements from. |
||
| 395 | * |
||
| 396 | * @return array|string |
||
| 397 | */ |
||
| 398 | protected function inputSources() |
||
| 408 | } |
||
| 409 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.