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:
Complex classes like Header often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Header, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 4 | class Header |
||
| 5 | { |
||
| 6 | const CMD_PROXY = 1; |
||
| 7 | const CMD_LOCAL = 0; |
||
| 8 | const UNSPECIFIED_PROTOCOL = "\x00"; |
||
| 9 | const TCP4 = "\x11"; |
||
| 10 | const UDP4 = "\x12"; |
||
| 11 | const TCP6 = "\x21"; |
||
| 12 | const UDP6 = "\x22"; |
||
| 13 | const USTREAM = "\x31"; |
||
| 14 | const USOCK = "\x32"; |
||
| 15 | |||
| 16 | // 12 bytes. |
||
| 17 | protected static $signatures = [ |
||
| 18 | 2 => "\x0D\x0A\x0D\x0A\x00\x0D\x0A\x51\x55\x49\x54\x0A", |
||
| 19 | 1 => "PROXY" |
||
| 20 | ]; |
||
| 21 | // 4 bits |
||
| 22 | public $version = 2; |
||
| 23 | // 4 bits |
||
| 24 | public $command = self::CMD_PROXY; |
||
| 25 | |||
| 26 | // 1 byte |
||
| 27 | public $protocol = self::TCP4; |
||
| 28 | |||
| 29 | protected static $lengths = [ |
||
| 30 | self::TCP4 => 12, |
||
| 31 | self::UDP4 => 12, |
||
| 32 | self::TCP6 => 36, |
||
| 33 | self::UDP6 => 36, |
||
| 34 | self::USTREAM => 216, |
||
| 35 | self::USOCK => 216, |
||
| 36 | ]; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var string The address of the client. |
||
| 40 | */ |
||
| 41 | public $sourceAddress; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string The address to which the client connected. |
||
| 45 | */ |
||
| 46 | public $targetAddress; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var int The port of the client |
||
| 50 | */ |
||
| 51 | public $sourcePort; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var int The port to which the client connected. |
||
| 55 | */ |
||
| 56 | public $targetPort; |
||
| 57 | |||
| 58 | |||
| 59 | 1 | protected function getProtocol() |
|
| 72 | /** |
||
| 73 | * @return uint16_t |
||
| 74 | */ |
||
| 75 | 1 | protected function getAddressLength() |
|
| 82 | |||
| 83 | 1 | View Code Duplication | protected function encodeAddress($address, $protocol) { |
| 102 | |||
| 103 | 1 | View Code Duplication | protected static function decodeAddress($version, $address, $protocol) |
| 124 | |||
| 125 | /** |
||
| 126 | * @return string |
||
| 127 | * @throws \Exception |
||
| 128 | */ |
||
| 129 | 1 | protected function getAddresses() |
|
| 133 | |||
| 134 | 1 | View Code Duplication | protected function encodePort($port, $protocol) { |
| 154 | |||
| 155 | /** |
||
| 156 | * @return string |
||
| 157 | * @throws \Exception |
||
| 158 | */ |
||
| 159 | 1 | protected function getPorts() |
|
| 163 | |||
| 164 | /** |
||
| 165 | * @return string |
||
| 166 | */ |
||
| 167 | 1 | protected function getSignature() |
|
| 171 | |||
| 172 | /** |
||
| 173 | * Constructs the header by concatenating all relevant fields. |
||
| 174 | * @return string |
||
| 175 | */ |
||
| 176 | 1 | public function constructProxyHeader() { |
|
| 187 | |||
| 188 | 1 | public function __toString() |
|
| 192 | |||
| 193 | /** |
||
| 194 | * This function creates the forwarding header. This header should be sent over the upstream connection as soon as |
||
| 195 | * it is established. |
||
| 196 | * @param string $sourceAddress |
||
| 197 | * @param int $sourcePort |
||
| 198 | * @param string $targetAddress |
||
| 199 | * @param int $targetPort |
||
| 200 | * @return self |
||
| 201 | * @throws \Exception |
||
| 202 | */ |
||
| 203 | 2 | public static function createForward4($sourceAddress, $sourcePort, $targetAddress, $targetPort, $version = 2) { |
|
| 204 | 2 | $result = new static(); |
|
| 205 | 2 | $result->version = $version; |
|
| 206 | 2 | $result->sourceAddress = $sourceAddress; |
|
| 207 | 2 | $result->targetPort = $targetPort; |
|
| 208 | 2 | $result->targetAddress = $targetAddress; |
|
| 209 | 2 | $result->sourcePort = $sourcePort; |
|
| 210 | 2 | return $result; |
|
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param string $data |
||
| 215 | * @return Header|null |
||
| 216 | */ |
||
| 217 | 1 | public static function parseHeader(&$data) |
|
| 239 | |||
| 240 | protected static function parseVersion1($data) |
||
| 254 | |||
| 255 | 1 | protected static function parseVersion2($data) |
|
| 279 | |||
| 280 | } |
||
| 281 |