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 StreamReader extends Sync\StreamReader implements StreamReaderInterface |
||
13 | { |
||
14 | use LoopAwareTrait; |
||
15 | |||
16 | /** |
||
17 | * @var bool |
||
18 | */ |
||
19 | protected $reading; |
||
20 | |||
21 | /** |
||
22 | * @var bool |
||
23 | */ |
||
24 | protected $readingStarted; |
||
25 | |||
26 | /** |
||
27 | * @var bool |
||
28 | */ |
||
29 | protected $paused; |
||
30 | |||
31 | /** |
||
32 | * @param resource $resource |
||
33 | * @param LoopInterface $loop |
||
34 | * @param bool $autoClose |
||
35 | * @throws InvalidArgumentException |
||
36 | */ |
||
37 | 3 | View Code Duplication | public function __construct($resource, LoopInterface $loop, $autoClose = true) |
53 | |||
54 | /** |
||
55 | * |
||
56 | */ |
||
57 | 1 | public function __destruct() |
|
66 | |||
67 | /** |
||
68 | * @override |
||
69 | * @inheritDoc |
||
70 | */ |
||
71 | public function isPaused() |
||
75 | |||
76 | /** |
||
77 | * @override |
||
78 | * @inheritDoc |
||
79 | */ |
||
80 | public function setBufferSize($bufferSize) |
||
84 | |||
85 | /** |
||
86 | * @override |
||
87 | * @inheritDoc |
||
88 | */ |
||
89 | public function getBufferSize() |
||
93 | |||
94 | /** |
||
95 | * @override |
||
96 | * @inheritDoc |
||
97 | */ |
||
98 | 2 | public function pause() |
|
107 | |||
108 | /** |
||
109 | * @override |
||
110 | * @inheritDoc |
||
111 | */ |
||
112 | 3 | public function resume() |
|
124 | |||
125 | /** |
||
126 | * @override |
||
127 | * @inheritDoc |
||
128 | */ |
||
129 | 2 | View Code Duplication | public function read($length = null) |
147 | |||
148 | /** |
||
149 | * Handle the incoming stream. |
||
150 | * |
||
151 | * @internal |
||
152 | */ |
||
153 | 2 | View Code Duplication | public function handleRead() |
176 | |||
177 | /** |
||
178 | * Get function that should be invoked on read event. |
||
179 | * |
||
180 | * @return callable |
||
181 | */ |
||
182 | 3 | protected function getHandleReadFunction() |
|
186 | |||
187 | /** |
||
188 | * Handle close. |
||
189 | * |
||
190 | * @internal |
||
191 | */ |
||
192 | 2 | public function handleClose() |
|
198 | } |
||
199 |
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.