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 | 16 | public function __construct(Endpoint $endpoint) |
|
| 47 | |||
| 48 | /** |
||
| 49 | * Opens a socket |
||
| 50 | * |
||
| 51 | * @throws SocketException |
||
| 52 | */ |
||
| 53 | 16 | private function open() |
|
| 62 | |||
| 63 | /** |
||
| 64 | * Clean up |
||
| 65 | */ |
||
| 66 | 16 | public function __destruct() |
|
| 70 | |||
| 71 | /** |
||
| 72 | * |
||
| 73 | * {@inheritdoc} |
||
| 74 | * @see \Generics\Streams\Stream::close() |
||
| 75 | */ |
||
| 76 | 16 | public function close() |
|
| 83 | |||
| 84 | /** |
||
| 85 | * |
||
| 86 | * {@inheritdoc} |
||
| 87 | * @see \Generics\Streams\Stream::ready() |
||
| 88 | */ |
||
| 89 | 11 | View Code Duplication | public function ready(): bool |
| 118 | |||
| 119 | /** |
||
| 120 | * |
||
| 121 | * {@inheritdoc} |
||
| 122 | * @see \Generics\Streams\OutputStream::isWriteable() |
||
| 123 | */ |
||
| 124 | 1 | View Code Duplication | public function isWriteable(): bool |
| 153 | |||
| 154 | /** |
||
| 155 | * |
||
| 156 | * {@inheritdoc} |
||
| 157 | * @see \Countable::count() |
||
| 158 | */ |
||
| 159 | public function count() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * |
||
| 166 | * {@inheritdoc} |
||
| 167 | * @see \Generics\Streams\InputStream::read() |
||
| 168 | */ |
||
| 169 | 9 | public function read($length = 1, $offset = null): string |
|
| 185 | |||
| 186 | /** |
||
| 187 | * |
||
| 188 | * {@inheritdoc} |
||
| 189 | * @see \Generics\Streams\OutputStream::write() |
||
| 190 | */ |
||
| 191 | 10 | public function write($buffer) |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Get the socket endpoint |
||
| 208 | * |
||
| 209 | * @return \Generics\Socket\Endpoint |
||
| 210 | */ |
||
| 211 | 11 | public function getEndpoint(): Endpoint |
|
| 215 | |||
| 216 | /** |
||
| 217 | * |
||
| 218 | * {@inheritdoc} |
||
| 219 | * @see \Generics\Streams\OutputStream::flush() |
||
| 220 | */ |
||
| 221 | public function flush() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * |
||
| 228 | * {@inheritdoc} |
||
| 229 | * @see \Generics\Streams\Stream::isOpen() |
||
| 230 | */ |
||
| 231 | 1 | public function isOpen(): bool |
|
| 235 | |||
| 236 | /** |
||
| 237 | * |
||
| 238 | * {@inheritdoc} |
||
| 239 | * @see \Generics\Resettable::reset() |
||
| 240 | */ |
||
| 241 | View Code Duplication | public function reset() |
|
| 250 | } |
||
| 251 |