Complex classes like Integrations 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 Integrations, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | abstract class Integrations extends Field implements PreviewableFieldInterface |
||
| 32 | { |
||
| 33 | use ModifyElementQueryTrait, |
||
| 34 | NormalizeValueTrait; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The Plugin's translation category |
||
| 38 | */ |
||
| 39 | const TRANSLATION_CATEGORY = ''; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The action path to preform field actions |
||
| 43 | */ |
||
| 44 | const ACTION_PREFORM_ACTION_PATH = ''; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The action path to preform field actions |
||
| 48 | */ |
||
| 49 | const ACTION_PREFORM_ITEM_ACTION_PATH = ''; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * The action path to associate an item |
||
| 53 | */ |
||
| 54 | const ACTION_ASSOCIATION_ITEM_PATH = ''; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * The action path to dissociate an item |
||
| 58 | */ |
||
| 59 | const ACTION_DISSOCIATION_ITEM_PATH = ''; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * The action path to create an integration object |
||
| 63 | */ |
||
| 64 | const ACTION_CREATE_ITEM_PATH = ''; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * The action event name |
||
| 68 | */ |
||
| 69 | const EVENT_REGISTER_ACTIONS = 'registerActions'; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * The action event name |
||
| 73 | */ |
||
| 74 | const EVENT_REGISTER_AVAILABLE_ACTIONS = 'registerAvailableActions'; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * The item action event name |
||
| 78 | */ |
||
| 79 | const EVENT_REGISTER_ITEM_ACTIONS = 'registerItemActions'; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * The item action event name |
||
| 83 | */ |
||
| 84 | const EVENT_REGISTER_AVAILABLE_ITEM_ACTIONS = 'registerAvailableItemActions'; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * The input template path |
||
| 88 | */ |
||
| 89 | const INPUT_TEMPLATE_PATH = ''; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * The input template path |
||
| 93 | */ |
||
| 94 | const INPUT_ITEM_TEMPLATE_PATH = '_inputItem'; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * The input template path |
||
| 98 | */ |
||
| 99 | const SETTINGS_TEMPLATE_PATH = ''; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var int|null |
||
| 103 | */ |
||
| 104 | public $min; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @var int|null |
||
| 108 | */ |
||
| 109 | public $max; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @var string |
||
| 113 | */ |
||
| 114 | public $viewUrl = ''; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @var string |
||
| 118 | */ |
||
| 119 | public $listUrl = ''; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @var IntegrationActionInterface[] |
||
| 123 | */ |
||
| 124 | public $selectedActions = []; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @var IntegrationItemActionInterface[] |
||
| 128 | */ |
||
| 129 | public $selectedItemActions = []; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @var string|null |
||
| 133 | */ |
||
| 134 | public $selectionLabel; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @inheritdoc |
||
| 138 | */ |
||
| 139 | protected $defaultAvailableActions = []; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @inheritdoc |
||
| 143 | */ |
||
| 144 | protected $defaultAvailableItemActions = []; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @return string |
||
| 148 | */ |
||
| 149 | abstract public static function recordClass(): string; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @return string |
||
| 153 | */ |
||
| 154 | abstract public function getObjectLabel(): string; |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @inheritdoc |
||
| 158 | */ |
||
| 159 | public static function hasContentColumn(): bool |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @return string |
||
| 166 | */ |
||
| 167 | public static function defaultSelectionLabel(): string |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @return string |
||
| 174 | */ |
||
| 175 | protected static function tableAlias(): string |
||
| 181 | |||
| 182 | /******************************************* |
||
| 183 | * VALIDATION |
||
| 184 | *******************************************/ |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @inheritdoc |
||
| 188 | */ |
||
| 189 | public function getElementValidationRules(): array |
||
| 212 | |||
| 213 | |||
| 214 | /******************************************* |
||
| 215 | * RULES |
||
| 216 | *******************************************/ |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @inheritdoc |
||
| 220 | */ |
||
| 221 | public function rules() |
||
| 242 | |||
| 243 | |||
| 244 | /******************************************* |
||
| 245 | * SEARCH |
||
| 246 | *******************************************/ |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @param IntegrationAssociationQuery $value |
||
| 250 | * @inheritdoc |
||
| 251 | */ |
||
| 252 | public function getSearchKeywords($value, ElementInterface $element): string |
||
| 263 | |||
| 264 | |||
| 265 | /******************************************* |
||
| 266 | * VIEWS |
||
| 267 | *******************************************/ |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @inheritdoc |
||
| 271 | * @param IntegrationAssociationQuery $value |
||
| 272 | * @throws \Twig_Error_Loader |
||
| 273 | * @throws \yii\base\Exception |
||
| 274 | */ |
||
| 275 | public function getInputHtml($value, ElementInterface $element = null): string |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @param IntegrationAssociationQuery $query |
||
| 289 | * @param ElementInterface|null $element |
||
| 290 | * @param bool $static |
||
| 291 | * @return array |
||
| 292 | * @throws \craft\errors\MissingComponentException |
||
| 293 | * @throws \yii\base\InvalidConfigException |
||
| 294 | */ |
||
| 295 | protected function inputHtmlVariables( |
||
| 336 | |||
| 337 | |||
| 338 | /******************************************* |
||
| 339 | * ACTIONS |
||
| 340 | *******************************************/ |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @return IntegrationActionInterface[] |
||
| 344 | * @throws \craft\errors\MissingComponentException |
||
| 345 | * @throws \yii\base\InvalidConfigException |
||
| 346 | */ |
||
| 347 | public function getAvailableActions(): array |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @param ElementInterface|null $element |
||
| 366 | * @return IntegrationActionInterface[] |
||
| 367 | * @throws \craft\errors\MissingComponentException |
||
| 368 | * @throws \yii\base\InvalidConfigException |
||
| 369 | */ |
||
| 370 | public function getActions(ElementInterface $element = null): array |
||
| 387 | |||
| 388 | /** |
||
| 389 | * @return IntegrationActionInterface[] |
||
| 390 | * @throws \craft\errors\MissingComponentException |
||
| 391 | * @throws \yii\base\InvalidConfigException |
||
| 392 | */ |
||
| 393 | public function getAvailableItemActions(): array |
||
| 409 | |||
| 410 | /** |
||
| 411 | * @param ElementInterface|null $element |
||
| 412 | * @return IntegrationItemActionInterface[] |
||
| 413 | * @throws \craft\errors\MissingComponentException |
||
| 414 | * @throws \yii\base\InvalidConfigException |
||
| 415 | */ |
||
| 416 | public function getItemActions(ElementInterface $element = null): array |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @param array $actions |
||
| 436 | * @param string $instance |
||
| 437 | * @return array |
||
| 438 | * @throws \craft\errors\MissingComponentException |
||
| 439 | * @throws \yii\base\InvalidConfigException |
||
| 440 | */ |
||
| 441 | protected function resolveActions(array $actions, string $instance) |
||
| 456 | |||
| 457 | /** |
||
| 458 | * @param ElementInterface|null $element |
||
| 459 | * @return array |
||
| 460 | * @throws \craft\errors\MissingComponentException |
||
| 461 | * @throws \yii\base\InvalidConfigException |
||
| 462 | */ |
||
| 463 | protected function getActionHtml(ElementInterface $element = null): array |
||
| 479 | |||
| 480 | /** |
||
| 481 | * @param ElementInterface|null $element |
||
| 482 | * @return array |
||
| 483 | * @throws \craft\errors\MissingComponentException |
||
| 484 | * @throws \yii\base\InvalidConfigException |
||
| 485 | */ |
||
| 486 | protected function getItemActionHtml(ElementInterface $element = null): array |
||
| 502 | |||
| 503 | /******************************************* |
||
| 504 | * EVENTS |
||
| 505 | *******************************************/ |
||
| 506 | |||
| 507 | /** |
||
| 508 | * @param ElementInterface $element |
||
| 509 | * @param bool $isNew |
||
| 510 | * @return bool|void |
||
| 511 | * @throws \Throwable |
||
| 512 | * @throws \yii\db\StaleObjectException |
||
| 513 | */ |
||
| 514 | public function afterElementSave(ElementInterface $element, bool $isNew) |
||
| 588 | |||
| 589 | |||
| 590 | /******************************************* |
||
| 591 | * INDEX |
||
| 592 | *******************************************/ |
||
| 593 | |||
| 594 | /** |
||
| 595 | * @inheritdoc |
||
| 596 | */ |
||
| 597 | public function getTableAttributeHtml($value, ElementInterface $element): string |
||
| 609 | |||
| 610 | |||
| 611 | /******************************************* |
||
| 612 | * SETTINGS |
||
| 613 | *******************************************/ |
||
| 614 | |||
| 615 | /** |
||
| 616 | * @inheritdoc |
||
| 617 | * @throws \Twig_Error_Loader |
||
| 618 | * @throws \yii\base\Exception |
||
| 619 | */ |
||
| 620 | public function getSettingsHtml() |
||
| 627 | |||
| 628 | /** |
||
| 629 | * @return array |
||
| 630 | * @throws \craft\errors\MissingComponentException |
||
| 631 | * @throws \yii\base\InvalidConfigException |
||
| 632 | */ |
||
| 633 | protected function settingsHtmlVariables(): array |
||
| 642 | |||
| 643 | /** |
||
| 644 | * @inheritdoc |
||
| 645 | */ |
||
| 646 | public function settingsAttributes(): array |
||
| 661 | } |
||
| 662 |