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 namespace Comodojo\Daemon\Socket; |
||
| 11 | class SocketTransport extends AbstractSocket implements TransportInterface { |
||
| 12 | |||
| 13 | private $aes = null; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * {@inheritdoc} |
||
| 17 | */ |
||
| 18 | 5 | public function performCall( |
|
| 19 | LoggerInterface $logger, |
||
| 20 | $data, |
||
| 21 | $content_type, |
||
| 22 | $encrypt=false |
||
| 23 | ) { |
||
| 24 | |||
| 25 | try { |
||
| 26 | |||
| 27 | 5 | $logger->debug("Connecting to socket"); |
|
| 28 | |||
| 29 | 5 | $this->connect(); |
|
| 30 | |||
| 31 | 5 | $logger->debug("Sending RPC data"); |
|
| 32 | |||
| 33 | 5 | $data = $this->can($data, $encrypt); |
|
| 34 | |||
| 35 | 5 | $response = $this->send($content_type, $data); |
|
| 36 | |||
| 37 | 5 | $this->close(); |
|
| 38 | |||
| 39 | 5 | $logger->debug("Decoding RPC response"); |
|
| 40 | |||
| 41 | 5 | $return = $this->uncan($response, $encrypt); |
|
| 42 | |||
| 43 | 5 | } catch (SocketException $se) { |
|
| 44 | |||
| 45 | $logger->error("Socket Transport error: ".$se->getMessage()); |
||
| 46 | |||
| 47 | throw $se; |
||
| 48 | |||
| 49 | } catch (RpcException $re) { |
||
| 50 | |||
| 51 | $logger->error("RPC Client error: ".$re->getMessage()); |
||
| 52 | |||
| 53 | throw $re; |
||
| 54 | |||
| 55 | } catch (Exception $e) { |
||
| 56 | |||
| 57 | $logger->critical("Generic Client error: ".$e->getMessage()); |
||
| 58 | |||
| 59 | throw $e; |
||
| 60 | |||
| 61 | } |
||
| 62 | |||
| 63 | 5 | return $return; |
|
| 64 | |||
| 65 | } |
||
| 66 | |||
| 67 | 5 | public static function create($handler, $read_buffer = null) { |
|
| 72 | |||
| 73 | 5 | public function connect() { |
|
| 74 | |||
| 75 | 5 | $this->socket = @socket_create( |
|
| 76 | 5 | $this->socket_domain, |
|
| 77 | 5 | $this->socket_type, |
|
| 78 | 5 | $this->socket_protocol |
|
| 79 | 5 | ); |
|
| 80 | |||
| 81 | 5 | if ( $this->socket === false ) { |
|
| 82 | $error = self::getSocketError(); |
||
| 83 | throw new SocketException("Socket unavailable: $error"); |
||
| 84 | } |
||
| 85 | |||
| 86 | 5 | $connect = @socket_connect( |
|
| 87 | 5 | $this->socket, |
|
| 88 | 5 | $this->socket_resource, |
|
| 89 | 5 | $this->socket_port |
|
| 90 | 5 | ); |
|
| 91 | |||
| 92 | 5 | if ( $connect === false ) { |
|
| 93 | $error = self::getSocketError($this->socket); |
||
| 94 | throw new SocketException("Cannot connect to socket: $error"); |
||
| 95 | } |
||
| 96 | |||
| 97 | 5 | $greeter = $this->readGreeter(); |
|
| 98 | |||
| 99 | 5 | if ( $greeter->status != 'connected' ) { |
|
|
|
|||
| 100 | throw new SocketException("Socket connect failed: ".$greeter->status); |
||
| 101 | } |
||
| 102 | |||
| 103 | 5 | if ( $greeter->version != self::VERSION ) { |
|
| 104 | throw new SocketException("Socket connect failed: socket interface version mismatch"); |
||
| 105 | } |
||
| 106 | |||
| 107 | 5 | return $this; |
|
| 108 | |||
| 109 | } |
||
| 110 | |||
| 111 | 5 | public function close() { |
|
| 116 | |||
| 117 | 5 | protected function send($content_type, $data) { |
|
| 132 | |||
| 133 | 5 | protected function write($content_type, $data) { |
|
| 145 | |||
| 146 | 5 | protected function read() { |
|
| 147 | |||
| 148 | 5 | $response = new Response(); |
|
| 149 | |||
| 150 | 5 | $datagram = $this->rawRead(); |
|
| 151 | |||
| 152 | 5 | if ( is_null($datagram) ) { |
|
| 153 | $response->status = false; |
||
| 154 | $response->message = "Server has gone away"; |
||
| 155 | 5 | } else if ( empty($datagram) ) { |
|
| 156 | $response->status = false; |
||
| 157 | $response->message = "No response received"; |
||
| 158 | } else { |
||
| 159 | 5 | $response->unserialize($datagram); |
|
| 160 | } |
||
| 161 | |||
| 162 | 5 | return $response; |
|
| 163 | |||
| 164 | } |
||
| 165 | |||
| 166 | 5 | protected function readGreeter() { |
|
| 185 | |||
| 186 | 5 | protected function rawRead() { |
|
| 187 | |||
| 188 | 5 | $datagram = ''; |
|
| 189 | |||
| 190 | 5 | View Code Duplication | while (true) { |
| 191 | 5 | $recv = @socket_read($this->socket, $this->read_buffer, PHP_NORMAL_READ); |
|
| 192 | 5 | if ( $recv === false ) break; |
|
| 193 | 5 | if ( $recv === 0 ) return null; |
|
| 194 | 5 | $datagram .= $recv; |
|
| 195 | 5 | if(strstr($recv, PHP_EOL)) break; |
|
| 196 | 5 | } |
|
| 197 | |||
| 198 | 5 | return trim($datagram); |
|
| 199 | |||
| 200 | } |
||
| 201 | |||
| 202 | 5 | private function can($data, $key) { |
|
| 203 | |||
| 204 | 5 | if ( !empty($key) && is_string($key) ) { |
|
| 205 | |||
| 206 | $this->aes = new AES(); |
||
| 207 | |||
| 208 | $this->aes->setKey($key); |
||
| 209 | |||
| 210 | $return = 'comodojo_encrypted_request-'.base64_encode( $this->aes->encrypt($data) ); |
||
| 211 | |||
| 212 | } else { |
||
| 213 | |||
| 214 | 5 | $return = $data; |
|
| 215 | |||
| 216 | } |
||
| 217 | |||
| 218 | 5 | return $return; |
|
| 219 | |||
| 220 | } |
||
| 221 | |||
| 222 | 5 | private function uncan($data, $key) { |
|
| 223 | |||
| 224 | 5 | if ( !empty($key) && is_string($key) ) { |
|
| 225 | |||
| 226 | if ( self::checkEncryptedResponseConsistency($data) === false ) throw new RpcException("Inconsistent encrypted response received"); |
||
| 227 | |||
| 228 | $return = $this->aes->decrypt(base64_decode(substr($data, 28))); |
||
| 229 | |||
| 230 | } else { |
||
| 231 | |||
| 232 | 5 | $return = $data; |
|
| 233 | |||
| 234 | } |
||
| 235 | |||
| 236 | 5 | return $return; |
|
| 237 | |||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Check if an encrypted envelope is consisent or not |
||
| 242 | * |
||
| 243 | * @param string $data |
||
| 244 | * |
||
| 245 | * @return bool |
||
| 246 | */ |
||
| 247 | private static function checkEncryptedResponseConsistency($data) { |
||
| 252 | |||
| 253 | } |
||
| 254 |
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.