Conditions | 10 |
Paths | 30 |
Total Lines | 41 |
Code Lines | 24 |
Lines | 0 |
Ratio | 0 % |
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 |
||
109 | private function attemptCurl($method, $endpoint, $headers, $body) |
||
110 | { |
||
111 | //open connection |
||
112 | $ch = \curl_init(); |
||
113 | \curl_setopt($ch, \CURLOPT_URL, $endpoint); |
||
114 | |||
115 | if ($method === RouteInterface::POST_METHOD || $method === RouteInterface::PUT_METHOD) { |
||
116 | ($method === RouteInterface:: POST_METHOD) && \curl_setopt($ch, \CURLOPT_POST, true); |
||
117 | ($method === RouteInterface ::PUT_METHOD) && \curl_setopt($ch, \CURLOPT_CUSTOMREQUEST, "PUT"); |
||
118 | |||
119 | \curl_setopt($ch, \CURLOPT_POSTFIELDS, $body); |
||
120 | } |
||
121 | //flatten the headers |
||
122 | $flattened_headers = []; |
||
123 | while (list($key, $value) = each($headers)) { |
||
124 | $flattened_headers[] = $key . ": " . $value; |
||
125 | } |
||
126 | \curl_setopt($ch, \CURLOPT_HTTPHEADER, $flattened_headers); |
||
127 | \curl_setopt($ch, \CURLOPT_RETURNTRANSFER, 1); |
||
128 | \curl_setopt($ch, \CURLOPT_SSLVERSION, 6); |
||
129 | |||
130 | $response = \curl_exec($ch); |
||
131 | |||
132 | if (\curl_errno($ch)) { // should be 0 |
||
133 | // curl ended with an error |
||
134 | $cerr = \curl_error($ch); |
||
135 | \curl_close($ch); |
||
136 | throw new \Exception("Curl failed with response: '" . $cerr . "'."); |
||
137 | } |
||
138 | |||
139 | // Decode JSON from Paystack: |
||
140 | $resp = \json_decode($response); |
||
141 | \curl_close($ch); |
||
142 | |||
143 | if (json_last_error() !== JSON_ERROR_NONE || !$resp->status) { |
||
144 | throw new \Exception("Paystack Request failed with response: '" . |
||
145 | ((json_last_error() === JSON_ERROR_NONE) ? $resp->message : $response) . "'."); |
||
146 | } |
||
147 | |||
148 | return $resp; |
||
149 | } |
||
150 | } |
||
151 |