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 |
||
| 41 | class Provider extends ActiveRecordWithId |
||
| 42 | { |
||
| 43 | use HandleRules, |
||
| 44 | StateAttribute; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The table alias |
||
| 48 | */ |
||
| 49 | const TABLE_ALIAS = 'patron_providers'; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @deprecated |
||
| 53 | */ |
||
| 54 | const CLIENT_ID_LENGTH = ProviderInstance::CLIENT_ID_LENGTH; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @deprecated |
||
| 58 | */ |
||
| 59 | const CLIENT_SECRET_LENGTH = ProviderInstance::CLIENT_SECRET_LENGTH; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var bool |
||
| 63 | */ |
||
| 64 | public $autoSaveInstances = false; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Environments that are temporarily set during the save process |
||
| 68 | * |
||
| 69 | * @var null|array |
||
| 70 | */ |
||
| 71 | private $insertInstances; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @return string|null |
||
| 75 | */ |
||
| 76 | public function getIcon() |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @inheritdoc |
||
| 89 | * @return ProviderActiveQuery |
||
| 90 | * @throws \yii\base\InvalidConfigException |
||
| 91 | */ |
||
| 92 | public static function find() |
||
| 93 | { |
||
| 94 | /** @noinspection PhpIncompatibleReturnTypeInspection */ |
||
| 95 | return Craft::createObject(ProviderActiveQuery::class, [get_called_class()]); |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @inheritdoc |
||
| 100 | */ |
||
| 101 | public function rules() |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Get all of the associated tokens. |
||
| 136 | * |
||
| 137 | * @param array $config |
||
| 138 | * @return \yii\db\ActiveQuery |
||
| 139 | */ |
||
| 140 | public function getTokens(array $config = []) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Get all of the associated tokens. |
||
| 159 | * |
||
| 160 | * @param array $config |
||
| 161 | * @return \yii\db\ActiveQuery |
||
| 162 | */ |
||
| 163 | public function getLocks(array $config = []) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Get all of the associated instances. |
||
| 182 | * |
||
| 183 | * @param array $config |
||
| 184 | * @return \yii\db\ActiveQuery |
||
| 185 | */ |
||
| 186 | public function getInstances(array $config = []) |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @param array $instances |
||
| 206 | * @return $this |
||
| 207 | */ |
||
| 208 | public function setInstances(array $instances = []) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @param $instance |
||
| 221 | * @return ProviderInstance |
||
| 222 | */ |
||
| 223 | protected function resolveInstance($instance): ProviderInstance |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Get all of the associated environments. |
||
| 240 | * |
||
| 241 | * @param array $config |
||
| 242 | * @return \yii\db\ActiveQuery |
||
| 243 | */ |
||
| 244 | public function getEnvironments(array $config = []) |
||
| 262 | |||
| 263 | /******************************************* |
||
| 264 | * SAVE |
||
| 265 | *******************************************/ |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @param PluginInterface $plugin |
||
| 269 | * @param bool $runValidation |
||
| 270 | * @param null $attributeNames |
||
| 271 | * @return bool |
||
| 272 | * @throws \Throwable |
||
| 273 | * @throws \yii\db\StaleObjectException |
||
| 274 | */ |
||
| 275 | public function saveAndLock(PluginInterface $plugin, $runValidation = true, $attributeNames = null): bool |
||
| 283 | |||
| 284 | |||
| 285 | /******************************************* |
||
| 286 | * LOCK |
||
| 287 | *******************************************/ |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @param PluginInterface $plugin |
||
| 291 | * @return bool |
||
| 292 | * @throws \Throwable |
||
| 293 | * @throws \yii\db\StaleObjectException |
||
| 294 | */ |
||
| 295 | public function addLock(PluginInterface $plugin): bool |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @param PluginInterface $plugin |
||
| 309 | * @return bool |
||
| 310 | * @throws \Throwable |
||
| 311 | */ |
||
| 312 | public function removeLock(PluginInterface $plugin): bool |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @return bool |
||
| 326 | */ |
||
| 327 | public function isLocked(): bool |
||
| 331 | |||
| 332 | /** |
||
| 333 | * @param PluginInterface $plugin |
||
| 334 | * @return int|null |
||
| 335 | */ |
||
| 336 | protected function getPluginId(PluginInterface $plugin) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @param PluginInterface $plugin |
||
| 353 | * @return int|null |
||
| 354 | */ |
||
| 355 | protected function getPluginName(PluginInterface $plugin) |
||
| 369 | |||
| 370 | |||
| 371 | /******************************************* |
||
| 372 | * DELETE |
||
| 373 | *******************************************/ |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @param PluginInterface|null $plugin |
||
| 377 | * @return bool|false|int |
||
| 378 | * @throws \Throwable |
||
| 379 | * @throws \yii\db\StaleObjectException |
||
| 380 | */ |
||
| 381 | public function delete(PluginInterface $plugin = null) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @param PluginInterface|null $plugin |
||
| 388 | * @return bool |
||
| 389 | * @throws \craft\errors\InvalidPluginException |
||
| 390 | */ |
||
| 391 | protected function canDelete(PluginInterface $plugin = null) |
||
| 435 | |||
| 436 | /******************************************* |
||
| 437 | * EVENTS |
||
| 438 | *******************************************/ |
||
| 439 | |||
| 440 | /** |
||
| 441 | * @inheritdoc |
||
| 442 | */ |
||
| 443 | public function beforeSave($insert): bool |
||
| 460 | |||
| 461 | /******************************************* |
||
| 462 | * UPDATE / INSERT |
||
| 463 | *******************************************/ |
||
| 464 | |||
| 465 | /** |
||
| 466 | * We're extracting the environments that may have been explicitly set on the record. When the 'id' |
||
| 467 | * attribute is updated, it removes any associated relationships. |
||
| 468 | * |
||
| 469 | * @inheritdoc |
||
| 470 | * @throws \Throwable |
||
| 471 | */ |
||
| 472 | protected function insertInternal($attributes = null) |
||
| 487 | |||
| 488 | /** |
||
| 489 | * @inheritdoc |
||
| 490 | * @throws \Throwable |
||
| 491 | */ |
||
| 492 | protected function updateInternal($attributes = null) |
||
| 500 | |||
| 501 | /** |
||
| 502 | * @param null $attributes |
||
| 503 | * @return bool |
||
| 504 | * @throws \Throwable |
||
| 505 | * @throws \yii\db\StaleObjectException |
||
| 506 | */ |
||
| 507 | protected function upsertInternal($attributes = null): bool |
||
| 519 | |||
| 520 | /** |
||
| 521 | * @param bool $force |
||
| 522 | * @return bool |
||
| 523 | * @throws \Throwable |
||
| 524 | * @throws \yii\db\StaleObjectException |
||
| 525 | */ |
||
| 526 | protected function saveInstances(bool $force = false): bool |
||
| 566 | |||
| 567 | /** |
||
| 568 | * @return string |
||
| 569 | */ |
||
| 570 | public function getDisplayName(): string |
||
| 571 | { |
||
| 572 | return ProviderHelper::displayName( |
||
| 573 | $this->class |
||
| 574 | ); |
||
| 575 | } |
||
| 576 | } |
||
| 577 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: