Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
7 | class BtcE extends ExchangeAbstract |
||
8 | { |
||
9 | private $nonce = false; |
||
10 | |||
11 | |||
12 | /** |
||
13 | * @param $uri |
||
14 | * @param array $params |
||
15 | * @return mixed |
||
16 | * @throws \Exception |
||
17 | */ |
||
18 | 7 | public function send($uri, array $params = []) |
|
19 | { |
||
20 | 7 | if(!$this->nonce) { |
|
21 | |||
22 | 7 | $nonce = explode(' ', microtime()); |
|
23 | 7 | $this->nonce = + $nonce[1].($nonce[0] * 1000000); |
|
|
|||
24 | 7 | $this->nonce = substr($this->nonce,5); |
|
25 | |||
26 | 7 | $this->nonce = explode(' ', microtime())[1]; |
|
27 | // $this->nonce = (int) 10000 * microtime(true); |
||
28 | } else { |
||
29 | $this->nonce ++ ; |
||
30 | } |
||
31 | |||
32 | |||
33 | 7 | $params['nonce'] = $this->nonce; |
|
34 | 7 | $params['method'] = $uri; |
|
35 | |||
36 | // generate the POST data string |
||
37 | 7 | $post_data = http_build_query($params, '', '&'); |
|
38 | 7 | $sign = hash_hmac('sha512', $post_data, $this->config->getSecret()); |
|
39 | |||
40 | |||
41 | |||
42 | $headers = [ |
||
43 | 7 | 'Sign: '.$sign, |
|
44 | 7 | 'Key: '.$this->config->getKey(), |
|
45 | ]; |
||
46 | |||
47 | |||
48 | 7 | static $ch = null; |
|
49 | 7 | if (is_null($ch)) { |
|
50 | 1 | $ch = curl_init(); |
|
51 | 1 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
52 | 1 | curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; BTCE PHP client; '.php_uname('s').'; PHP/'.phpversion().')'); |
|
53 | } |
||
54 | 7 | curl_setopt($ch, CURLOPT_URL, 'https://wex.nz/tapi/'); |
|
55 | 7 | curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); |
|
56 | 7 | curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
|
57 | 7 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); |
|
58 | |||
59 | // run the query |
||
60 | 7 | $res = curl_exec($ch); |
|
61 | 7 | if ($res === false) throw new \Exception('Could not get reply: '.curl_error($ch)); |
|
62 | 7 | $dec = json_decode($res, true); |
|
63 | 7 | if (!$dec) throw new \Exception('Invalid data received, please make sure connection is working and requested API exists'); |
|
64 | 7 | if($dec['success'] == 0) { |
|
65 | 7 | throw new \Exception($dec['error']); |
|
66 | } |
||
67 | return $dec['return']; |
||
68 | |||
69 | } |
||
70 | |||
71 | /** |
||
72 | * |
||
73 | */ |
||
74 | 9 | public function setClient() |
|
78 | |||
79 | /** |
||
80 | * @param $btc |
||
81 | * @param $price |
||
82 | * @return mixed |
||
83 | * @throws \Exception |
||
84 | */ |
||
85 | 1 | View Code Duplication | public function buyOrder($btc, $price) |
94 | |||
95 | /** |
||
96 | * @param $btc |
||
97 | * @param $price |
||
98 | * @return mixed |
||
99 | */ |
||
100 | 1 | View Code Duplication | public function sellOrder($btc, $price) |
109 | |||
110 | /** |
||
111 | * @return mixed |
||
112 | * @throws \Exception |
||
113 | */ |
||
114 | 1 | public function getInfo() |
|
118 | |||
119 | /** |
||
120 | * @return mixed |
||
121 | */ |
||
122 | 1 | public function getTicker() |
|
126 | |||
127 | |||
128 | |||
129 | 1 | public function getOrders() |
|
135 | |||
136 | |||
137 | 1 | public function getTransactionHistory() |
|
141 | |||
142 | 1 | public function getTradeHistory() |
|
146 | |||
147 | |||
148 | |||
149 | 1 | public function cancelOrder($order_id) |
|
155 | |||
156 | |||
157 | } |
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.