Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Bankly 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 Bankly, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class Bankly |
||
| 24 | { |
||
| 25 | public $api_url; |
||
| 26 | public $login_url; |
||
| 27 | private $client_id; |
||
| 28 | private $client_secret; |
||
| 29 | private $token_expiry = 0; |
||
| 30 | private $token = null; |
||
| 31 | private $api_version = '1.0'; |
||
| 32 | private $headers; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Bankly constructor. |
||
| 36 | * @param null|string $client_secret provided by Bankly Staff |
||
| 37 | * @param null|string $client_id provided by Bankly Staff |
||
| 38 | */ |
||
| 39 | public function __construct($client_secret = null, $client_id = null) |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @param array|null $credentials |
||
| 49 | * @return $this |
||
| 50 | */ |
||
| 51 | public function setClientCredentials(array $credentials = null) |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @return array|mixed |
||
| 60 | * @throws RequestException |
||
| 61 | */ |
||
| 62 | public function getBankList() |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Retrieve your balance account |
||
| 69 | * @param string $branch |
||
| 70 | * @param string $account |
||
| 71 | * @return array|mixed |
||
| 72 | * @throws RequestException |
||
| 73 | * @note If you have a RequestException on this endpoint in staging environment, please use getAccount() method instead. |
||
| 74 | */ |
||
| 75 | public function getBalance(string $branch, string $account) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @param string $account |
||
| 86 | * @param string $includeBalance |
||
| 87 | * @return array|mixed |
||
| 88 | * @throws RequestException |
||
| 89 | * @note This method on this date (2020-10-21) works only on staging environment. Contact Bankly/Acesso for more details |
||
| 90 | */ |
||
| 91 | public function getAccount(string $account, string $includeBalance = 'true') |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @param $branch |
||
| 100 | * @param $account |
||
| 101 | * @param int $offset |
||
| 102 | * @param int $limit |
||
| 103 | * @param string $details |
||
| 104 | * @param string $detailsLevelBasic |
||
| 105 | * @return array|mixed |
||
| 106 | * @throws RequestException |
||
| 107 | */ |
||
| 108 | public function getStatement( |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @param string $branch |
||
| 128 | * @param string $account |
||
| 129 | * @param int $page |
||
| 130 | * @param int $pagesize |
||
| 131 | * @param string $include_details |
||
| 132 | * @param string[] $cardProxy |
||
| 133 | * @param string|null $begin_date |
||
| 134 | * @param string|null $end_date |
||
| 135 | * @return array|mixed |
||
| 136 | * @throws RequestException |
||
| 137 | * @note This endpoint has been deprecated for some clients. |
||
| 138 | * You need to check with Acesso/Bankly if your environment has different parameters also. |
||
| 139 | * The response of this request does not have a default interface between environments. |
||
| 140 | * Pay attention when use this in your project. |
||
| 141 | */ |
||
| 142 | public function getEvents( |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @param int $amount |
||
| 180 | * @param string $description |
||
| 181 | * @param array $sender |
||
| 182 | * @param array $recipient |
||
| 183 | * @param string|null $correlation_id |
||
| 184 | * @return array|mixed |
||
| 185 | * @throws RequestException |
||
| 186 | */ |
||
| 187 | public function transfer( |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Get transfer funds from an account |
||
| 213 | * @param string $branch |
||
| 214 | * @param string $account |
||
| 215 | * @param int $pageSize |
||
| 216 | * @param string|null $nextPage |
||
| 217 | * @return array|mixed |
||
| 218 | * @throws RequestException |
||
| 219 | */ |
||
| 220 | public function getTransferFunds(string $branch, string $account, int $pageSize = 10, string $nextPage = null) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Get Transfer Funds By Authentication Code |
||
| 235 | * @param string $branch |
||
| 236 | * @param string $account |
||
| 237 | * @param string $authenticationCode |
||
| 238 | * @return array|mixed |
||
| 239 | * @throws RequestException |
||
| 240 | */ |
||
| 241 | public function findTransferFundByAuthCode(string $branch, string $account, string $authenticationCode) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @param string $branch |
||
| 252 | * @param string $account |
||
| 253 | * @param string $authentication_id |
||
| 254 | * @return array|mixed |
||
| 255 | * @throws RequestException |
||
| 256 | */ |
||
| 257 | public function getTransferStatus(string $branch, string $account, string $authentication_id) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @param string $documentNumber |
||
| 267 | * @param DocumentAnalysis $document |
||
| 268 | * @param string $correlationId |
||
| 269 | * @return array|mixed |
||
| 270 | * @throws RequestException |
||
| 271 | */ |
||
| 272 | public function documentAnalysis( |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @param string $documentNumber |
||
| 296 | * @param array $tokens |
||
| 297 | * @param string $resultLevel |
||
| 298 | * @param string $correlationId |
||
| 299 | * @return array|mixed |
||
| 300 | */ |
||
| 301 | public function getDocumentAnalysis( |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @param string $documentNumber |
||
| 323 | * @param Customer $customer |
||
| 324 | * @param string $correlationId |
||
| 325 | * @return array|mixed |
||
| 326 | * @throws TypeError|RequestException |
||
| 327 | */ |
||
| 328 | public function customer( |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @param string $documentNumber |
||
| 342 | * @param string $resultLevel |
||
| 343 | * @return array|mixed |
||
| 344 | */ |
||
| 345 | public function getCustomer(string $documentNumber, string $resultLevel = 'DETAILED') |
||
| 349 | |||
| 350 | /** |
||
| 351 | * @param string $documentNumber |
||
| 352 | * @return array|mixed |
||
| 353 | */ |
||
| 354 | public function getCustomerAccounts(string $documentNumber) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Validate of boleto or dealership |
||
| 361 | * |
||
| 362 | * @param string $code - Digitable line |
||
| 363 | * @param string $correlationId |
||
| 364 | * @return array|mixed |
||
| 365 | * @throws RequestException |
||
| 366 | */ |
||
| 367 | public function paymentValidate(string $code, string $correlationId) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Confirmation of payment of boleto or dealership |
||
| 374 | * |
||
| 375 | * @param BillPayment $billPayment |
||
| 376 | * @param string $correlationId |
||
| 377 | * @return array|mixed |
||
| 378 | */ |
||
| 379 | public function paymentConfirm( |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @param DepositBillet $depositBillet |
||
| 388 | * @return array|mixed |
||
| 389 | */ |
||
| 390 | public function depositBillet(DepositBillet $depositBillet) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @param string $authenticationCode |
||
| 397 | * @return mixed |
||
| 398 | */ |
||
| 399 | public function printBillet(string $authenticationCode) |
||
| 403 | |||
| 404 | /** |
||
| 405 | * @param string $branch |
||
| 406 | * @param string $accountNumber |
||
| 407 | * @param string $authenticationCode |
||
| 408 | * @return array|mixed |
||
| 409 | */ |
||
| 410 | public function getBillet(string $branch, string $accountNumber, string $authenticationCode) |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @param string $datetime |
||
| 417 | * @return array|mixed |
||
| 418 | */ |
||
| 419 | public function getBilletByDate(string $datetime) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * @param string $barcode |
||
| 426 | * @return array|mixed |
||
| 427 | */ |
||
| 428 | public function getBilletByBarcode(string $barcode) |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Create a new PIX key link with account. |
||
| 435 | * |
||
| 436 | * @param PixEntries $pixEntries |
||
| 437 | * @return array|mixed |
||
| 438 | */ |
||
| 439 | public function registerPixKey(PixEntries $pixEntries) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Gets the list of address keys linked to an account. |
||
| 449 | * |
||
| 450 | * @param string $accountNumber |
||
| 451 | * @return array|mixed |
||
| 452 | */ |
||
| 453 | public function getPixAddressingKeys(string $accountNumber) |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Gets details of the account linked to an addressing key. |
||
| 460 | * |
||
| 461 | * @param string $documentNumber |
||
| 462 | * @param string $addressinKeyValue |
||
| 463 | * @return array|mixed |
||
| 464 | */ |
||
| 465 | public function getPixAddressingKeyValue(string $documentNumber, string $addressinKeyValue) |
||
| 470 | |||
| 471 | /** |
||
| 472 | * Delete a key link with account. |
||
| 473 | * |
||
| 474 | * @param string $addressingKeyValue |
||
| 475 | * @return array|mixed |
||
| 476 | */ |
||
| 477 | public function deletePixAddressingKeyValue(string $addressingKeyValue) |
||
| 481 | |||
| 482 | /** |
||
| 483 | * @param PixCashoutInterface $pixCashout |
||
| 484 | * @param string $correlationId |
||
| 485 | * @return array|mixed |
||
| 486 | */ |
||
| 487 | public function pixCashout(PixCashoutInterface $pixCashout, string $correlationId) |
||
| 491 | |||
| 492 | /** |
||
| 493 | * @param string $endpoint |
||
| 494 | * @param array|string|null $query |
||
| 495 | * @param null $correlation_id |
||
| 496 | * @return array|mixed |
||
| 497 | * @throws RequestException |
||
| 498 | */ |
||
| 499 | private function get(string $endpoint, $query = null, $correlation_id = null) |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Create a new virtual card |
||
| 518 | * |
||
| 519 | * @param Card $virtualCard |
||
| 520 | * @return array|mixed |
||
| 521 | * @throws RequestException |
||
| 522 | */ |
||
| 523 | public function virtualCard(Card $virtualCard) |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Create a new physical card |
||
| 530 | * |
||
| 531 | * @param Card $physicalCard |
||
| 532 | * @return array|mixed |
||
| 533 | * @throws RequestException |
||
| 534 | */ |
||
| 535 | public function physicalCard(Card $physicalCard) |
||
| 536 | { |
||
| 537 | return $this->post('/cards/physical', $physicalCard->toArray(), null, true); |
||
| 538 | } |
||
| 539 | |||
| 540 | /** |
||
| 541 | * @param string $endpoint |
||
| 542 | * @param array|null $body |
||
| 543 | * @param string|null $correlation_id |
||
| 544 | * @param bool $asJson |
||
| 545 | * @return array|mixed |
||
| 546 | * @throws RequestException |
||
| 547 | */ |
||
| 548 | private function post(string $endpoint, array $body = null, string $correlation_id = null, bool $asJson = false) |
||
| 568 | |||
| 569 | /** |
||
| 570 | * @param string $endpoint |
||
| 571 | * @param array|null $body |
||
| 572 | * @param string|null $correlation_id |
||
| 573 | * @param bool $asJson |
||
| 574 | * @param bool $attachment |
||
| 575 | * @param DocumentAnalysis $document |
||
| 576 | * @param string $fieldName |
||
| 577 | * @return array|mixed |
||
| 578 | * @throws RequestException |
||
| 579 | */ |
||
| 580 | private function put( |
||
| 611 | |||
| 612 | /** |
||
| 613 | * Http delete method. |
||
| 614 | * |
||
| 615 | * @param string $endpoint |
||
| 616 | * @return array|mixed |
||
| 617 | * @throws RequestException |
||
| 618 | */ |
||
| 619 | private function delete(string $endpoint) |
||
| 632 | |||
| 633 | /** |
||
| 634 | * @param string $version API version |
||
| 635 | * @return $this |
||
| 636 | */ |
||
| 637 | private function setApiVersion($version = '1.0') |
||
| 642 | |||
| 643 | /** |
||
| 644 | * @param array $headers |
||
| 645 | * @return array|string[] |
||
| 646 | */ |
||
| 647 | private function getHeaders($headers = []) |
||
| 657 | |||
| 658 | /** |
||
| 659 | * @param array $header |
||
| 660 | * @return void |
||
| 661 | */ |
||
| 662 | private function setHeaders($header) |
||
| 666 | |||
| 667 | /** |
||
| 668 | * @param string $endpoint |
||
| 669 | * @return bool |
||
| 670 | */ |
||
| 671 | private function requireCorrelationId(string $endpoint) |
||
| 680 | |||
| 681 | /** |
||
| 682 | * @param string $endpoint |
||
| 683 | * @return string |
||
| 684 | */ |
||
| 685 | private function getFinalUrl(string $endpoint) |
||
| 689 | |||
| 690 | /** |
||
| 691 | * Do authentication |
||
| 692 | * @param string $grant_type Default sets to 'client_credentials' |
||
| 693 | * @throws RequestException |
||
| 694 | */ |
||
| 695 | View Code Duplication | private function auth($grant_type = 'client_credentials'): void |
|
| 708 | } |
||
| 709 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: