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 |
||
12 | class StreamSeeker extends BaseEventEmitter implements StreamSeekerInterface |
||
13 | { |
||
14 | /** |
||
15 | * @var resource |
||
16 | */ |
||
17 | protected $resource; |
||
18 | |||
19 | /** |
||
20 | * @var bool |
||
21 | */ |
||
22 | protected $autoClose; |
||
23 | |||
24 | /** |
||
25 | * @var bool |
||
26 | */ |
||
27 | protected $closing; |
||
28 | |||
29 | /** |
||
30 | * @param resource $resource |
||
31 | * @param bool $autoClose |
||
32 | * @throws InvalidArgumentException |
||
33 | */ |
||
34 | 115 | public function __construct($resource, $autoClose = true) |
|
50 | |||
51 | /** |
||
52 | * |
||
53 | */ |
||
54 | 89 | public function __destruct() |
|
64 | |||
65 | /** |
||
66 | * @override |
||
67 | * @inheritDoc |
||
68 | */ |
||
69 | 36 | public function getResource() |
|
73 | |||
74 | /** |
||
75 | * @override |
||
76 | * @inheritDoc |
||
77 | */ |
||
78 | 7 | public function getResourceId() |
|
82 | |||
83 | /** |
||
84 | * @override |
||
85 | * @inheritDoc |
||
86 | */ |
||
87 | 40 | public function getMetadata() |
|
91 | |||
92 | /** |
||
93 | * @override |
||
94 | * @inheritDoc |
||
95 | */ |
||
96 | public function getStreamType() |
||
100 | |||
101 | /** |
||
102 | * @override |
||
103 | * @inheritDoc |
||
104 | */ |
||
105 | public function getWrapperType() |
||
109 | |||
110 | /** |
||
111 | * @override |
||
112 | * @inheritDoc |
||
113 | */ |
||
114 | 21 | public function isOpen() |
|
118 | |||
119 | /** |
||
120 | * @override |
||
121 | * @inheritDoc |
||
122 | */ |
||
123 | 33 | public function isSeekable() |
|
127 | |||
128 | /** |
||
129 | * @override |
||
130 | * @inheritDoc |
||
131 | */ |
||
132 | 14 | public function tell() |
|
147 | |||
148 | /** |
||
149 | * @override |
||
150 | * @inheritDoc |
||
151 | */ |
||
152 | 7 | public function seek($offset, $whence = SEEK_SET) |
|
167 | |||
168 | /** |
||
169 | * @override |
||
170 | * @inheritDoc |
||
171 | */ |
||
172 | 12 | public function rewind() |
|
184 | |||
185 | /** |
||
186 | * @override |
||
187 | * @inheritDoc |
||
188 | */ |
||
189 | 6 | View Code Duplication | public function close() |
204 | |||
205 | /** |
||
206 | * Emit error event and the throws it too. |
||
207 | * |
||
208 | * @param Error|Exception $ex |
||
209 | * @return null |
||
210 | * @throws Error|Exception |
||
211 | */ |
||
212 | protected function throwAndEmitException($ex) |
||
217 | |||
218 | /** |
||
219 | * Handle the close of the stream object. |
||
220 | * |
||
221 | * @internal |
||
222 | */ |
||
223 | 100 | public function handleClose() |
|
230 | } |
||
231 |
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.