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 |
||
| 9 | class StreamIO implements IOInterface |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var resource|null |
||
| 13 | */ |
||
| 14 | private $stream; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var string |
||
| 18 | */ |
||
| 19 | private $buffer; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var int |
||
| 23 | */ |
||
| 24 | private $readAheadSize; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * {@inheritdoc} |
||
| 28 | */ |
||
| 29 | 25 | public function open($protocol, $host, $port, array $parameters = []) |
|
| 54 | |||
| 55 | /** |
||
| 56 | * @param float $timeout |
||
| 57 | */ |
||
| 58 | 25 | private function setReadingTimeout($timeout) |
|
| 63 | |||
| 64 | /** |
||
| 65 | * @param int $size |
||
| 66 | */ |
||
| 67 | 25 | private function setReadAheadSize($size) |
|
| 71 | |||
| 72 | /** |
||
| 73 | * @param array $parameters |
||
| 74 | * |
||
| 75 | * @return resource |
||
| 76 | */ |
||
| 77 | 25 | private function createStreamContext(array $parameters) |
|
| 100 | |||
| 101 | /** |
||
| 102 | * @return $this |
||
| 103 | */ |
||
| 104 | 23 | public function close() |
|
| 116 | |||
| 117 | /** |
||
| 118 | * @param string $data |
||
| 119 | * @param int|null $length |
||
| 120 | * |
||
| 121 | * @return $this |
||
| 122 | * |
||
| 123 | * @throws IOException |
||
| 124 | */ |
||
| 125 | 22 | public function write($data, $length = null) |
|
| 147 | |||
| 148 | /** |
||
| 149 | * {@inheritdoc} |
||
| 150 | */ |
||
| 151 | 23 | public function peek($length, $blocking = true) |
|
| 167 | |||
| 168 | /** |
||
| 169 | * {@inheritdoc} |
||
| 170 | */ |
||
| 171 | 22 | View Code Duplication | public function read($length, $blocking = true) |
| 183 | |||
| 184 | /** |
||
| 185 | * @param int $length |
||
| 186 | * @param bool $blocking |
||
| 187 | * |
||
| 188 | * @return string |
||
| 189 | * |
||
| 190 | * @throws IOException |
||
| 191 | */ |
||
| 192 | 23 | private function recv($length, $blocking) |
|
| 214 | |||
| 215 | /** |
||
| 216 | * @return bool |
||
| 217 | */ |
||
| 218 | 25 | public function isOpen() |
|
| 222 | } |
||
| 223 |