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 HandleRules, |
||
| 40 | StateAttribute; |
||
| 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 | * @return string|null |
||
| 71 | */ |
||
| 72 | public function getIcon() |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @inheritdoc |
||
| 85 | * @return ProviderActiveQuery |
||
| 86 | * @throws \yii\base\InvalidConfigException |
||
| 87 | */ |
||
| 88 | public static function find() |
||
| 89 | { |
||
| 90 | /** @noinspection PhpIncompatibleReturnTypeInspection */ |
||
| 91 | return Craft::createObject(ProviderActiveQuery::class, [get_called_class()]); |
||
| 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 | * @throws \Throwable |
||
| 289 | * @throws \yii\db\StaleObjectException |
||
| 290 | */ |
||
| 291 | public function addLock(PluginInterface $plugin): bool |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @param PluginInterface $plugin |
||
| 305 | * @return bool |
||
| 306 | * @throws \Throwable |
||
| 307 | */ |
||
| 308 | public function removeLock(PluginInterface $plugin): bool |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @return bool |
||
| 322 | */ |
||
| 323 | public function isLocked(): bool |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @param PluginInterface $plugin |
||
| 330 | * @return int|null |
||
| 331 | */ |
||
| 332 | protected function getPluginId(PluginInterface $plugin) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @param PluginInterface $plugin |
||
| 349 | * @return int|null |
||
| 350 | */ |
||
| 351 | protected function getPluginName(PluginInterface $plugin) |
||
| 365 | |||
| 366 | |||
| 367 | /******************************************* |
||
| 368 | * DELETE |
||
| 369 | *******************************************/ |
||
| 370 | |||
| 371 | /** |
||
| 372 | * @param PluginInterface|null $plugin |
||
| 373 | * @return bool|false|int |
||
| 374 | * @throws \Throwable |
||
| 375 | * @throws \yii\db\StaleObjectException |
||
| 376 | */ |
||
| 377 | public function delete(PluginInterface $plugin = null) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * @param PluginInterface|null $plugin |
||
| 384 | * @return bool |
||
| 385 | * @throws \craft\errors\InvalidPluginException |
||
| 386 | */ |
||
| 387 | protected function canDelete(PluginInterface $plugin = null) |
||
| 431 | |||
| 432 | /******************************************* |
||
| 433 | * EVENTS |
||
| 434 | *******************************************/ |
||
| 435 | |||
| 436 | /** |
||
| 437 | * @inheritdoc |
||
| 438 | */ |
||
| 439 | public function beforeSave($insert): bool |
||
| 456 | |||
| 457 | /******************************************* |
||
| 458 | * UPDATE / INSERT |
||
| 459 | *******************************************/ |
||
| 460 | |||
| 461 | /** |
||
| 462 | * We're extracting the environments that may have been explicitly set on the record. When the 'id' |
||
| 463 | * attribute is updated, it removes any associated relationships. |
||
| 464 | * |
||
| 465 | * @inheritdoc |
||
| 466 | * @throws \Throwable |
||
| 467 | */ |
||
| 468 | protected function insertInternal($attributes = null) |
||
| 483 | |||
| 484 | /** |
||
| 485 | * @inheritdoc |
||
| 486 | * @throws \Throwable |
||
| 487 | */ |
||
| 488 | protected function updateInternal($attributes = null) |
||
| 489 | { |
||
| 490 | if (false === ($response = parent::updateInternal($attributes))) { |
||
| 491 | return false; |
||
| 492 | } |
||
| 493 | |||
| 494 | return $this->upsertInternal($attributes) ? $response : false; |
||
| 495 | } |
||
| 496 | |||
| 497 | /** |
||
| 498 | * @param null $attributes |
||
| 499 | * @return bool |
||
| 500 | * @throws \Throwable |
||
| 501 | * @throws \yii\db\StaleObjectException |
||
| 502 | */ |
||
| 503 | protected function upsertInternal($attributes = null): bool |
||
| 515 | |||
| 516 | /** |
||
| 517 | * @param bool $force |
||
| 518 | * @return bool |
||
| 519 | * @throws \Throwable |
||
| 520 | * @throws \yii\db\StaleObjectException |
||
| 521 | */ |
||
| 522 | protected function saveInstances(bool $force = false): bool |
||
| 562 | |||
| 563 | /** |
||
| 564 | * @return string |
||
| 565 | */ |
||
| 566 | public function getDisplayName(): string |
||
| 567 | { |
||
| 568 | return ProviderHelper::displayName( |
||
| 569 | $this->class |
||
| 570 | ); |
||
| 571 | } |
||
| 572 | } |
||
| 573 |