Conditions | 6 |
Paths | 8 |
Total Lines | 54 |
Code Lines | 30 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
39 | 5 | public function sendRequest(Interfaces\HttpRequestInterface $http_request, string $action, string $query = null, array $options = array()): \stdClass |
|
40 | { |
||
41 | // Url construction |
||
42 | 5 | $url = $this->base_api_url . $action; |
|
43 | |||
44 | // Parameters |
||
45 | 5 | $params = []; |
|
46 | 5 | $params['api_key'] = $this->api_key; |
|
47 | 5 | if (!is_null($query)) |
|
48 | { |
||
49 | 1 | $params['query'] = $query; |
|
50 | } |
||
51 | |||
52 | 5 | $params = array_merge($params, $options); |
|
53 | |||
54 | // URL with paramters construction |
||
55 | 5 | $url = $url . '?' . http_build_query($params); |
|
56 | |||
57 | 5 | $http_request->setUrl($url); |
|
58 | 5 | $http_request->setOption(CURLOPT_HEADER, 0); |
|
59 | 5 | $http_request->setOption(CURLOPT_RETURNTRANSFER, true); |
|
|
|||
60 | 5 | $http_request->setOption(CURLOPT_MAXREDIRS, 10); |
|
61 | 5 | $http_request->setOption(CURLOPT_ENCODING, ""); |
|
62 | 5 | $http_request->setOption(CURLOPT_TIMEOUT, 30); |
|
63 | 5 | $http_request->setOption(CURLINFO_HEADER_OUT, true); // To gets header in curl_getinfo() |
|
64 | |||
65 | 5 | $result = $http_request->execute(); |
|
66 | |||
67 | 4 | $http_code = $http_request->getInfo(CURLINFO_HTTP_CODE); |
|
68 | |||
69 | 4 | if ($http_code !== 200) |
|
70 | { |
||
71 | 2 | if ($http_code == 429) |
|
72 | { |
||
73 | 1 | $message = new \stdClass(); |
|
74 | 1 | $message->message = 'Request rate limit exceeded'; |
|
75 | 1 | $header_out = $http_request->getInfo(CURLINFO_HEADER_OUT); |
|
76 | 1 | $message->headers = var_export($header_out, true); |
|
77 | |||
78 | 1 | throw new \Exception(json_encode($message), 1006); |
|
79 | } |
||
80 | 1 | throw new \Exception('Incorrect HTTP Code (' . $http_code . ') response : ' . var_export($http_request->getInfo(), true), 1005); |
|
81 | } |
||
82 | |||
83 | // cUrl closing |
||
84 | 2 | $http_request->close(); |
|
85 | |||
86 | 2 | $response = json_decode($result); |
|
87 | 2 | if (is_null($response) || $response === false) |
|
88 | { |
||
89 | 1 | throw new \Exception('Search failed : ' . var_export($result, true), 2001); |
|
90 | } |
||
91 | 1 | return $response; |
|
92 | } |
||
93 | |||
180 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: