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 |
||
32 | abstract class Integrations extends Field |
||
33 | { |
||
34 | use ModifyElementQueryTrait, |
||
35 | NormalizeValueTrait; |
||
36 | |||
37 | /** |
||
38 | * The Plugin's translation category |
||
39 | */ |
||
40 | const TRANSLATION_CATEGORY = ''; |
||
41 | |||
42 | /** |
||
43 | * The action path to preform field actions |
||
44 | */ |
||
45 | const ACTION_PREFORM_ACTION_PATH = ''; |
||
46 | |||
47 | /** |
||
48 | * The action path to preform field actions |
||
49 | */ |
||
50 | const ACTION_PREFORM_ITEM_ACTION_PATH = ''; |
||
51 | |||
52 | /** |
||
53 | * The action path to associate an item |
||
54 | */ |
||
55 | const ACTION_ASSOCIATION_ITEM_PATH = ''; |
||
56 | |||
57 | /** |
||
58 | * The action path to dissociate an item |
||
59 | */ |
||
60 | const ACTION_DISSOCIATION_ITEM_PATH = ''; |
||
61 | |||
62 | /** |
||
63 | * The action path to create an integration object |
||
64 | */ |
||
65 | const ACTION_CREATE_ITEM_PATH = ''; |
||
66 | |||
67 | /** |
||
68 | * The action event name |
||
69 | */ |
||
70 | const EVENT_REGISTER_ACTIONS = 'registerActions'; |
||
71 | |||
72 | /** |
||
73 | * The action event name |
||
74 | */ |
||
75 | const EVENT_REGISTER_AVAILABLE_ACTIONS = 'registerAvailableActions'; |
||
76 | |||
77 | /** |
||
78 | * The item action event name |
||
79 | */ |
||
80 | const EVENT_REGISTER_ITEM_ACTIONS = 'registerItemActions'; |
||
81 | |||
82 | /** |
||
83 | * The item action event name |
||
84 | */ |
||
85 | const EVENT_REGISTER_AVAILABLE_ITEM_ACTIONS = 'registerAvailableItemActions'; |
||
86 | |||
87 | /** |
||
88 | * The input template path |
||
89 | */ |
||
90 | const INPUT_TEMPLATE_PATH = ''; |
||
91 | |||
92 | /** |
||
93 | * The input template path |
||
94 | */ |
||
95 | const INPUT_ITEM_TEMPLATE_PATH = '_inputItem'; |
||
96 | |||
97 | /** |
||
98 | * The input template path |
||
99 | */ |
||
100 | const SETTINGS_TEMPLATE_PATH = ''; |
||
101 | |||
102 | /** |
||
103 | * @var string |
||
104 | */ |
||
105 | public $object; |
||
106 | |||
107 | /** |
||
108 | * @var int|null |
||
109 | */ |
||
110 | public $min; |
||
111 | |||
112 | /** |
||
113 | * @var int|null |
||
114 | */ |
||
115 | public $max; |
||
116 | |||
117 | /** |
||
118 | * @var string |
||
119 | */ |
||
120 | public $viewUrl = ''; |
||
121 | |||
122 | /** |
||
123 | * @var string |
||
124 | */ |
||
125 | public $listUrl = ''; |
||
126 | |||
127 | /** |
||
128 | * @var IntegrationActionInterface[] |
||
129 | */ |
||
130 | public $selectedActions = []; |
||
131 | |||
132 | /** |
||
133 | * @var IntegrationItemActionInterface[] |
||
134 | */ |
||
135 | public $selectedItemActions = []; |
||
136 | |||
137 | /** |
||
138 | * @var string|null |
||
139 | */ |
||
140 | public $selectionLabel; |
||
141 | |||
142 | /** |
||
143 | * @inheritdoc |
||
144 | */ |
||
145 | protected $defaultAvailableActions = []; |
||
146 | |||
147 | /** |
||
148 | * @inheritdoc |
||
149 | */ |
||
150 | protected $defaultAvailableItemActions = []; |
||
151 | |||
152 | /** |
||
153 | * @return string |
||
154 | */ |
||
155 | abstract public static function recordClass(): string; |
||
156 | |||
157 | /** |
||
158 | * @inheritdoc |
||
159 | */ |
||
160 | public static function hasContentColumn(): bool |
||
161 | { |
||
162 | return false; |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * @return string |
||
167 | */ |
||
168 | public static function defaultSelectionLabel(): string |
||
169 | { |
||
170 | return Craft::t(static::TRANSLATION_CATEGORY, 'Add an Object'); |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * @return string |
||
175 | */ |
||
176 | protected static function tableAlias(): string |
||
177 | { |
||
178 | /** @var ActiveRecord $recordClass */ |
||
179 | $recordClass = static::recordClass(); |
||
180 | return $recordClass::tableAlias(); |
||
181 | } |
||
182 | |||
183 | /******************************************* |
||
184 | * OBJECT |
||
185 | *******************************************/ |
||
186 | |||
187 | /** |
||
188 | * @return string |
||
189 | */ |
||
190 | public function getObjectLabel(): string |
||
191 | { |
||
192 | return StringHelper::titleize($this->object); |
||
193 | } |
||
194 | |||
195 | /******************************************* |
||
196 | * VALIDATION |
||
197 | *******************************************/ |
||
198 | |||
199 | /** |
||
200 | * @inheritdoc |
||
201 | */ |
||
202 | public function getElementValidationRules(): array |
||
203 | { |
||
204 | return [ |
||
205 | [ |
||
206 | MinMaxValidator::class, |
||
207 | 'min' => $this->min ? (int)$this->min : null, |
||
208 | 'max' => $this->max ? (int)$this->max : null, |
||
209 | 'tooFew' => Craft::t( |
||
210 | static::TRANSLATION_CATEGORY, |
||
211 | '{attribute} should contain at least ' . |
||
212 | '{min, number} ' . |
||
213 | '{min, plural, one{association} other{associations}}.' |
||
214 | ), |
||
215 | 'tooMany' => Craft::t( |
||
216 | static::TRANSLATION_CATEGORY, |
||
217 | '{attribute} should contain at most ' . |
||
218 | '{max, number} ' . |
||
219 | '{max, plural, one{association} other{associations}}.' |
||
220 | ), |
||
221 | 'skipOnEmpty' => false |
||
222 | ] |
||
223 | ]; |
||
224 | } |
||
225 | |||
226 | /******************************************* |
||
227 | * RULES |
||
228 | *******************************************/ |
||
229 | |||
230 | /** |
||
231 | * @inheritdoc |
||
232 | */ |
||
233 | public function rules() |
||
260 | |||
261 | |||
262 | /******************************************* |
||
263 | * SEARCH |
||
264 | *******************************************/ |
||
265 | |||
266 | /** |
||
267 | * @param IntegrationAssociationQuery $value |
||
268 | * @inheritdoc |
||
269 | */ |
||
270 | public function getSearchKeywords($value, ElementInterface $element): string |
||
281 | |||
282 | |||
283 | /******************************************* |
||
284 | * VIEWS |
||
285 | *******************************************/ |
||
286 | |||
287 | /** |
||
288 | * @inheritdoc |
||
289 | * @param IntegrationAssociationQuery $value |
||
290 | * @throws \Twig_Error_Loader |
||
291 | * @throws \yii\base\Exception |
||
292 | */ |
||
293 | public function getInputHtml($value, ElementInterface $element = null): string |
||
304 | |||
305 | /** |
||
306 | * @param IntegrationAssociationQuery $query |
||
307 | * @param ElementInterface|null $element |
||
308 | * @param bool $static |
||
309 | * @return array |
||
310 | * @throws \craft\errors\MissingComponentException |
||
311 | * @throws \yii\base\InvalidConfigException |
||
312 | */ |
||
313 | protected function inputHtmlVariables( |
||
355 | |||
356 | |||
357 | /******************************************* |
||
358 | * ACTIONS |
||
359 | *******************************************/ |
||
360 | |||
361 | /** |
||
362 | * @return IntegrationActionInterface[] |
||
363 | * @throws \craft\errors\MissingComponentException |
||
364 | * @throws \yii\base\InvalidConfigException |
||
365 | */ |
||
366 | public function getAvailableActions(): array |
||
382 | |||
383 | /** |
||
384 | * @param ElementInterface|null $element |
||
385 | * @return IntegrationActionInterface[] |
||
386 | * @throws \craft\errors\MissingComponentException |
||
387 | * @throws \yii\base\InvalidConfigException |
||
388 | */ |
||
389 | public function getActions(ElementInterface $element = null): array |
||
406 | |||
407 | /** |
||
408 | * @return IntegrationActionInterface[] |
||
409 | * @throws \craft\errors\MissingComponentException |
||
410 | * @throws \yii\base\InvalidConfigException |
||
411 | */ |
||
412 | public function getAvailableItemActions(): array |
||
428 | |||
429 | /** |
||
430 | * @param ElementInterface|null $element |
||
431 | * @return IntegrationItemActionInterface[] |
||
432 | * @throws \craft\errors\MissingComponentException |
||
433 | * @throws \yii\base\InvalidConfigException |
||
434 | */ |
||
435 | public function getItemActions(ElementInterface $element = null): array |
||
452 | |||
453 | /** |
||
454 | * @param array $actions |
||
455 | * @param string $instance |
||
456 | * @return array |
||
457 | * @throws \craft\errors\MissingComponentException |
||
458 | * @throws \yii\base\InvalidConfigException |
||
459 | */ |
||
460 | protected function resolveActions(array $actions, string $instance) |
||
475 | |||
476 | /** |
||
477 | * @param ElementInterface|null $element |
||
478 | * @return array |
||
479 | * @throws \craft\errors\MissingComponentException |
||
480 | * @throws \yii\base\InvalidConfigException |
||
481 | */ |
||
482 | protected function getActionHtml(ElementInterface $element = null): array |
||
498 | |||
499 | /** |
||
500 | * @param ElementInterface|null $element |
||
501 | * @return array |
||
502 | * @throws \craft\errors\MissingComponentException |
||
503 | * @throws \yii\base\InvalidConfigException |
||
504 | */ |
||
505 | protected function getItemActionHtml(ElementInterface $element = null): array |
||
521 | |||
522 | /******************************************* |
||
523 | * EVENTS |
||
524 | *******************************************/ |
||
525 | |||
526 | /** |
||
527 | * @param ElementInterface $element |
||
528 | * @param bool $isNew |
||
529 | * @return bool|void |
||
530 | * @throws \Throwable |
||
531 | * @throws \yii\db\StaleObjectException |
||
532 | */ |
||
533 | public function afterElementSave(ElementInterface $element, bool $isNew) |
||
607 | |||
608 | |||
609 | /******************************************* |
||
610 | * INDEX |
||
611 | *******************************************/ |
||
612 | |||
613 | /** |
||
614 | * @inheritdoc |
||
615 | */ |
||
616 | public function getTableAttributeHtml($value, ElementInterface $element): string |
||
629 | |||
630 | |||
631 | /******************************************* |
||
632 | * SETTINGS |
||
633 | *******************************************/ |
||
634 | |||
635 | /** |
||
636 | * @inheritdoc |
||
637 | * @throws \Twig_Error_Loader |
||
638 | * @throws \yii\base\Exception |
||
639 | */ |
||
640 | public function getSettingsHtml() |
||
647 | |||
648 | /** |
||
649 | * @return array |
||
650 | * @throws \craft\errors\MissingComponentException |
||
651 | * @throws \yii\base\InvalidConfigException |
||
652 | */ |
||
653 | protected function settingsHtmlVariables(): array |
||
662 | |||
663 | /** |
||
664 | * @inheritdoc |
||
665 | */ |
||
666 | public function settingsAttributes(): array |
||
682 | } |
||
683 |