Completed
Push — master ( 0adda3...dfe1d7 )
by Derek Stephen
02:03 queued 53s
created

BtcE   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 151
Duplicated Lines 11.92 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 96.55%

Importance

Changes 0
Metric Value
wmc 15
lcom 2
cbo 4
dl 18
loc 151
ccs 56
cts 58
cp 0.9655
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setClient() 0 4 1
A buyOrder() 9 9 1
A sellOrder() 9 9 1
A getInfo() 0 4 1
A getTicker() 0 4 1
A getOrders() 0 6 1
A getTransactionHistory() 0 4 1
A getTradeHistory() 0 4 1
A cancelOrder() 0 6 1
B send() 0 52 6

How to fix   Duplicated Code   

Duplicated Code

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
2
3
namespace Del\Exchange;
4
5
use GuzzleHttp\Client;
6
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);
0 ignored issues
show
Documentation Bug introduced by
The property $nonce was declared of type boolean, but +$nonce[1] . $nonce[0] * 1000000 is of type string. Maybe add a type cast?

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.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
24 7
            $this->nonce = substr($this->nonce,5);
0 ignored issues
show
Documentation Bug introduced by
The property $nonce was declared of type boolean, but substr($this->nonce, 5) is of type string. Maybe add a type cast?

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.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
25
26 7
            $this->nonce = explode(' ', microtime())[1];
27
//            $this->nonce = (int) 10000 * microtime(true);
0 ignored issues
show
Unused Code Comprehensibility introduced by
45% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
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()
75
    {
76 9
        $this->client = new Client(['base_uri' => 'https://btc-e.com/tapi/']);
77 9
    }
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)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86
    {
87 1
        return $this->send('Trade',[
88 1
            'pair' => 'btc_usd',
89 1
            'type' => 'buy',
90 1
            'rate' => $price,
91 1
            'amount' => $btc
92
        ]);
93
    }
94
95
    /**
96
     * @param $btc
97
     * @param $price
98
     * @return mixed
99
     */
100 1 View Code Duplication
    public function sellOrder($btc, $price)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
    {
102 1
        return $this->send('Trade',[
103 1
            'pair' => 'btc_usd',
104 1
            'type' => 'sell',
105 1
            'rate' => $price,
106 1
            'amount' => $btc
107
        ]);
108
    }
109
110
    /**
111
     * @return mixed
112
     * @throws \Exception
113
     */
114 1
    public function getInfo()
115
    {
116 1
        return $this->send('getInfo');
117
    }
118
119
    /**
120
     * @return mixed
121
     */
122 1
    public function getTicker()
123
    {
124 1
        return json_decode($this->client->get('https://btc-e.com/api/3/ticker/btc_usd')->getBody()->getContents(), true);
125
    }
126
127
128
129 1
    public function getOrders()
130
    {
131 1
        return $this->send('ActiveOrders',[
132 1
            'pair' => 'btc_usd'
133
        ]);
134
    }
135
136
137 1
    public function getTransactionHistory()
138
    {
139 1
        return $this->send('TransHistory',[]);
140
    }
141
142 1
    public function getTradeHistory()
143
    {
144 1
        return $this->send('TradeHistory',[]);
145
    }
146
147
148
149 1
    public function cancelOrder($order_id)
150
    {
151 1
        return $this->send('CancelOrder',[
152 1
            'order_id' => $order_id
153
        ]);
154
    }
155
156
157
}