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 |
||
| 20 | abstract class SecureSocket implements SocketStream |
||
| 21 | { |
||
| 22 | |||
| 23 | /** |
||
| 24 | * The socket handle |
||
| 25 | * |
||
| 26 | * @var resource |
||
| 27 | */ |
||
| 28 | protected $handle; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * The socket endpoint |
||
| 32 | * |
||
| 33 | * @var Endpoint |
||
| 34 | */ |
||
| 35 | protected $endpoint; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * The stream context |
||
| 39 | * |
||
| 40 | * @var resource |
||
| 41 | */ |
||
| 42 | private $streamContext; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Create a new socket |
||
| 46 | * |
||
| 47 | * @param Endpoint $endpoint |
||
| 48 | * The endpoint for the socket |
||
| 49 | */ |
||
| 50 | 12 | public function __construct(Endpoint $endpoint) |
|
| 55 | |||
| 56 | /** |
||
| 57 | * |
||
| 58 | * {@inheritdoc} |
||
| 59 | * @see \Generics\Streams\InputStream::read() |
||
| 60 | */ |
||
| 61 | 9 | public function read($length = 1, $offset = null): string |
|
| 65 | |||
| 66 | /** |
||
| 67 | * |
||
| 68 | * {@inheritdoc} |
||
| 69 | * @see \Generics\Streams\Stream::isOpen() |
||
| 70 | */ |
||
| 71 | public function isOpen(): bool |
||
| 75 | |||
| 76 | /** |
||
| 77 | * |
||
| 78 | * {@inheritdoc} |
||
| 79 | * @see \Generics\Streams\OutputStream::flush() |
||
| 80 | */ |
||
| 81 | public function flush() |
||
| 85 | |||
| 86 | /** |
||
| 87 | * |
||
| 88 | * {@inheritdoc} |
||
| 89 | * @see \Generics\Streams\Stream::ready() |
||
| 90 | */ |
||
| 91 | 10 | View Code Duplication | public function ready(): bool |
| 119 | |||
| 120 | /** |
||
| 121 | * |
||
| 122 | * {@inheritdoc} |
||
| 123 | * @see Countable::count() |
||
| 124 | */ |
||
| 125 | public function count() |
||
| 136 | |||
| 137 | /** |
||
| 138 | * |
||
| 139 | * {@inheritdoc} |
||
| 140 | * @see \Generics\Resettable::reset() |
||
| 141 | */ |
||
| 142 | View Code Duplication | public function reset() |
|
| 151 | |||
| 152 | /** |
||
| 153 | * |
||
| 154 | * {@inheritdoc} |
||
| 155 | * @see \Generics\Streams\Stream::close() |
||
| 156 | */ |
||
| 157 | 6 | public function close() |
|
| 164 | |||
| 165 | /** |
||
| 166 | * |
||
| 167 | * {@inheritdoc} |
||
| 168 | * @see \Generics\Streams\OutputStream::write() |
||
| 169 | */ |
||
| 170 | 11 | public function write($buffer) |
|
| 188 | |||
| 189 | /** |
||
| 190 | * |
||
| 191 | * {@inheritdoc} |
||
| 192 | * @see \Generics\Streams\OutputStream::isWriteable() |
||
| 193 | */ |
||
| 194 | 11 | View Code Duplication | public function isWriteable(): bool |
| 222 | |||
| 223 | 12 | private function open() |
|
| 240 | |||
| 241 | 12 | private function prepareStreamContext() |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Retrieve end point object |
||
| 262 | * |
||
| 263 | * @return Endpoint |
||
| 264 | */ |
||
| 265 | 11 | public function getEndPoint(): Endpoint |
|
| 269 | } |
||
| 270 |