| Conditions | 12 |
| Paths | 33 |
| Total Lines | 67 |
| Code Lines | 40 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 111 | private function callViaCurl($interface, $payload = [ ], $sentargs = [ ]) |
||
| 112 | { |
||
| 113 | |||
| 114 | |||
| 115 | $endpoint = Router::PAYSTACK_API_ROOT . $interface[RouteInterface::ENDPOINT_KEY]; |
||
| 116 | $method = $interface[RouteInterface::METHOD_KEY]; |
||
| 117 | |||
| 118 | $this->moveArgsToSentargs($interface, $payload, $sentargs); |
||
| 119 | $this->putArgsIntoEndpoint($endpoint, $sentargs); |
||
| 120 | |||
| 121 | $headers = ["Authorization"=>"Bearer " . $this->secret_key ]; |
||
| 122 | if (($method === RouteInterface::POST_METHOD)|| |
||
| 123 | ($method === RouteInterface::PUT_METHOD)) { |
||
| 124 | $headers["Content-Type"] = "application/json"; |
||
| 125 | $body = json_encode($payload); |
||
| 126 | } elseif ($method === RouteInterface::GET_METHOD) { |
||
| 127 | $endpoint = $endpoint . '?' . http_build_query($payload); |
||
| 128 | $body=''; |
||
| 129 | } |
||
| 130 | // Use Guzzle if found, else use Curl |
||
| 131 | if ($this->use_guzzle && class_exists('\\GuzzleHttp\\Client') && class_exists('\\GuzzleHttp\\Psr7\\Request')) { |
||
| 132 | |||
| 133 | $request = new \GuzzleHttp\Psr7\Request(strtoupper($method), $endpoint, $headers, $body); |
||
|
|
|||
| 134 | $client = new \GuzzleHttp\Client(['http_errors' => false]); |
||
| 135 | $response = $client->send($request); |
||
| 136 | return $response; |
||
| 137 | |||
| 138 | } else { |
||
| 139 | //open connection |
||
| 140 | |||
| 141 | $ch = \curl_init(); |
||
| 142 | // set url |
||
| 143 | \curl_setopt($ch, \CURLOPT_URL, $endpoint); |
||
| 144 | |||
| 145 | if ($method === RouteInterface::POST_METHOD || $method === RouteInterface::PUT_METHOD) { |
||
| 146 | ($method === RouteInterface:: POST_METHOD) && \curl_setopt($ch, \CURLOPT_POST, true); |
||
| 147 | ($method === RouteInterface ::PUT_METHOD) && \curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); |
||
| 148 | |||
| 149 | \curl_setopt($ch, \CURLOPT_POSTFIELDS, $body); |
||
| 150 | } |
||
| 151 | //flatten the headers |
||
| 152 | $flattened_headers = []; |
||
| 153 | while (list($key, $value) = each($headers)) { |
||
| 154 | $flattened_headers[] = $key . ": " . $value; |
||
| 155 | } |
||
| 156 | \curl_setopt($ch, \CURLOPT_HTTPHEADER, $flattened_headers); |
||
| 157 | \curl_setopt($ch, \CURLOPT_RETURNTRANSFER, 1); |
||
| 158 | \curl_setopt($ch, \CURLOPT_HEADER, 1); |
||
| 159 | |||
| 160 | $response = \curl_exec($ch); |
||
| 161 | |||
| 162 | // Then, after your \curl_exec call: |
||
| 163 | $header_size = \ curl_getinfo($ch, CURLINFO_HEADER_SIZE); |
||
| 164 | $header = substr($response, 0, $header_size); |
||
| 165 | $header = $this->headersFromLines(explode("\n", trim($header))); |
||
| 166 | $body = substr($response, $header_size); |
||
| 167 | $body = json_decode($body, true); |
||
| 168 | |||
| 169 | |||
| 170 | //close connection |
||
| 171 | \curl_close($ch); |
||
| 172 | |||
| 173 | return [ |
||
| 174 | Router ::HEADER_KEY => $header, Router::BODY_KEY => $body ]; |
||
| 175 | } |
||
| 176 | |||
| 177 | } |
||
| 178 | |||
| 271 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: