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 | * @inheritdoc |
||
82 | */ |
||
83 | protected static function findByCondition($condition) |
||
92 | |||
93 | |||
94 | /** |
||
95 | * @return string|null |
||
96 | */ |
||
97 | public function getIcon() |
||
107 | |||
108 | /** |
||
109 | * @inheritdoc |
||
110 | */ |
||
111 | public function rules() |
||
143 | |||
144 | /** |
||
145 | * Get all of the associated tokens. |
||
146 | * |
||
147 | * @param array $config |
||
148 | * @return \yii\db\ActiveQuery |
||
149 | */ |
||
150 | public function getTokens(array $config = []) |
||
166 | |||
167 | /** |
||
168 | * Get all of the associated tokens. |
||
169 | * |
||
170 | * @param array $config |
||
171 | * @return \yii\db\ActiveQuery |
||
172 | */ |
||
173 | public function getLocks(array $config = []) |
||
189 | |||
190 | /** |
||
191 | * Get all of the associated instances. |
||
192 | * |
||
193 | * @param array $config |
||
194 | * @return \yii\db\ActiveQuery |
||
195 | */ |
||
196 | public function getInstances(array $config = []) |
||
213 | |||
214 | /** |
||
215 | * @param array $instances |
||
216 | * @return $this |
||
217 | */ |
||
218 | public function setInstances(array $instances = []) |
||
228 | |||
229 | /** |
||
230 | * @param $instance |
||
231 | * @return ProviderInstance |
||
232 | */ |
||
233 | protected function resolveInstance($instance): ProviderInstance |
||
247 | |||
248 | /** |
||
249 | * Get all of the associated environments. |
||
250 | * |
||
251 | * @param array $config |
||
252 | * @return \yii\db\ActiveQuery |
||
253 | */ |
||
254 | public function getEnvironments(array $config = []) |
||
272 | |||
273 | /******************************************* |
||
274 | * SAVE |
||
275 | *******************************************/ |
||
276 | |||
277 | /** |
||
278 | * @param PluginInterface $plugin |
||
279 | * @param bool $runValidation |
||
280 | * @param null $attributeNames |
||
281 | * @return bool |
||
282 | */ |
||
283 | public function saveAndLock(PluginInterface $plugin, $runValidation = true, $attributeNames = null): bool |
||
291 | |||
292 | |||
293 | /******************************************* |
||
294 | * LOCK |
||
295 | *******************************************/ |
||
296 | |||
297 | /** |
||
298 | * @param PluginInterface $plugin |
||
299 | * @return bool |
||
300 | */ |
||
301 | public function addLock(PluginInterface $plugin): bool |
||
325 | |||
326 | /** |
||
327 | * @param PluginInterface $plugin |
||
328 | * @return bool |
||
329 | * @throws \Throwable |
||
330 | */ |
||
331 | public function removeLock(PluginInterface $plugin): bool |
||
346 | |||
347 | /** |
||
348 | * @return bool |
||
349 | */ |
||
350 | public function isLocked(): bool |
||
354 | |||
355 | /** |
||
356 | * @param PluginInterface $plugin |
||
357 | * @return int|null |
||
358 | */ |
||
359 | protected function getPluginId(PluginInterface $plugin) |
||
373 | |||
374 | /** |
||
375 | * @param PluginInterface $plugin |
||
376 | * @return int|null |
||
377 | */ |
||
378 | protected function getPluginName(PluginInterface $plugin) |
||
392 | |||
393 | |||
394 | /******************************************* |
||
395 | * DELETE |
||
396 | *******************************************/ |
||
397 | |||
398 | /** |
||
399 | * @param PluginInterface|null $plugin |
||
400 | * @return bool|false|int |
||
401 | * @throws \Throwable |
||
402 | * @throws \yii\db\StaleObjectException |
||
403 | */ |
||
404 | public function delete(PluginInterface $plugin = null) |
||
408 | |||
409 | /** |
||
410 | * @param PluginInterface|null $plugin |
||
411 | * @return bool |
||
412 | * @throws \craft\errors\InvalidPluginException |
||
413 | */ |
||
414 | protected function canDelete(PluginInterface $plugin = null) |
||
458 | |||
459 | /******************************************* |
||
460 | * EVENTS |
||
461 | *******************************************/ |
||
462 | |||
463 | /** |
||
464 | * @inheritdoc |
||
465 | */ |
||
466 | public function beforeSave($insert): bool |
||
483 | |||
484 | /******************************************* |
||
485 | * UPDATE / INSERT |
||
486 | *******************************************/ |
||
487 | |||
488 | /** |
||
489 | * We're extracting the environments that may have been explicitly set on the record. When the 'id' |
||
490 | * attribute is updated, it removes any associated relationships. |
||
491 | * |
||
492 | * @inheritdoc |
||
493 | * @throws \Throwable |
||
494 | */ |
||
495 | protected function insertInternal($attributes = null) |
||
510 | |||
511 | /** |
||
512 | * @inheritdoc |
||
513 | * @throws \Throwable |
||
514 | */ |
||
515 | protected function updateInternal($attributes = null) |
||
523 | |||
524 | /** |
||
525 | * @param null $attributes |
||
526 | * @return bool |
||
527 | * @throws \Throwable |
||
528 | * @throws \yii\db\StaleObjectException |
||
529 | */ |
||
530 | protected function upsertInternal($attributes = null): bool |
||
542 | |||
543 | /** |
||
544 | * @param bool $force |
||
545 | * @return bool |
||
546 | * @throws \Throwable |
||
547 | * @throws \yii\db\StaleObjectException |
||
548 | */ |
||
549 | protected function saveInstances(bool $force = false): bool |
||
589 | |||
590 | /** |
||
591 | * @return string |
||
592 | * @throws \ReflectionException |
||
593 | */ |
||
594 | public function getDisplayName(): string |
||
595 | { |
||
596 | return ProviderHelper::displayName( |
||
597 | $this->class |
||
598 | ); |
||
599 | } |
||
600 | } |
||
601 |