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 = []) |
|
57 | |||
58 | /** |
||
59 | * @param float $timeout |
||
60 | */ |
||
61 | 25 | private function setReadingTimeout($timeout) |
|
66 | |||
67 | /** |
||
68 | * @param int $size |
||
69 | */ |
||
70 | 25 | private function setReadAheadSize($size) |
|
74 | |||
75 | /** |
||
76 | * @param array $parameters |
||
77 | * |
||
78 | * @return resource |
||
79 | */ |
||
80 | 25 | private function createStreamContext(array $parameters) |
|
103 | |||
104 | /** |
||
105 | * @return $this |
||
106 | */ |
||
107 | 23 | public function close() |
|
119 | |||
120 | /** |
||
121 | * @param string $data |
||
122 | * @param int|null $length |
||
123 | * |
||
124 | * @return $this |
||
125 | * |
||
126 | * @throws IOException |
||
127 | */ |
||
128 | 22 | public function write($data, $length = null) |
|
150 | |||
151 | /** |
||
152 | * {@inheritdoc} |
||
153 | */ |
||
154 | 23 | public function peek($length, $blocking = true) |
|
170 | |||
171 | /** |
||
172 | * {@inheritdoc} |
||
173 | */ |
||
174 | 22 | View Code Duplication | public function read($length, $blocking = true) |
186 | |||
187 | /** |
||
188 | * @param int $length |
||
189 | * @param bool $blocking |
||
190 | * |
||
191 | * @return string |
||
192 | * |
||
193 | * @throws IOException |
||
194 | */ |
||
195 | 23 | private function recv($length, $blocking) |
|
217 | |||
218 | /** |
||
219 | * @return bool |
||
220 | */ |
||
221 | 25 | public function isOpen() |
|
225 | } |
||
226 |