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:
Complex classes like Stream often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Stream, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Stream extends Sync\Stream implements StreamInterface |
||
16 | { |
||
17 | use LoopAwareTrait; |
||
18 | |||
19 | /** |
||
20 | * @var bool |
||
21 | */ |
||
22 | protected $writing; |
||
23 | |||
24 | /** |
||
25 | * @var bool |
||
26 | */ |
||
27 | protected $reading; |
||
28 | |||
29 | /** |
||
30 | * @var bool |
||
31 | */ |
||
32 | protected $readingStarted; |
||
33 | |||
34 | /** |
||
35 | * @var bool |
||
36 | */ |
||
37 | protected $paused; |
||
38 | |||
39 | /** |
||
40 | * @var BufferInterface |
||
41 | */ |
||
42 | protected $buffer; |
||
43 | |||
44 | /** |
||
45 | * @param resource $resource |
||
46 | * @param LoopInterface $loop |
||
47 | * @param bool $autoClose |
||
48 | * @throws InvalidArgumentException |
||
49 | */ |
||
50 | 17 | public function __construct($resource, LoopInterface $loop, $autoClose = true) |
|
73 | |||
74 | /** |
||
75 | * |
||
76 | */ |
||
77 | 13 | public function __destruct() |
|
88 | |||
89 | /** |
||
90 | * @override |
||
91 | * @inheritDoc |
||
92 | */ |
||
93 | 1 | public function isPaused() |
|
97 | |||
98 | /** |
||
99 | * @override |
||
100 | * @inheritDoc |
||
101 | */ |
||
102 | public function setBufferSize($bufferSize) |
||
106 | |||
107 | /** |
||
108 | * @override |
||
109 | * @inheritDoc |
||
110 | */ |
||
111 | public function getBufferSize() |
||
115 | |||
116 | /** |
||
117 | * @override |
||
118 | * @inheritDoc |
||
119 | */ |
||
120 | 15 | public function pause() |
|
131 | |||
132 | /** |
||
133 | * @override |
||
134 | * @inheritDoc |
||
135 | */ |
||
136 | 16 | public function resume() |
|
155 | |||
156 | /** |
||
157 | * @override |
||
158 | * @inheritDoc |
||
159 | */ |
||
160 | 2 | View Code Duplication | public function write($text = '') |
179 | |||
180 | /** |
||
181 | * @override |
||
182 | * @inheritDoc |
||
183 | */ |
||
184 | 1 | View Code Duplication | public function read($length = null) |
202 | |||
203 | /** |
||
204 | * @override |
||
205 | * @inheritDoc |
||
206 | */ |
||
207 | 3 | View Code Duplication | public function close() |
227 | |||
228 | /** |
||
229 | * Handle the outcoming stream. |
||
230 | * |
||
231 | * @internal |
||
232 | */ |
||
233 | 2 | View Code Duplication | public function handleWrite() |
260 | |||
261 | /** |
||
262 | * Handle the incoming stream. |
||
263 | * |
||
264 | * @internal |
||
265 | */ |
||
266 | 2 | View Code Duplication | public function handleRead() |
289 | |||
290 | /** |
||
291 | * Handle close. |
||
292 | * |
||
293 | * @internal |
||
294 | */ |
||
295 | 15 | public function handleClose() |
|
301 | |||
302 | /** |
||
303 | * Get function that should be invoked on read event. |
||
304 | * |
||
305 | * @return callable |
||
306 | */ |
||
307 | 2 | protected function getHandleReadFunction() |
|
311 | |||
312 | /** |
||
313 | * Get function that should be invoked on write event. |
||
314 | * |
||
315 | * @return callable |
||
316 | */ |
||
317 | 3 | protected function getHandleWriteFunction() |
|
321 | |||
322 | /** |
||
323 | * |
||
324 | */ |
||
325 | View Code Duplication | private function writeEnd() |
|
345 | } |
||
346 |
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.