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 Api 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 Api, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace SOSTheBlack\Moip; |
||
| 24 | class Api extends MoipAbstract { |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Encoding of the page |
||
| 28 | * |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | public $encoding = 'UTF-8'; |
||
| 32 | /** |
||
| 33 | * Associative array with two keys. 'key'=>'your_key','token'=>'your_token' |
||
| 34 | * |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | protected $credential; |
||
| 38 | /** |
||
| 39 | * Define the payment's reason |
||
| 40 | * |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | protected $reason; |
||
| 44 | /** |
||
| 45 | * The application's environment |
||
| 46 | * |
||
| 47 | * @var SOSTheBlack\Moip\Environment |
||
| 48 | */ |
||
| 49 | protected $environment = null; |
||
| 50 | /** |
||
| 51 | * Transaction's unique ID |
||
| 52 | * |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | protected $uniqueID; |
||
| 56 | /** |
||
| 57 | * Associative array of payment's way |
||
| 58 | * |
||
| 59 | * @var array |
||
| 60 | */ |
||
| 61 | protected $payment_ways = array('billet' => 'BoletoBancario', |
||
| 62 | 'financing' => 'FinanciamentoBancario', |
||
| 63 | 'debit' => 'DebitoBancario', |
||
| 64 | 'creditCard' => 'CartaoCredito', |
||
| 65 | 'debitCard' => 'CartaoDebito'); |
||
| 66 | /** |
||
| 67 | * Associative array of payment's institutions |
||
| 68 | * |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | protected $institution = array('moip' => 'MoIP', |
||
| 72 | 'visa' => 'Visa', |
||
| 73 | 'american_express' => 'AmericanExpress', |
||
| 74 | 'mastercard' => 'Mastercard', |
||
| 75 | 'diners' => 'Diners', |
||
| 76 | 'banco_brasil' => 'BancoDoBrasil', |
||
| 77 | 'bradesco' => 'Bradesco', |
||
| 78 | 'itau' => 'Itau', |
||
| 79 | 'real' => 'BancoReal', |
||
| 80 | 'unibanco' => 'Unibanco', |
||
| 81 | 'aura' => 'Aura', |
||
| 82 | 'hipercard' => 'Hipercard', |
||
| 83 | 'paggo' => 'Paggo', //oi paggo |
||
| 84 | 'banrisul' => 'Banrisul' |
||
| 85 | ); |
||
| 86 | /** |
||
| 87 | * Associative array of delivery's type |
||
| 88 | * |
||
| 89 | * @var array |
||
| 90 | */ |
||
| 91 | protected $delivery_type = array('proprio' => 'Proprio', 'correios' => 'Correios'); |
||
| 92 | /** |
||
| 93 | * Associative array with type of delivery's time |
||
| 94 | * |
||
| 95 | * @var array |
||
| 96 | */ |
||
| 97 | protected $delivery_type_time = array('corridos' => 'Corridos', 'uteis' => 'Uteis'); |
||
| 98 | /** |
||
| 99 | * Payment method |
||
| 100 | * |
||
| 101 | * @var array |
||
| 102 | */ |
||
| 103 | protected $payment_method; |
||
| 104 | /** |
||
| 105 | * Arguments of payment method |
||
| 106 | * |
||
| 107 | * @var array |
||
| 108 | */ |
||
| 109 | protected $payment_method_args; |
||
| 110 | /** |
||
| 111 | * Payment's type |
||
| 112 | * |
||
| 113 | * @var string |
||
| 114 | */ |
||
| 115 | protected $payment_type; |
||
| 116 | /** |
||
| 117 | * Associative array with payer's information |
||
| 118 | * |
||
| 119 | * @var array |
||
| 120 | */ |
||
| 121 | protected $payer; |
||
| 122 | /** |
||
| 123 | * Server's answer |
||
| 124 | * |
||
| 125 | * @var \SOSTheBlack\Moip\Response |
||
| 126 | */ |
||
| 127 | public $answer; |
||
| 128 | /** |
||
| 129 | * The transaction's value |
||
| 130 | * |
||
| 131 | * @var float |
||
| 132 | */ |
||
| 133 | protected $value; |
||
| 134 | /** |
||
| 135 | * Simple XML object |
||
| 136 | * |
||
| 137 | * @var SimpleXMLElement |
||
| 138 | */ |
||
| 139 | protected $xml; |
||
| 140 | /** |
||
| 141 | * Simple XML object |
||
| 142 | * |
||
| 143 | * @var object |
||
| 144 | */ |
||
| 145 | public $errors; |
||
| 146 | /** |
||
| 147 | * @var array |
||
| 148 | */ |
||
| 149 | protected $payment_way = array(); |
||
| 150 | /** |
||
| 151 | * @var float |
||
| 152 | */ |
||
| 153 | protected $adds; |
||
| 154 | /** |
||
| 155 | * @var float |
||
| 156 | */ |
||
| 157 | protected $deduction; |
||
| 158 | /** |
||
| 159 | * Method construct |
||
| 160 | * |
||
| 161 | * @access public |
||
| 162 | */ |
||
| 163 | public function __construct() { |
||
| 172 | |||
| 173 | private function convert_encoding($text, $post = false) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Method initXMLObject() |
||
| 192 | * |
||
| 193 | * Start a new XML structure for the requests |
||
| 194 | * |
||
| 195 | * @return void |
||
| 196 | * @access private |
||
| 197 | */ |
||
| 198 | private function initXMLObject() { |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Method setPaymentType() |
||
| 205 | * |
||
| 206 | * Define the payment's type between 'Basic' or 'Identification' |
||
| 207 | * |
||
| 208 | * @param string $tipo Can be 'Basic' or 'Identification' |
||
| 209 | * @return Api |
||
| 210 | * @access public |
||
| 211 | */ |
||
| 212 | public function setPaymentType($tipo) { |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Method setCredential() |
||
| 224 | * |
||
| 225 | * Set the credentials(key,token) required for the API authentication. |
||
| 226 | * |
||
| 227 | * @param array $credential Array with the credentials token and key |
||
| 228 | * @return Api |
||
| 229 | * @access public |
||
| 230 | */ |
||
| 231 | public function setCredential($credential) { |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Method \SOSTheBlack\Moip\Environment setEnvironment() |
||
| 244 | * |
||
| 245 | * Define the environment for the API utilization. |
||
| 246 | * |
||
| 247 | * @param bool $testing If true, will use the sandbox environment |
||
| 248 | * @return Api |
||
| 249 | */ |
||
| 250 | public function setEnvironment($testing = false) { |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Method validate() |
||
| 269 | * |
||
| 270 | * Make the data validation |
||
| 271 | * |
||
| 272 | * @param string $validateType Identification or Basic, defaults to Basic |
||
| 273 | * @return Api |
||
| 274 | * @access public |
||
| 275 | */ |
||
| 276 | public function validate($validateType = "Basic") { |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Method setUniqueID() |
||
| 326 | * |
||
| 327 | * Set the unique ID for the transaction |
||
| 328 | * |
||
| 329 | * @param int $id Unique ID for each transaction |
||
| 330 | * @return Api |
||
| 331 | * @access public |
||
| 332 | */ |
||
| 333 | public function setUniqueID($id) { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Method setReason() |
||
| 341 | * |
||
| 342 | * Set the short description of transaction. eg. Order Number. |
||
| 343 | * |
||
| 344 | * @param string $reason The reason fo transaction |
||
| 345 | * @return Api |
||
| 346 | * @access public |
||
| 347 | */ |
||
| 348 | public function setReason($reason) { |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Method addPaymentWay() |
||
| 357 | * |
||
| 358 | * Add a payment's method |
||
| 359 | * |
||
| 360 | * @param string $way The payment method. Options: 'billet','financing','debit','creditCard','debitCard'. |
||
| 361 | * @return Api |
||
| 362 | * @access public |
||
| 363 | */ |
||
| 364 | public function addPaymentWay($way) { |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Method billetConf() |
||
| 384 | * |
||
| 385 | * Add a payment's method |
||
| 386 | * |
||
| 387 | * @param int $expiration expiration in days or dateTime. |
||
| 388 | * @param boolean $workingDays expiration should be counted in working days? |
||
| 389 | * @param array $instructions Additional payment instructions can be array of message or a message in string |
||
| 390 | * @param string $uriLogo URL of the image to be displayed on docket (75x40) |
||
| 391 | * @return Api |
||
| 392 | * @access public |
||
| 393 | */ |
||
| 394 | public function setBilletConf($expiration, $workingDays=false, $instructions = null, $uriLogo = null) { |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Method setPayer() |
||
| 430 | * |
||
| 431 | * Set contacts informations for the payer. |
||
| 432 | * |
||
| 433 | * @param string $payer Contact information for the payer. |
||
| 434 | * @return Api |
||
| 435 | * @access public |
||
| 436 | */ |
||
| 437 | public function setPayer($payer) { |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Method setValue() |
||
| 475 | * |
||
| 476 | * Set the transaction's value |
||
| 477 | * |
||
| 478 | * @param float $value The transaction's value |
||
| 479 | * @return Api |
||
| 480 | * @access public |
||
| 481 | */ |
||
| 482 | public function setValue($value) { |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Method setAdds() |
||
| 497 | * |
||
| 498 | * Adds a value on payment. Can be used for collecting fines, shipping and other |
||
| 499 | * |
||
| 500 | * @param float $value The value to add. |
||
| 501 | * @return Api |
||
| 502 | * @access public |
||
| 503 | */ |
||
| 504 | View Code Duplication | public function setAdds($value) { |
|
| 514 | |||
| 515 | /** |
||
| 516 | * Method setDeduct() |
||
| 517 | * |
||
| 518 | * Deducts a payment amount. It is mainly used for discounts. |
||
| 519 | * |
||
| 520 | * @param float $value The value to deduct |
||
| 521 | * @return Api |
||
| 522 | * @access public |
||
| 523 | */ |
||
| 524 | View Code Duplication | public function setDeduct($value) { |
|
| 534 | |||
| 535 | /** |
||
| 536 | * Method addMessage() |
||
| 537 | * |
||
| 538 | * Add a message in the instruction to be displayed to the payer. |
||
| 539 | * |
||
| 540 | * @param string $msg Message to be displayed. |
||
| 541 | * @return Api |
||
| 542 | * @access public |
||
| 543 | */ |
||
| 544 | View Code Duplication | public function addMessage($msg) { |
|
| 552 | |||
| 553 | /** |
||
| 554 | * Method setReturnURL() |
||
| 555 | * |
||
| 556 | * Set the return URL, which redirects the client after payment. |
||
| 557 | * |
||
| 558 | * @param string $url Return URL |
||
| 559 | * @return Api |
||
| 560 | * @access public |
||
| 561 | */ |
||
| 562 | public function setReturnURL($url) { |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Method setNotificationURL() |
||
| 571 | * |
||
| 572 | * Set the notification URL, which sends information about changes in payment status |
||
| 573 | * |
||
| 574 | * @param string $url Notification URL |
||
| 575 | * @access public |
||
| 576 | */ |
||
| 577 | public function setNotificationURL($url) { |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Method setError() |
||
| 585 | * |
||
| 586 | * Set Erroe alert |
||
| 587 | * |
||
| 588 | * @param String $error Error alert |
||
| 589 | * @return Api |
||
| 590 | * @access public |
||
| 591 | */ |
||
| 592 | public function setError($error) { |
||
| 597 | |||
| 598 | /** |
||
| 599 | * Method addComission() |
||
| 600 | * |
||
| 601 | * Allows to specify commissions on the payment, like fixed values or percent. |
||
| 602 | * |
||
| 603 | * @param string $reason reason for commissioning |
||
| 604 | * @param string $receiver login Moip the secondary receiver |
||
| 605 | * @param number $value value of the division of payment |
||
| 606 | * @param boolean $percentageValue percentage value should be |
||
| 607 | * @param boolean $ratePayer this secondary recipient will pay the fee Moip |
||
| 608 | * @return Api |
||
| 609 | * @access public |
||
| 610 | */ |
||
| 611 | public function addComission($reason, $receiver, $value, $percentageValue=false, $ratePayer=false) { |
||
| 634 | |||
| 635 | /** |
||
| 636 | * Method addParcel() |
||
| 637 | * |
||
| 638 | * Allows to add a order to parceling. |
||
| 639 | * |
||
| 640 | * @param int $min The minimum number of parcels. |
||
| 641 | * @param int $max The maximum number of parcels. |
||
| 642 | * @param float $rate The percentual value of rates |
||
| 643 | * @param boolean $transfer "true" defines the amount of interest charged by MoIP installment to be paid by the payer |
||
| 644 | * @return Api |
||
| 645 | * @access public |
||
| 646 | */ |
||
| 647 | public function addParcel($min, $max, $rate=null, $transfer=false, $receipt=false) { |
||
| 681 | |||
| 682 | /** |
||
| 683 | * @param boolean $receipt |
||
| 684 | */ |
||
| 685 | private function receipt($receipt) |
||
| 689 | |||
| 690 | /** |
||
| 691 | * Method setReceiving() |
||
| 692 | * |
||
| 693 | * Allows to add a order to parceling. |
||
| 694 | * |
||
| 695 | * @param string $receiver login Moip the secondary receiver |
||
| 696 | * @return Api |
||
| 697 | * @access public |
||
| 698 | */ |
||
| 699 | public function setReceiver($receiver) { |
||
| 707 | |||
| 708 | /** |
||
| 709 | * Method getXML() |
||
| 710 | * |
||
| 711 | * Returns the XML that is generated. Useful for debugging. |
||
| 712 | * |
||
| 713 | * @return string |
||
| 714 | * @access public |
||
| 715 | */ |
||
| 716 | public function getXML() { |
||
| 724 | |||
| 725 | /** |
||
| 726 | * Method send() |
||
| 727 | * |
||
| 728 | * Send the request to the server |
||
| 729 | * |
||
| 730 | * @param object $client The server's connection |
||
| 731 | * @return type|null |
||
| 732 | * @access public |
||
| 733 | */ |
||
| 734 | public function send($client=null) { |
||
| 746 | |||
| 747 | /** |
||
| 748 | * Method getAnswer() |
||
| 749 | * |
||
| 750 | * Gets the server's answer |
||
| 751 | * @param boolean $return_xml_as_string Return the answer XMl string |
||
| 752 | * @return MoipResponse|string |
||
| 753 | * @access public |
||
| 754 | */ |
||
| 755 | public function getAnswer($return_xml_as_string = false) { |
||
| 773 | |||
| 774 | /** |
||
| 775 | * Method verifyParcelValues() |
||
| 776 | * |
||
| 777 | * Get all informations about the parcelling of user defined by $login_moip |
||
| 778 | * |
||
| 779 | * @param string $login The client's login for Moip services |
||
| 780 | * @param int $maxParcel The total parcels |
||
| 781 | * @param float $rate The rate's percents of the parcelling. |
||
| 782 | * @param float $simulatedValue The value for simulation |
||
| 783 | * @return array |
||
| 784 | * @access public |
||
| 785 | */ |
||
| 786 | public function queryParcel($login, $maxParcel, $rate, $simulatedValue) { |
||
| 819 | |||
| 820 | } |
||
| 821 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..