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 |
||
14 | class ShellResource implements SSH2ResourceInterface |
||
15 | { |
||
16 | use BaseEventEmitterTrait; |
||
17 | |||
18 | /** |
||
19 | * @var SSH2DriverInterface |
||
20 | */ |
||
21 | protected $driver; |
||
22 | |||
23 | /** |
||
24 | * @var resource |
||
25 | */ |
||
26 | protected $resource; |
||
27 | |||
28 | /** |
||
29 | * @var bool |
||
30 | */ |
||
31 | private $paused; |
||
32 | |||
33 | /** |
||
34 | * @var bool |
||
35 | */ |
||
36 | private $closing; |
||
37 | |||
38 | /** |
||
39 | * @var bool |
||
40 | */ |
||
41 | private $readable; |
||
42 | |||
43 | /** |
||
44 | * @var bool |
||
45 | */ |
||
46 | private $writable; |
||
47 | |||
48 | /** |
||
49 | * @var int |
||
50 | */ |
||
51 | private $bufferSize; |
||
52 | |||
53 | /** |
||
54 | * @var string |
||
55 | */ |
||
56 | private $prefix; |
||
57 | |||
58 | /** |
||
59 | * @var string |
||
60 | */ |
||
61 | private $successSuffix; |
||
62 | |||
63 | /** |
||
64 | * @var string |
||
65 | */ |
||
66 | private $failureSuffix; |
||
67 | |||
68 | /** |
||
69 | * @param SSH2DriverInterface $driver |
||
70 | * @param resource $resource |
||
71 | */ |
||
72 | 2 | public function __construct(SSH2DriverInterface $driver, $resource) |
|
88 | |||
89 | /** |
||
90 | * @return string |
||
91 | */ |
||
92 | 2 | public function getId() |
|
96 | |||
97 | /** |
||
98 | * @return string |
||
99 | */ |
||
100 | public function getPrefix() |
||
104 | |||
105 | /** |
||
106 | * @return string |
||
107 | */ |
||
108 | 2 | public function getSuccessSuffix() |
|
112 | |||
113 | /** |
||
114 | * @return string |
||
115 | */ |
||
116 | 2 | public function getFailureSuffix() |
|
120 | |||
121 | /** |
||
122 | * @override |
||
123 | * @inheritDoc |
||
124 | */ |
||
125 | public function setLoop(LoopInterface $loop = null) |
||
129 | |||
130 | /** |
||
131 | * @override |
||
132 | * @inheritDoc |
||
133 | */ |
||
134 | public function getLoop() |
||
138 | |||
139 | /** |
||
140 | * @override |
||
141 | * @inheritDoc |
||
142 | */ |
||
143 | 2 | public function pause() |
|
150 | |||
151 | /** |
||
152 | * @override |
||
153 | * @inheritDoc |
||
154 | */ |
||
155 | public function resume() |
||
162 | |||
163 | /** |
||
164 | * @override |
||
165 | * @inheritDoc |
||
166 | */ |
||
167 | public function isPaused() |
||
171 | |||
172 | /** |
||
173 | * @override |
||
174 | * @inheritDoc |
||
175 | */ |
||
176 | public function getResource() |
||
180 | |||
181 | /** |
||
182 | * @override |
||
183 | * @inheritDoc |
||
184 | */ |
||
185 | public function getResourceId() |
||
189 | |||
190 | /** |
||
191 | * @override |
||
192 | * @inheritDoc |
||
193 | */ |
||
194 | public function getMetadata() |
||
198 | |||
199 | /** |
||
200 | * @override |
||
201 | * @inheritDoc |
||
202 | */ |
||
203 | public function getStreamType() |
||
207 | |||
208 | /** |
||
209 | * @override |
||
210 | * @inheritDoc |
||
211 | */ |
||
212 | public function getWrapperType() |
||
216 | |||
217 | /** |
||
218 | * @override |
||
219 | * @inheritDoc |
||
220 | */ |
||
221 | public function isOpen() |
||
225 | |||
226 | /** |
||
227 | * @override |
||
228 | * @inheritDoc |
||
229 | */ |
||
230 | public function isSeekable() |
||
234 | |||
235 | /** |
||
236 | * @override |
||
237 | * @inheritDoc |
||
238 | */ |
||
239 | public function tell() |
||
243 | |||
244 | /** |
||
245 | * @override |
||
246 | * @inheritDoc |
||
247 | */ |
||
248 | public function seek($offset, $whence = SEEK_SET) |
||
252 | |||
253 | /** |
||
254 | * @override |
||
255 | * @inheritDoc |
||
256 | */ |
||
257 | public function rewind() |
||
261 | |||
262 | /** |
||
263 | * @override |
||
264 | * @inheritDoc |
||
265 | */ |
||
266 | 2 | public function close() |
|
281 | |||
282 | /** |
||
283 | * @override |
||
284 | * @inheritDoc |
||
285 | */ |
||
286 | public function isReadable() |
||
290 | |||
291 | /** |
||
292 | * @override |
||
293 | * @inheritDoc |
||
294 | */ |
||
295 | public function setBufferSize($bufferSize) |
||
299 | |||
300 | /** |
||
301 | * @override |
||
302 | * @inheritDoc |
||
303 | */ |
||
304 | 2 | public function getBufferSize() |
|
308 | |||
309 | /** |
||
310 | * @override |
||
311 | * @inheritDoc |
||
312 | */ |
||
313 | public function read($length = null) |
||
347 | |||
348 | /** |
||
349 | * @override |
||
350 | * @inheritDoc |
||
351 | */ |
||
352 | public function isWritable() |
||
356 | |||
357 | /** |
||
358 | * @override |
||
359 | * @inheritDoc |
||
360 | */ |
||
361 | 2 | public function write($text = '') |
|
393 | |||
394 | /** |
||
395 | * Emit error event and the throws it too. |
||
396 | * |
||
397 | * @param Error|Exception $ex |
||
398 | * @return null |
||
399 | * @throws Error|Exception |
||
400 | */ |
||
401 | protected function throwAndEmitException($ex) |
||
406 | } |
||
407 |
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.