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 |
||
19 | class EventLoop implements EventLoopInterface |
||
20 | { |
||
21 | /** |
||
22 | * @var EvLoop |
||
23 | */ |
||
24 | protected $loop; |
||
25 | |||
26 | /** |
||
27 | * @var SplObjectStorage|EvTimer[] |
||
28 | */ |
||
29 | protected $timers; |
||
30 | |||
31 | /** |
||
32 | * @var SplObjectStorage|EvSignal[] |
||
33 | */ |
||
34 | protected $signals; |
||
35 | |||
36 | /** |
||
37 | * @var SplObjectStorage|EvChild[] |
||
38 | */ |
||
39 | protected $children; |
||
40 | |||
41 | /** |
||
42 | * @var bool |
||
43 | */ |
||
44 | protected $running; |
||
45 | |||
46 | /** |
||
47 | * @var EvIo[] |
||
48 | */ |
||
49 | private $readEvents; |
||
50 | |||
51 | /** |
||
52 | * @var EvIo[] |
||
53 | */ |
||
54 | private $writeEvents; |
||
55 | |||
56 | public function __construct() |
||
65 | |||
66 | /** |
||
67 | * @inheritdoc |
||
68 | */ |
||
69 | public function addReadStream($stream, callable $listener) : self |
||
75 | |||
76 | /** |
||
77 | * @inheritdoc |
||
78 | */ |
||
79 | public function addWriteStream($stream, callable $listener) : self |
||
85 | |||
86 | /** |
||
87 | * @inheritdoc |
||
88 | */ |
||
89 | View Code Duplication | public function removeReadStream($stream) : self |
|
99 | |||
100 | /** |
||
101 | * @inheritdoc |
||
102 | */ |
||
103 | View Code Duplication | public function removeWriteStream($stream) : self |
|
113 | |||
114 | /** |
||
115 | * @inheritdoc |
||
116 | */ |
||
117 | public function removeStream($stream) : self |
||
124 | |||
125 | /** |
||
126 | * @inheritdoc |
||
127 | */ |
||
128 | public function addStream($stream, callable $listener, $flags) : self |
||
144 | |||
145 | /** |
||
146 | * @inheritdoc |
||
147 | */ |
||
148 | View Code Duplication | public function addTimer($interval, callable $callback, int $priority = 0) : Timer |
|
166 | |||
167 | /** |
||
168 | * @inheritdoc |
||
169 | */ |
||
170 | View Code Duplication | public function addPeriodicTimer($interval, callable $callback, int $priority = 0) : Timer |
|
191 | |||
192 | /** |
||
193 | * @inheritdoc |
||
194 | */ |
||
195 | public function cancelTimer(Timer $timer) : self |
||
204 | |||
205 | /** |
||
206 | * @inheritdoc |
||
207 | */ |
||
208 | public function isTimerActive(Timer $timer) : bool |
||
212 | |||
213 | /** |
||
214 | * @inheritdoc |
||
215 | */ |
||
216 | View Code Duplication | public function addSignal(int $signalNo, callable $callback, int $priority = 0) : Signal |
|
230 | |||
231 | /** |
||
232 | * @inheritdoc |
||
233 | */ |
||
234 | public function cancelSignal(Signal $signal) : self |
||
243 | |||
244 | /** |
||
245 | * @inheritdoc |
||
246 | */ |
||
247 | public function isSignalActive(Signal $signal) : bool |
||
251 | |||
252 | /** |
||
253 | * @inheritdoc |
||
254 | */ |
||
255 | View Code Duplication | public function addChild(callable $callback, int $pid = 0, int $priority = 0) : Child |
|
269 | |||
270 | /** |
||
271 | * @inheritdoc |
||
272 | */ |
||
273 | public function cancelChild(Child $child) : self |
||
282 | |||
283 | /** |
||
284 | * @inheritdoc |
||
285 | */ |
||
286 | public function isChildActive(Child $child) : bool |
||
290 | |||
291 | /** |
||
292 | * @inheritdoc |
||
293 | */ |
||
294 | public function afterFork() : self |
||
300 | |||
301 | /** |
||
302 | * {@inheritDoc} |
||
303 | */ |
||
304 | public function tick() : self |
||
310 | |||
311 | /** |
||
312 | * @inheritdoc |
||
313 | */ |
||
314 | public function run() : self |
||
335 | |||
336 | /** |
||
337 | * @inheritdoc |
||
338 | */ |
||
339 | public function stop() : self |
||
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.