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 StreamWriter extends Sync\StreamWriter implements StreamWriterInterface |
||
15 | { |
||
16 | use LoopAwareTrait; |
||
17 | |||
18 | /** |
||
19 | * @var bool |
||
20 | */ |
||
21 | protected $writing; |
||
22 | |||
23 | /** |
||
24 | * @var bool |
||
25 | */ |
||
26 | protected $paused; |
||
27 | |||
28 | /** |
||
29 | * @var BufferInterface |
||
30 | */ |
||
31 | protected $buffer; |
||
32 | |||
33 | /** |
||
34 | * @param resource $resource |
||
35 | * @param LoopInterface $loop |
||
36 | * @param bool $autoClose |
||
37 | * @throws InvalidArgumentException |
||
38 | */ |
||
39 | 4 | View Code Duplication | public function __construct($resource, LoopInterface $loop, $autoClose = true) |
55 | |||
56 | /** |
||
57 | * |
||
58 | */ |
||
59 | 1 | public function __destruct() |
|
68 | |||
69 | /** |
||
70 | * @override |
||
71 | * @inheritDoc |
||
72 | */ |
||
73 | public function isPaused() |
||
77 | |||
78 | /** |
||
79 | * @override |
||
80 | * @inheritDoc |
||
81 | */ |
||
82 | public function setBufferSize($bufferSize) |
||
86 | |||
87 | /** |
||
88 | * @override |
||
89 | * @inheritDoc |
||
90 | */ |
||
91 | 2 | public function getBufferSize() |
|
95 | |||
96 | /** |
||
97 | * @override |
||
98 | * @inheritDoc |
||
99 | */ |
||
100 | 2 | public function pause() |
|
109 | |||
110 | /** |
||
111 | * @override |
||
112 | * @inheritDoc |
||
113 | */ |
||
114 | 4 | public function resume() |
|
126 | |||
127 | /** |
||
128 | * @override |
||
129 | * @inheritDoc |
||
130 | */ |
||
131 | 3 | View Code Duplication | public function write($text = '') |
150 | |||
151 | /** |
||
152 | * @override |
||
153 | * @inheritDoc |
||
154 | */ |
||
155 | 1 | View Code Duplication | public function close() |
175 | |||
176 | /** |
||
177 | * Handle the outcoming stream. |
||
178 | * |
||
179 | * @internal |
||
180 | */ |
||
181 | 3 | View Code Duplication | public function handleWrite() |
208 | |||
209 | /** |
||
210 | * Handle close. |
||
211 | * |
||
212 | * @internal |
||
213 | */ |
||
214 | 2 | public function handleClose() |
|
220 | |||
221 | /** |
||
222 | * Get function that should be invoked on write event. |
||
223 | * |
||
224 | * @return callable |
||
225 | */ |
||
226 | 4 | protected function getHandleWriteFunction() |
|
230 | |||
231 | /** |
||
232 | * |
||
233 | */ |
||
234 | View Code Duplication | private function writeEnd() |
|
254 | } |
||
255 |
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.