Conditions | 6 |
Paths | 16 |
Total Lines | 53 |
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 |
||
26 | public function send($uri, array $params = []) |
||
27 | { |
||
28 | if(!$this->nonce) { |
||
29 | |||
30 | $nonce = explode(' ', microtime()); |
||
31 | $this->nonce = + $nonce[1].($nonce[0] * 1000000); |
||
|
|||
32 | $this->nonce = substr($this->nonce,5); |
||
33 | |||
34 | $this->nonce = explode(' ', microtime())[1]; |
||
35 | // $this->nonce = (int) 10000 * microtime(true); |
||
36 | } else { |
||
37 | $this->nonce ++ ; |
||
38 | } |
||
39 | |||
40 | |||
41 | $params['nonce'] = $this->nonce; |
||
42 | $params['method'] = $uri; |
||
43 | |||
44 | // generate the POST data string |
||
45 | $post_data = http_build_query($params, '', '&'); |
||
46 | $sign = hash_hmac('sha512', $post_data, $this->config->getSecret()); |
||
47 | |||
48 | |||
49 | |||
50 | $headers = [ |
||
51 | 'Sign: '.$sign, |
||
52 | 'Key: '.$this->config->getKey(), |
||
53 | ]; |
||
54 | |||
55 | |||
56 | static $ch = null; |
||
57 | if (is_null($ch)) { |
||
58 | $ch = curl_init(); |
||
59 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||
60 | curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; BTCE PHP client; '.php_uname('s').'; PHP/'.phpversion().')'); |
||
61 | } |
||
62 | curl_setopt($ch, CURLOPT_URL, 'https://wex.nz/tapi/'); |
||
63 | curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); |
||
64 | curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
||
65 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); |
||
66 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); |
||
67 | |||
68 | // run the query |
||
69 | $res = curl_exec($ch); |
||
70 | if ($res === false) throw new \Exception('Could not get reply: '.curl_error($ch)); |
||
71 | $dec = json_decode($res, true); |
||
72 | if (!$dec) throw new \Exception('Invalid data received, please make sure connection is working and requested API exists'); |
||
73 | if($dec['success'] == 0) { |
||
74 | throw new \Exception($dec['error']); |
||
75 | } |
||
76 | return $dec['return']; |
||
77 | |||
78 | } |
||
79 | |||
192 | } |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.