Completed
Pull Request — master (#77)
by
unknown
22:32
created

SocksProxy::connect()   C

Complexity

Conditions 13
Paths 36

Size

Total Lines 57
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 57
rs 6.5962
c 0
b 0
f 0
cc 13
eloc 44
nc 36
nop 2

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
namespace TraderBot\Service;
4
5
use Fabiang\Xmpp\Options;
6
7
class SocksProxy extends SocketClient
8
{
9
    /** @var string */
10
    private $realAddress;
11
12
    /** @var string */
13
    private $username;
14
15
    /** @var string */
16
    private $password;
17
18
    public function __construct(Options $options)
19
    {
20
        $contextOptions = $options->getContextOptions();
21
        $contextOptions['ssl']['verify_peer_name'] = false;
22
        $addr = explode('@', ltrim('tcp://', $options->getSocksProxyAddress()));
23
        if (count($addr) > 1) {
24
            $socksAddr = $addr[1];
25
            list($this->username, $this->password) = explode(':', $addr[0]);
26
        } else {
27
            $socksAddr = $addr[0];
28
        }
29
        parent::__construct($socksAddr, $contextOptions);
30
        $this->realAddress = ltrim('tcp://', $options->getAddress());
31
    }
32
33
    public function connect($timeout = 30, $persistent = false)
34
    {
35
        parent::connect($timeout, $persistent);
36
        $methods = chr(0);
37
        if ($this->username) {
38
            $methods .= chr(2);
39
        }
40
        $this->write(chr(5).chr(strlen($methods)).$methods);
41
        $version = ord($this->read(1));
42
        $method = ord($this->read(1));
43
        if ($version !== 5) {
44
            throw new \Exception("Wrong SOCKS5 version: $version");
45
        }
46
        if ($method === 2) {
47
            $this->write(chr(1).chr(strlen($this->username)).$this->username.chr(strlen($this->password)).$this->password);
48
49
            $version = ord($this->read(1));
50
            if ($version !== 1) {
51
                throw new \Exception("Wrong authorized SOCKS version: $version");
52
            }
53
            $result = ord($this->read(1));
54
            if ($result !== 0) {
55
                throw new \Exception("Wrong authorization status: $result");
56
            }
57
        } elseif ($method !== 0) {
58
            throw new \Exception("Wrong method: $method");
59
        }
60
        list($address, $port) = explode(':', $this->realAddress);
61
        $payload  = pack('C5', 0x05, 0x01, 0x00, 0x03, strlen($address)).$address;
62
        $payload .= pack('n', $port);
63
        $this->write($payload);
64
65
        $version = ord($this->read(1));
66
        if ($version !== 5) {
67
            throw new \Exception("Wrong SOCKS5 version after CONNECT: $version");
68
        }
69
        $rep = ord($this->read(1));
70
        if ($rep !== 0) {
71
            throw new \Exception("Wrong SOCKS5 rep after CONNECT: $rep");
72
        }
73
        $rsv = ord($this->read(1));
74
        if ($rsv !== 0) {
75
            throw new \Exception("Wrong socks5 final RSV after CONNECT: $rsv");
76
        }
77
        switch (ord($this->read(1))) {
78
            case 1:
79
                $ip = inet_ntop($this->read(4));
0 ignored issues
show
Unused Code introduced by
$ip is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
80
                break;
81
            case 4:
82
                $ip = inet_ntop($this->read(16));
0 ignored issues
show
Unused Code introduced by
$ip is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
83
                break;
84
            case 3:
85
                $ip = $this->read(ord($this->read(1)));
0 ignored issues
show
Unused Code introduced by
$ip is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
86
                break;
87
        }
88
        $port = unpack('n', $this->read(2))[1];
0 ignored issues
show
Unused Code introduced by
$port is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
89
    }
90
}
91