Conditions | 13 |
Paths | 36 |
Total Lines | 57 |
Code Lines | 44 |
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 |
||
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)); |
||
|
|||
80 | break; |
||
81 | case 4: |
||
82 | $ip = inet_ntop($this->read(16)); |
||
83 | break; |
||
84 | case 3: |
||
85 | $ip = $this->read(ord($this->read(1))); |
||
86 | break; |
||
87 | } |
||
88 | $port = unpack('n', $this->read(2))[1]; |
||
89 | } |
||
90 | } |
||
91 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
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.