Complex classes like Credential 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 Credential, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class Credential extends Controller |
||
| 20 | { |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Credential model class instance |
||
| 24 | * @var \gplcart\modules\gapi\models\Credential $credential |
||
| 25 | */ |
||
| 26 | protected $credential; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * File transfer module instance |
||
| 30 | * @var \gplcart\core\models\FileTransfer $file_transfer |
||
| 31 | */ |
||
| 32 | protected $file_transfer; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Pager limit |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | protected $data_limit; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The current updating credential |
||
| 42 | * @var array |
||
| 43 | */ |
||
| 44 | protected $data_credential = array(); |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Credential type |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | protected $data_type; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @param CredentialModel $credential |
||
| 54 | * @param FileTransfer $file_transfer |
||
| 55 | */ |
||
| 56 | public function __construct(CredentialModel $credential, FileTransfer $file_transfer) |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Route page callback |
||
| 66 | * Displays the add credential page |
||
| 67 | * @param string $type |
||
| 68 | */ |
||
| 69 | public function addCredential($type) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Handler callback |
||
| 81 | * Edit "API key" credential page |
||
| 82 | */ |
||
| 83 | public function editKeyCredential() |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Handler callback |
||
| 94 | * Edit "OAuth" credential page |
||
| 95 | */ |
||
| 96 | public function editOauthCredential() |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Handler callback |
||
| 107 | * Edit "Service" credential page |
||
| 108 | */ |
||
| 109 | public function editServiceCredential() |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Route page callback |
||
| 120 | * Displays the edit credential page |
||
| 121 | * @param int $credential_id |
||
| 122 | */ |
||
| 123 | public function editCredential($credential_id) |
||
| 124 | { |
||
| 125 | $this->setCredential($credential_id); |
||
| 126 | $this->setTitleEditCredential(); |
||
| 127 | $this->setBreadcrumbEditCredential(); |
||
| 128 | |||
| 129 | $this->setData('credential', $this->data_credential); |
||
| 130 | |||
| 131 | $this->submitDeleteCredential(); |
||
| 132 | $this->credential->callHandler($this->data_credential['type'], 'edit'); |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Sets the credential data |
||
| 137 | * @param $credential_id |
||
| 138 | */ |
||
| 139 | protected function setCredential($credential_id) |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Set titles on the edit credential page |
||
| 151 | */ |
||
| 152 | protected function setTitleEditCredential() |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Set breadcrumbs on the edit credential page |
||
| 165 | */ |
||
| 166 | protected function setBreadcrumbEditCredential() |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Validates "API key" credential data |
||
| 185 | * @return bool |
||
| 186 | */ |
||
| 187 | protected function validateKeyCredential() |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Validates "OAuth" credential data |
||
| 202 | * @return bool |
||
| 203 | */ |
||
| 204 | protected function validateOauthCredential() |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Validates "Service" credential data |
||
| 220 | * @return bool |
||
| 221 | */ |
||
| 222 | protected function validateServiceCredential() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Validates JSON file upload |
||
| 237 | * @return bool |
||
| 238 | */ |
||
| 239 | protected function validateFileUploadCredential() |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Saves the submitted credential data |
||
| 286 | */ |
||
| 287 | protected function saveCredential() |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Updates a submitted credential |
||
| 298 | */ |
||
| 299 | protected function updateSubmittedCredential() |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Adds a submitted credential |
||
| 312 | */ |
||
| 313 | protected function addSubmittedCredential() |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Delete a submitted credential |
||
| 329 | */ |
||
| 330 | protected function submitDeleteCredential() |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Route callback |
||
| 347 | * Displays the credential overview page |
||
| 348 | */ |
||
| 349 | public function listCredential() |
||
| 350 | { |
||
| 351 | $this->actionListCredential(); |
||
| 352 | $this->setTitleListCredential(); |
||
| 353 | $this->setBreadcrumbListCredential(); |
||
| 354 | $this->setFilterListCredential(); |
||
| 355 | $this->setPagerListCredential(); |
||
| 356 | |||
| 357 | $this->setData('credentials', $this->getListCredential()); |
||
| 358 | $this->setData('handlers', $this->credential->getHandlers()); |
||
| 359 | $this->outputListCredential(); |
||
| 360 | } |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Applies an action to the selected credentials |
||
| 364 | */ |
||
| 365 | protected function actionListCredential() |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Sets filter parameters |
||
| 387 | */ |
||
| 388 | protected function setFilterListCredential() |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Sets pager |
||
| 395 | * @return array |
||
| 396 | */ |
||
| 397 | protected function setPagerListCredential() |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Returns an array of credentials |
||
| 412 | * @return array |
||
| 413 | */ |
||
| 414 | protected function getListCredential() |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Sets title on the credential overview page |
||
| 424 | */ |
||
| 425 | protected function setTitleListCredential() |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Sets breadcrumbs on the credential overview page |
||
| 432 | */ |
||
| 433 | protected function setBreadcrumbListCredential() |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Render and output the credential overview page |
||
| 445 | */ |
||
| 446 | protected function outputListCredential() |
||
| 450 | |||
| 451 | } |
||
| 452 |