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 | const DEFAULT_CONNECTION_TIMEOUT = 30; |
||
| 12 | const DEFAULT_READING_TIMEOUT = 1; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @var resource|null |
||
| 16 | */ |
||
| 17 | private $stream; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | private $buffer; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * {@inheritdoc} |
||
| 26 | */ |
||
| 27 | 25 | public function open($protocol, $host, $port, array $parameters = []) |
|
| 46 | |||
| 47 | /** |
||
| 48 | * @param string $protocol |
||
| 49 | * @param string $host |
||
| 50 | * @param int $port |
||
| 51 | * @param array $parameters |
||
| 52 | * |
||
| 53 | * @return resource |
||
| 54 | * |
||
| 55 | * @throws IOException |
||
| 56 | */ |
||
| 57 | 25 | private function openConnection($protocol, $host, $port, array $parameters = []) |
|
| 77 | |||
| 78 | /** |
||
| 79 | * @param array $parameters |
||
| 80 | * |
||
| 81 | * @return resource |
||
| 82 | */ |
||
| 83 | 25 | private function createStreamContext(array $parameters) |
|
| 106 | |||
| 107 | /** |
||
| 108 | * @param array $parameters |
||
| 109 | */ |
||
| 110 | 25 | private function tuneConnection(array $parameters) |
|
| 120 | |||
| 121 | /** |
||
| 122 | * @return $this |
||
| 123 | */ |
||
| 124 | 23 | public function close() |
|
| 136 | |||
| 137 | /** |
||
| 138 | * @param string $data |
||
| 139 | * @param int|null $length |
||
| 140 | * |
||
| 141 | * @return $this |
||
| 142 | * |
||
| 143 | * @throws IOException |
||
| 144 | */ |
||
| 145 | 22 | public function write($data, $length = null) |
|
| 167 | |||
| 168 | /** |
||
| 169 | * {@inheritdoc} |
||
| 170 | */ |
||
| 171 | 23 | public function peek($length, $blocking = true) |
|
| 187 | |||
| 188 | /** |
||
| 189 | * {@inheritdoc} |
||
| 190 | */ |
||
| 191 | 22 | View Code Duplication | public function read($length, $blocking = true) |
| 203 | |||
| 204 | /** |
||
| 205 | * @param int $length |
||
| 206 | * @param bool $blocking |
||
| 207 | * |
||
| 208 | * @return string |
||
| 209 | * |
||
| 210 | * @throws IOException |
||
| 211 | */ |
||
| 212 | 23 | private function recv($length, $blocking) |
|
| 237 | |||
| 238 | /** |
||
| 239 | * @return bool |
||
| 240 | */ |
||
| 241 | 25 | public function isOpen() |
|
| 245 | } |
||
| 246 |
If you suppress an error, we recommend checking for the error condition explicitly: