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( |
|
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 | ); |
||
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 | ); |
||
91 | |||
92 | 5 | View Code Duplication | 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() { |
|
165 | |||
166 | 5 | protected function readGreeter() { |
|
185 | |||
186 | 5 | protected function rawRead() { |
|
201 | |||
202 | 5 | private function can($data, $key) { |
|
221 | |||
222 | 5 | private function uncan($data, $key) { |
|
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 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.