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 |
||
| 19 | abstract class Socket implements SocketStream |
||
| 20 | { |
||
| 21 | |||
| 22 | /** |
||
| 23 | * The socket handle |
||
| 24 | * |
||
| 25 | * @var resource |
||
| 26 | */ |
||
| 27 | protected $handle; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The socket endpoint |
||
| 31 | * |
||
| 32 | * @var Endpoint |
||
| 33 | */ |
||
| 34 | protected $endpoint; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Create a new socket |
||
| 38 | * |
||
| 39 | * @param Endpoint $endpoint |
||
| 40 | * The endpoint for the socket |
||
| 41 | */ |
||
| 42 | 7 | public function __construct(Endpoint $endpoint) |
|
| 47 | |||
| 48 | /** |
||
| 49 | * Opens a socket |
||
| 50 | * |
||
| 51 | * @throws SocketException |
||
| 52 | */ |
||
| 53 | 7 | private function open() |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Clean up |
||
| 65 | */ |
||
| 66 | 7 | public function __destruct() |
|
| 70 | |||
| 71 | /** |
||
| 72 | * {@inheritDoc} |
||
| 73 | * @see \Generics\Streams\Stream::close() |
||
| 74 | */ |
||
| 75 | 5 | public function close() |
|
| 82 | |||
| 83 | /** |
||
| 84 | * {@inheritDoc} |
||
| 85 | * @see \Generics\Streams\Stream::ready() |
||
| 86 | */ |
||
| 87 | 4 | View Code Duplication | public function ready() |
| 88 | { |
||
| 89 | 4 | if (! is_resource($this->handle)) { |
|
| 90 | 1 | return false; |
|
| 91 | } |
||
| 92 | |||
| 93 | $read = array( |
||
| 94 | 3 | $this->handle |
|
| 95 | ); |
||
| 96 | 3 | $write = null; |
|
| 97 | 3 | $except = null; |
|
| 98 | |||
| 99 | 3 | $num = @socket_select($read, $write, $except, 0); |
|
| 100 | |||
| 101 | 3 | if ($num === false) { |
|
| 102 | $code = socket_last_error($this->handle); |
||
| 103 | throw new SocketException(socket_strerror($code), array(), $code); |
||
| 104 | } |
||
| 105 | |||
| 106 | 3 | if ($num < 1) { |
|
| 107 | 3 | return false; |
|
| 108 | } |
||
| 109 | |||
| 110 | 2 | if (! in_array($this->handle, $read)) { |
|
| 111 | return false; |
||
| 112 | } |
||
| 113 | |||
| 114 | 2 | return true; |
|
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * {@inheritDoc} |
||
| 119 | * @see \Generics\Streams\OutputStream::isWriteable() |
||
| 120 | */ |
||
| 121 | 1 | View Code Duplication | public function isWriteable() |
| 150 | |||
| 151 | /** |
||
| 152 | * {@inheritDoc} |
||
| 153 | * @see \Countable::count() |
||
| 154 | */ |
||
| 155 | public function count() |
||
| 159 | |||
| 160 | /** |
||
| 161 | * {@inheritDoc} |
||
| 162 | * @see \Generics\Streams\InputStream::read() |
||
| 163 | */ |
||
| 164 | 2 | public function read($length = 1, $offset = null) |
|
| 180 | |||
| 181 | /** |
||
| 182 | * {@inheritDoc} |
||
| 183 | * @see \Generics\Streams\OutputStream::write() |
||
| 184 | */ |
||
| 185 | 3 | public function write($buffer) |
|
| 199 | |||
| 200 | /** |
||
| 201 | * Get the socket endpoint |
||
| 202 | * |
||
| 203 | * @return \Generics\Socket\Endpoint |
||
| 204 | */ |
||
| 205 | 3 | public function getEndpoint() |
|
| 209 | |||
| 210 | /** |
||
| 211 | * {@inheritDoc} |
||
| 212 | * @see \Generics\Streams\OutputStream::flush() |
||
| 213 | */ |
||
| 214 | public function flush() |
||
| 218 | |||
| 219 | /** |
||
| 220 | * {@inheritDoc} |
||
| 221 | * @see \Generics\Streams\Stream::isOpen() |
||
| 222 | */ |
||
| 223 | 1 | public function isOpen() |
|
| 227 | |||
| 228 | /** |
||
| 229 | * {@inheritDoc} |
||
| 230 | * @see \Generics\Resettable::reset() |
||
| 231 | */ |
||
| 232 | public function reset() |
||
| 243 | } |
||
| 244 |