Complex classes like Provider 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 Provider, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | class Provider extends ActiveRecordWithId |
||
38 | { |
||
39 | use HandleRulesTrait, |
||
40 | StateAttributeTrait; |
||
41 | |||
42 | /** |
||
43 | * The table alias |
||
44 | */ |
||
45 | const TABLE_ALIAS = 'patron_providers'; |
||
46 | |||
47 | /** |
||
48 | * @deprecated |
||
49 | */ |
||
50 | const CLIENT_ID_LENGTH = ProviderInstance::CLIENT_ID_LENGTH; |
||
51 | |||
52 | /** |
||
53 | * @deprecated |
||
54 | */ |
||
55 | const CLIENT_SECRET_LENGTH = ProviderInstance::CLIENT_SECRET_LENGTH; |
||
56 | |||
57 | /** |
||
58 | * @var bool |
||
59 | */ |
||
60 | public $autoSaveInstances = false; |
||
61 | |||
62 | /** |
||
63 | * Environments that are temporarily set during the save process |
||
64 | * |
||
65 | * @var null|array |
||
66 | */ |
||
67 | private $insertInstances; |
||
68 | |||
69 | /** |
||
70 | * @inheritdoc |
||
71 | * @return ProviderActiveQuery |
||
72 | * @throws \yii\base\InvalidConfigException |
||
73 | */ |
||
74 | public static function find() |
||
75 | { |
||
76 | /** @noinspection PhpIncompatibleReturnTypeInspection */ |
||
77 | return Craft::createObject(ProviderActiveQuery::class, [get_called_class()]); |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @return string|null |
||
82 | */ |
||
83 | public function getIcon() |
||
84 | { |
||
85 | if ($this->class === null) { |
||
86 | return null; |
||
87 | } |
||
88 | |||
89 | return Patron::getInstance()->getCp()->getProviderIcon( |
||
90 | $this->class |
||
91 | ); |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @inheritdoc |
||
96 | */ |
||
97 | public function rules() |
||
129 | |||
130 | /** |
||
131 | * Get all of the associated tokens. |
||
132 | * |
||
133 | * @param array $config |
||
134 | * @return \yii\db\ActiveQuery |
||
135 | */ |
||
136 | public function getTokens(array $config = []) |
||
152 | |||
153 | /** |
||
154 | * Get all of the associated tokens. |
||
155 | * |
||
156 | * @param array $config |
||
157 | * @return \yii\db\ActiveQuery |
||
158 | */ |
||
159 | public function getLocks(array $config = []) |
||
175 | |||
176 | /** |
||
177 | * Get all of the associated instances. |
||
178 | * |
||
179 | * @param array $config |
||
180 | * @return \yii\db\ActiveQuery |
||
181 | */ |
||
182 | public function getInstances(array $config = []) |
||
199 | |||
200 | /** |
||
201 | * @param array $instances |
||
202 | * @return $this |
||
203 | */ |
||
204 | public function setInstances(array $instances = []) |
||
214 | |||
215 | /** |
||
216 | * @param $instance |
||
217 | * @return ProviderInstance |
||
218 | */ |
||
219 | protected function resolveInstance($instance): ProviderInstance |
||
233 | |||
234 | /** |
||
235 | * Get all of the associated environments. |
||
236 | * |
||
237 | * @param array $config |
||
238 | * @return \yii\db\ActiveQuery |
||
239 | */ |
||
240 | public function getEnvironments(array $config = []) |
||
258 | |||
259 | /******************************************* |
||
260 | * SAVE |
||
261 | *******************************************/ |
||
262 | |||
263 | /** |
||
264 | * @param PluginInterface $plugin |
||
265 | * @param bool $runValidation |
||
266 | * @param null $attributeNames |
||
267 | * @return bool |
||
268 | * @throws \Throwable |
||
269 | * @throws \yii\db\StaleObjectException |
||
270 | */ |
||
271 | public function saveAndLock(PluginInterface $plugin, $runValidation = true, $attributeNames = null): bool |
||
279 | |||
280 | |||
281 | /******************************************* |
||
282 | * LOCK |
||
283 | *******************************************/ |
||
284 | |||
285 | /** |
||
286 | * @param PluginInterface $plugin |
||
287 | * @return bool |
||
288 | */ |
||
289 | public function addLock(PluginInterface $plugin): bool |
||
304 | |||
305 | /** |
||
306 | * @param PluginInterface $plugin |
||
307 | * @return bool |
||
308 | * @throws \Throwable |
||
309 | */ |
||
310 | public function removeLock(PluginInterface $plugin): bool |
||
325 | |||
326 | /** |
||
327 | * @return bool |
||
328 | */ |
||
329 | public function isLocked(): bool |
||
333 | |||
334 | /** |
||
335 | * @param PluginInterface $plugin |
||
336 | * @return int|null |
||
337 | */ |
||
338 | protected function getPluginId(PluginInterface $plugin) |
||
352 | |||
353 | /** |
||
354 | * @param PluginInterface $plugin |
||
355 | * @return int|null |
||
356 | */ |
||
357 | protected function getPluginName(PluginInterface $plugin) |
||
371 | |||
372 | |||
373 | /******************************************* |
||
374 | * DELETE |
||
375 | *******************************************/ |
||
376 | |||
377 | /** |
||
378 | * @param PluginInterface|null $plugin |
||
379 | * @return bool|false|int |
||
380 | * @throws \Throwable |
||
381 | * @throws \yii\db\StaleObjectException |
||
382 | */ |
||
383 | public function delete(PluginInterface $plugin = null) |
||
387 | |||
388 | /** |
||
389 | * @param PluginInterface|null $plugin |
||
390 | * @return bool |
||
391 | * @throws \craft\errors\InvalidPluginException |
||
392 | */ |
||
393 | protected function canDelete(PluginInterface $plugin = null) |
||
437 | |||
438 | /******************************************* |
||
439 | * EVENTS |
||
440 | *******************************************/ |
||
441 | |||
442 | /** |
||
443 | * @inheritdoc |
||
444 | */ |
||
445 | public function beforeSave($insert): bool |
||
462 | |||
463 | /******************************************* |
||
464 | * UPDATE / INSERT |
||
465 | *******************************************/ |
||
466 | |||
467 | /** |
||
468 | * We're extracting the environments that may have been explicitly set on the record. When the 'id' |
||
469 | * attribute is updated, it removes any associated relationships. |
||
470 | * |
||
471 | * @inheritdoc |
||
472 | * @throws \Throwable |
||
473 | */ |
||
474 | protected function insertInternal($attributes = null) |
||
489 | |||
490 | /** |
||
491 | * @inheritdoc |
||
492 | * @throws \Throwable |
||
493 | */ |
||
494 | protected function updateInternal($attributes = null) |
||
502 | |||
503 | /** |
||
504 | * @param null $attributes |
||
505 | * @return bool |
||
506 | * @throws \Throwable |
||
507 | * @throws \yii\db\StaleObjectException |
||
508 | */ |
||
509 | protected function upsertInternal($attributes = null): bool |
||
521 | |||
522 | /** |
||
523 | * @param bool $force |
||
524 | * @return bool |
||
525 | * @throws \Throwable |
||
526 | * @throws \yii\db\StaleObjectException |
||
527 | */ |
||
528 | protected function saveInstances(bool $force = false): bool |
||
568 | |||
569 | /** |
||
570 | * @return string |
||
571 | */ |
||
572 | public function getDisplayName(): string |
||
573 | { |
||
574 | return ProviderHelper::displayName( |
||
575 | $this->class |
||
576 | ); |
||
577 | } |
||
578 | |||
579 | /** |
||
580 | * @return string |
||
581 | */ |
||
582 | public function __toString() |
||
586 | } |
||
587 |