Complex classes like Client 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 Client, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class Client extends AbstractClient implements ClientInterface |
||
| 26 | { |
||
| 27 | protected $socket; |
||
| 28 | protected $chunk_size; |
||
| 29 | protected $verify_peer_name; |
||
| 30 | |||
| 31 | public function __construct(array $config, ObjectSpec $objectSpec = null) |
||
| 32 | { |
||
| 33 | parent::__construct($config, $objectSpec); |
||
| 34 | |||
| 35 | if (!empty($config['chunk_size'])) { |
||
| 36 | $this->chunk_size = (int) $config['chunk_size']; |
||
| 37 | } else { |
||
| 38 | $this->chunk_size = 1024; |
||
| 39 | } |
||
| 40 | |||
| 41 | if (isset($config['verify_peer_name'])) { |
||
| 42 | $this->verify_peer_name = (bool) $config['verify_peer_name']; |
||
| 43 | } else { |
||
| 44 | $this->verify_peer_name = true; |
||
| 45 | } |
||
| 46 | |||
| 47 | if ($this->port === false) { |
||
| 48 | // if not set, default port is 700 |
||
| 49 | $this->port = 700; |
||
| 50 | } |
||
| 51 | } |
||
| 52 | |||
| 53 | public function __destruct() |
||
| 54 | { |
||
| 55 | $this->close(); |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Setup context in case of ssl connection |
||
| 60 | * |
||
| 61 | * @return resource|null |
||
| 62 | */ |
||
| 63 | private function setupContext() |
||
| 64 | { |
||
| 65 | if (!$this->ssl) { |
||
| 66 | return null; |
||
| 67 | } |
||
| 68 | |||
| 69 | $context = stream_context_create(); |
||
| 70 | |||
| 71 | $options_array = [ |
||
| 72 | 'verify_peer' => false, |
||
| 73 | 'verify_peer_name' => $this->verify_peer_name, |
||
| 74 | 'allow_self_signed' => true, |
||
| 75 | 'local_cert' => $this->local_cert, |
||
| 76 | 'passphrase' => $this->passphrase, |
||
| 77 | 'cafile' => $this->ca_cert, |
||
| 78 | 'local_pk' => $this->pk_cert, |
||
| 79 | ]; |
||
| 80 | |||
| 81 | // filter out empty user provided values |
||
| 82 | $options_array = array_filter( |
||
| 83 | $options_array, |
||
| 84 | function ($var) { |
||
| 85 | return !is_null($var); |
||
| 86 | } |
||
| 87 | ); |
||
| 88 | |||
| 89 | $options = ['ssl' => $options_array]; |
||
| 90 | |||
| 91 | // stream_context_set_option accepts array of options in form of $arr['wrapper']['option'] = value |
||
| 92 | stream_context_set_option($context, $options); |
||
| 93 | |||
| 94 | return $context; |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Setup connection socket |
||
| 99 | * |
||
| 100 | * @param resource|null $context SSL context or null in case of tcp connection |
||
| 101 | * |
||
| 102 | * @throws Exception on socket errors |
||
| 103 | */ |
||
| 104 | private function setupSocket($context = null) |
||
| 105 | { |
||
| 106 | $proto = $this->ssl ? 'ssl' : 'tcp'; |
||
| 107 | $target = sprintf('%s://%s:%d', $proto, $this->host, $this->port); |
||
| 108 | |||
| 109 | $errno = 0; |
||
| 110 | $errstr = ''; |
||
| 111 | |||
| 112 | $this->socket = @stream_socket_client($target, $errno, $errstr, $this->connect_timeout, STREAM_CLIENT_CONNECT, $context); |
||
| 113 | |||
| 114 | if ($this->socket === false) { |
||
| 115 | if ($errno === 0) { |
||
| 116 | // Socket initialization may fail, before system call connect() |
||
| 117 | // so the $errno isn't populated. |
||
| 118 | // see https://www.php.net/manual/en/function.stream-socket-client.php#refsect1-function.stream-socket-client-errors |
||
| 119 | throw new Exception("problem initializing the socket"); |
||
| 120 | } |
||
| 121 | throw new Exception($errstr, $errno); |
||
| 122 | } |
||
| 123 | |||
| 124 | // set stream time out |
||
| 125 | if (!stream_set_timeout($this->socket, $this->timeout)) { |
||
| 126 | throw new Exception('unable to set stream timeout'); |
||
| 127 | } |
||
| 128 | |||
| 129 | // set to non-blocking |
||
| 130 | if (!stream_set_blocking($this->socket, 0)) { |
||
| 131 | throw new Exception('unable to set blocking'); |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * {@inheritdoc} |
||
| 137 | * |
||
| 138 | * @see \AfriCC\EPP\ClientInterface::connect() |
||
| 139 | */ |
||
| 140 | public function connect($newPassword = false) |
||
| 141 | { |
||
| 142 | $context = $this->setupContext(); |
||
| 143 | $this->setupSocket($context); |
||
| 144 | |||
| 145 | // get greeting |
||
| 146 | $greeting = $this->getFrame(); |
||
| 147 | |||
| 148 | // login |
||
| 149 | $this->login($newPassword); |
||
| 150 | |||
| 151 | // return greeting |
||
| 152 | return $greeting; |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Closes a previously opened EPP connection |
||
| 157 | */ |
||
| 158 | public function close() |
||
| 159 | { |
||
| 160 | if ($this->active()) { |
||
| 161 | // send logout frame |
||
| 162 | $this->request(new LogoutCommand($this->objectSpec)); |
||
| 163 | |||
| 164 | return fclose($this->socket); |
||
| 165 | } |
||
| 166 | |||
| 167 | return false; |
||
| 168 | } |
||
| 169 | |||
| 170 | public function getFrame() |
||
| 171 | { |
||
| 172 | $hard_time_limit = time() + $this->timeout + 2; |
||
| 173 | $header = ''; |
||
|
|
|||
| 174 | do { |
||
| 175 | $header = $this->recv(4); |
||
| 176 | } while (empty($header) && (time()<$hard_time_limit)); |
||
| 177 | |||
| 178 | if (time() >= $hard_time_limit) { |
||
| 179 | throw new Exception('Timeout while reading header from EPP Server'); |
||
| 180 | } |
||
| 181 | |||
| 182 | // Unpack first 4 bytes which is our length |
||
| 183 | $unpacked = unpack('N', $header); |
||
| 184 | $length = $unpacked[1]; |
||
| 185 | |||
| 186 | if ($length < 5) { |
||
| 187 | throw new Exception(sprintf('Got a bad frame header length of %d bytes from peer', $length)); |
||
| 188 | } else { |
||
| 189 | $length -= 4; |
||
| 190 | |||
| 191 | return $this->recv($length); |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | public function sendFrame(FrameInterface $frame) |
||
| 196 | { |
||
| 197 | $buffer = (string) $frame; |
||
| 198 | $header = pack('N', mb_strlen($buffer, 'ASCII') + 4); |
||
| 199 | |||
| 200 | return $this->send($header . $buffer); |
||
| 201 | } |
||
| 202 | |||
| 203 | protected function log($message, $color = '0;32') |
||
| 210 | |||
| 211 | /** |
||
| 212 | * check if socket is still active |
||
| 213 | * |
||
| 214 | * @return bool |
||
| 215 | */ |
||
| 216 | private function active() |
||
| 220 | |||
| 221 | /** |
||
| 222 | * receive socket data |
||
| 223 | * |
||
| 224 | * @param int $length |
||
| 225 | * |
||
| 226 | * @throws Exception |
||
| 227 | * |
||
| 228 | * @return string |
||
| 229 | */ |
||
| 230 | private function recv($length) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * send data to socket |
||
| 272 | * |
||
| 273 | * @param string $buffer |
||
| 274 | */ |
||
| 275 | private function send($buffer) |
||
| 327 | } |
||
| 328 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.