| Conditions | 11 |
| Paths | 71 |
| Total Lines | 95 |
| Code Lines | 62 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 34 |
| CRAP Score | 20.8435 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 115 | 5 | public function __construct($response, $delimiter, $encap_char, $custom_fields) |
|
| 116 | { |
||
| 117 | 5 | if ($response) { |
|
| 118 | |||
| 119 | // If it's an XML response |
||
| 120 | 5 | if (substr($response, 0, 5) == "<?xml") { |
|
| 121 | |||
| 122 | $this->xml = @simplexml_load_string($response); |
||
| 123 | // Set all fields |
||
| 124 | $this->version = array_pop(array_slice(explode('"', $response), 1,1)); |
||
| 125 | $this->response_code = (string)$this->xml->ResponseCode; |
||
| 126 | |||
| 127 | if ($this->response_code == 1) { |
||
| 128 | $this->response_reason_code = (string)$this->xml->Messages->Message->Code; |
||
| 129 | $this->response_reason_text = (string)$this->xml->Messages->Message->Description; |
||
| 130 | } else { |
||
| 131 | $this->response_reason_code = (string)$this->xml->Errors->Error->ErrorCode; |
||
| 132 | $this->response_reason_text = (string)$this->xml->Errors->Error->ErrorText; |
||
| 133 | } |
||
| 134 | |||
| 135 | $this->authorization_code = (string)$this->xml->AuthCode; |
||
| 136 | $this->avs_code = (string)$this->xml->AVSResultCode; |
||
| 137 | $this->card_code_response = (string)$this->xml->CVVResultCode; |
||
| 138 | $this->transaction_id = (string)$this->xml->TransID; |
||
| 139 | $this->md5_hash = (string)$this->xml->TransHash; |
||
| 140 | $this->user_ref = (string)$this->xml->UserRef; |
||
| 141 | $this->card_num = (string)$this->xml->AccountNumber; |
||
| 142 | $this->card_type = (string)$this->xml->AccountType; |
||
| 143 | $this->test_mode = (string)$this->xml->TestMode; |
||
| 144 | $this->ref_trans_id = (string)$this->xml->RefTransID; |
||
| 145 | |||
| 146 | |||
| 147 | } else { // If it's an NVP response |
||
| 148 | |||
| 149 | // Split Array |
||
| 150 | 5 | $this->response = $response; |
|
| 151 | 5 | if ($encap_char) { |
|
| 152 | 5 | $this->_response_array = explode($encap_char.$delimiter.$encap_char, substr($response, 1, -1)); |
|
| 153 | } else { |
||
| 154 | $this->_response_array = explode($delimiter, $response); |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * If AuthorizeNet doesn't return a delimited response. |
||
| 159 | */ |
||
| 160 | 5 | if (count($this->_response_array) < 10) { |
|
| 161 | $this->approved = false; |
||
| 162 | $this->error = true; |
||
| 163 | $this->error_message = "Unrecognized response from AuthorizeNet: $response"; |
||
| 164 | return; |
||
| 165 | } |
||
| 166 | |||
| 167 | |||
| 168 | |||
| 169 | // Set all fields |
||
| 170 | 5 | $this->version = $this->_response_array[0]; |
|
| 171 | 5 | $this->response_code = $this->_response_array[1]; |
|
| 172 | 5 | $this->response_reason_code = $this->_response_array[2]; |
|
| 173 | 5 | $this->response_reason_text = $this->_response_array[3]; |
|
| 174 | 5 | $this->authorization_code = $this->_response_array[4]; |
|
| 175 | 5 | $this->avs_code = $this->_response_array[5]; |
|
| 176 | 5 | $this->card_code_response = $this->_response_array[6]; |
|
| 177 | 5 | $this->transaction_id = $this->_response_array[7]; |
|
| 178 | 5 | $this->md5_hash = $this->_response_array[8]; |
|
| 179 | 5 | $this->user_ref = $this->_response_array[9]; |
|
| 180 | 5 | $this->card_num = $this->_response_array[20]; |
|
| 181 | 5 | $this->card_type = $this->_response_array[21]; |
|
| 182 | 5 | $this->split_tender_id = isset($this->_response_array[22]) ? $this->_response_array[22] : NULL; |
|
| 183 | 5 | $this->requested_amount = isset($this->_response_array[23]) ? $this->_response_array[23] : NULL; |
|
| 184 | 5 | $this->approved_amount = isset($this->_response_array[24]) ? $this->_response_array[24] : NULL; |
|
| 185 | 5 | $this->card_balance = isset($this->_response_array[25]) ? $this->_response_array[25] : NULL; |
|
| 186 | |||
| 187 | |||
| 188 | |||
| 189 | } |
||
| 190 | 5 | $this->approved = ($this->response_code == self::APPROVED); |
|
| 191 | 5 | $this->declined = ($this->response_code == self::DECLINED); |
|
| 192 | 5 | $this->error = ($this->response_code == self::ERROR); |
|
| 193 | 5 | $this->held = ($this->response_code == self::HELD); |
|
| 194 | |||
| 195 | |||
| 196 | 5 | if ($this->error) { |
|
| 197 | 1 | $this->error_message = "AuthorizeNet Error: |
|
| 198 | 1 | Response Code: ".$this->response_code." |
|
| 199 | 1 | Response Reason Code: ".$this->response_reason_code." |
|
| 200 | 1 | Response Reason Text: ".$this->response_reason_text." |
|
| 201 | 5 | "; |
|
| 202 | } |
||
| 203 | |||
| 204 | } else { |
||
| 205 | $this->approved = false; |
||
| 206 | $this->error = true; |
||
| 207 | $this->error_message = "Error connecting to AuthorizeNet"; |
||
| 208 | } |
||
| 209 | 5 | } |
|
| 210 | |||
| 228 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.