| Conditions | 11 |
| Paths | 16 |
| Total Lines | 71 |
| Code Lines | 38 |
| Lines | 4 |
| Ratio | 5.63 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
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 |
||
| 80 | private function doCall($url, $body = null, $method = 'GET') |
||
| 81 | { |
||
| 82 | // build Authorization header |
||
| 83 | $headers[] = 'Authorization: Basic ' . $this->getAuthorizationHeader(); |
||
| 84 | |||
| 85 | // set options |
||
| 86 | $options[CURLOPT_URL] = self::API_URL . $url; |
||
| 87 | $options[CURLOPT_USERAGENT] = $this->getUserAgent(); |
||
| 88 | $options[CURLOPT_RETURNTRANSFER] = true; |
||
| 89 | $options[CURLOPT_TIMEOUT] = (int) $this->getTimeOut(); |
||
| 90 | $options[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_1; |
||
| 91 | $options[CURLOPT_HTTPHEADER] = $headers; |
||
| 92 | |||
| 93 | View Code Duplication | if ($method == 'POST') { |
|
| 94 | $options[CURLOPT_POST] = true; |
||
| 95 | $options[CURLOPT_POSTFIELDS] = $body; |
||
| 96 | } |
||
| 97 | |||
| 98 | // init |
||
| 99 | $this->curl = curl_init(); |
||
| 100 | |||
| 101 | // set options |
||
| 102 | curl_setopt_array($this->curl, $options); |
||
| 103 | |||
| 104 | // execute |
||
| 105 | $response = curl_exec($this->curl); |
||
| 106 | $headers = curl_getinfo($this->curl); |
||
| 107 | |||
| 108 | // fetch errors |
||
| 109 | $errorNumber = curl_errno($this->curl); |
||
| 110 | $errorMessage = curl_error($this->curl); |
||
| 111 | |||
| 112 | // error? |
||
| 113 | if ($errorNumber != '') { |
||
| 114 | throw new BpostCurlException($errorMessage, $errorNumber); |
||
| 115 | } |
||
| 116 | |||
| 117 | // valid HTTP-code |
||
| 118 | if (!in_array($headers['http_code'], array(0, 200))) { |
||
| 119 | $xml = @simplexml_load_string($response); |
||
| 120 | |||
| 121 | if ( |
||
| 122 | $xml !== false |
||
| 123 | && ($xml->getName() == 'businessException' || $xml->getName() == 'systemException') |
||
| 124 | ) { |
||
| 125 | $message = (string) $xml->message; |
||
| 126 | $code = isset($xml->code) ? (int) $xml->code : null; |
||
| 127 | switch ($xml->getName()) { |
||
| 128 | case 'businessException': |
||
| 129 | throw new BpostApiBusinessException($message, $code); |
||
| 130 | case 'systemException': |
||
| 131 | throw new BpostApiSystemException($message, $code); |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | throw new BpostInvalidResponseException('', $headers['http_code']); |
||
| 136 | } |
||
| 137 | |||
| 138 | // convert into XML |
||
| 139 | $xml = simplexml_load_string($response); |
||
| 140 | |||
| 141 | // validate |
||
| 142 | if ($xml->getName() == 'businessException') { |
||
| 143 | $message = (string) $xml->message; |
||
| 144 | $code = (string) $xml->code; |
||
| 145 | throw new BpostApiBusinessException($message, $code); |
||
| 146 | } |
||
| 147 | |||
| 148 | // return the response |
||
| 149 | return $xml; |
||
| 150 | } |
||
| 151 | |||
| 270 |
This check marks private properties in classes that are never used. Those properties can be removed.