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 |
||
18 | class Stream implements StreamInterface |
||
19 | { |
||
20 | /** |
||
21 | * @var string[] |
||
22 | */ |
||
23 | private static $readModes = ['r', 'w+', 'r+', 'x+', 'c+', 'rb', 'w+b', 'r+b', 'x+b', 'c+b', 'rt', 'w+t', |
||
24 | 'r+t', 'x+t', 'c+t', 'a+']; |
||
25 | |||
26 | /** |
||
27 | * @var string[] |
||
28 | */ |
||
29 | private static $writeModes = ['w', 'w+', 'rw', 'r+', 'x+', 'c+', 'wb', 'w+b', 'r+b', 'x+b', 'c+b', 'w+t', |
||
30 | 'r+t', 'x+t', 'c+t', 'a', 'a+']; |
||
31 | |||
32 | /** |
||
33 | * @var resource |
||
34 | */ |
||
35 | protected $resource; |
||
36 | |||
37 | /** |
||
38 | * @var bool |
||
39 | */ |
||
40 | protected $local; |
||
41 | |||
42 | /** |
||
43 | * @var bool |
||
44 | */ |
||
45 | protected $readable; |
||
46 | |||
47 | /** |
||
48 | * @var bool |
||
49 | */ |
||
50 | protected $writable; |
||
51 | |||
52 | /** |
||
53 | * @var bool |
||
54 | */ |
||
55 | protected $seekable; |
||
56 | |||
57 | /** |
||
58 | * @var string |
||
59 | */ |
||
60 | protected $uri; |
||
61 | |||
62 | /** |
||
63 | * @var StringReader |
||
64 | */ |
||
65 | protected $stringReader; |
||
66 | |||
67 | /** |
||
68 | * Create stream object from resource |
||
69 | * |
||
70 | * @param resource $resource |
||
71 | * |
||
72 | * @return StreamInterface |
||
73 | */ |
||
74 | 78 | public static function fromResource($resource) |
|
80 | |||
81 | /** |
||
82 | * Get meta data from the stream |
||
83 | * |
||
84 | * @throws Exception\IOException An exception will be thrown for invalid stream resources |
||
85 | * |
||
86 | * @return array |
||
87 | */ |
||
88 | 75 | protected function getMetaData() |
|
92 | |||
93 | /** |
||
94 | * {@inheritdoc} |
||
95 | */ |
||
96 | 78 | public function bindResource($resource) |
|
110 | |||
111 | /** |
||
112 | * {@inheritdoc} |
||
113 | */ |
||
114 | 78 | public function getResource() |
|
122 | |||
123 | /** |
||
124 | * {@inheritdoc} |
||
125 | */ |
||
126 | 9 | public function isLocal() |
|
130 | |||
131 | /** |
||
132 | * {@inheritdoc} |
||
133 | */ |
||
134 | 12 | public function isReadable() |
|
138 | |||
139 | /** |
||
140 | * {@inheritdoc} |
||
141 | */ |
||
142 | 12 | public function isWritable() |
|
146 | |||
147 | /** |
||
148 | * {@inheritdoc} |
||
149 | */ |
||
150 | 12 | public function isSeekable() |
|
154 | |||
155 | /** |
||
156 | * {@inheritdoc} |
||
157 | */ |
||
158 | 6 | public function getUri() |
|
162 | |||
163 | /** |
||
164 | * Get string reader |
||
165 | * |
||
166 | * @return StringReader |
||
167 | */ |
||
168 | 9 | public function getStringReader() |
|
176 | |||
177 | /** |
||
178 | * Set string reader |
||
179 | * |
||
180 | * @param StringReader $stringReader |
||
181 | * |
||
182 | * @return $this |
||
183 | */ |
||
184 | 3 | public function setStringReader(StringReader $stringReader) |
|
190 | |||
191 | /** |
||
192 | * Get information about the stream |
||
193 | * |
||
194 | * @param string $info The information to retrieve |
||
195 | * |
||
196 | * @throws Exception\IOException An exception will be thrown for invalid stream resources |
||
197 | * |
||
198 | * @return int |
||
199 | */ |
||
200 | 6 | protected function getStat($info) |
|
213 | |||
214 | /** |
||
215 | * {@inheritdoc} |
||
216 | */ |
||
217 | 9 | public function getSize() |
|
225 | |||
226 | /** |
||
227 | * {@inheritdoc} |
||
228 | */ |
||
229 | 6 | public function eof() |
|
233 | |||
234 | /** |
||
235 | * {@inheritdoc} |
||
236 | */ |
||
237 | 12 | public function tell() |
|
241 | |||
242 | /** |
||
243 | * {@inheritdoc} |
||
244 | */ |
||
245 | 15 | public function seek($offset, $whence = SEEK_SET) |
|
259 | |||
260 | /** |
||
261 | * {@inheritdoc} |
||
262 | */ |
||
263 | 3 | public function rewind() |
|
267 | |||
268 | /** |
||
269 | * {@inheritdoc} |
||
270 | */ |
||
271 | 15 | View Code Duplication | public function read($length) |
286 | |||
287 | /** |
||
288 | * {@inheritdoc} |
||
289 | */ |
||
290 | 3 | public function readString($length = 1) |
|
297 | |||
298 | /** |
||
299 | * {@inheritdoc} |
||
300 | */ |
||
301 | 12 | View Code Duplication | public function write($data) |
316 | |||
317 | /** |
||
318 | * {@inheritdoc} |
||
319 | */ |
||
320 | 9 | public function truncate($size) |
|
330 | |||
331 | /** |
||
332 | * {@inheritdoc} |
||
333 | */ |
||
334 | 3 | public function close() |
|
338 | } |
||
339 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.