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 DriverEio 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 DriverEio, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class DriverEio extends DriverAbstract implements DriverInterface |
||
15 | { |
||
16 | /** |
||
17 | * @var array |
||
18 | */ |
||
19 | protected $options; |
||
20 | |||
21 | /** |
||
22 | * @var InvokerInterface |
||
23 | */ |
||
24 | protected $invoker; |
||
25 | |||
26 | /** |
||
27 | * @var resource |
||
28 | */ |
||
29 | protected $stream; |
||
30 | |||
31 | /** |
||
32 | * @var bool |
||
33 | */ |
||
34 | protected $active = false; |
||
35 | |||
36 | /** |
||
37 | * @var FlagResolverInterface |
||
38 | */ |
||
39 | protected $flagPermission; |
||
40 | |||
41 | /** |
||
42 | * @var FlagResolverInterface |
||
43 | */ |
||
44 | protected $flagOpen; |
||
45 | |||
46 | /** |
||
47 | * @param LoopInterface $loop |
||
48 | * @param array $options |
||
49 | */ |
||
50 | public function __construct(LoopInterface $loop, $options = []) |
||
60 | |||
61 | /** |
||
62 | * @override |
||
63 | * @inheritDoc |
||
64 | */ |
||
65 | public function access($path, $mode = 0755) |
||
69 | |||
70 | /** |
||
71 | * @override |
||
72 | * @inheritDoc |
||
73 | */ |
||
74 | public function append($path, $data = '') |
||
78 | |||
79 | /** |
||
80 | * @override |
||
81 | * @inheritDoc |
||
82 | */ |
||
83 | public function chmod($path, $mode) |
||
88 | |||
89 | /** |
||
90 | * @override |
||
91 | * @inheritDoc |
||
92 | */ |
||
93 | public function chown($path, $uid = -1, $gid = -1) |
||
98 | |||
99 | /** |
||
100 | * @override |
||
101 | * @inheritDoc |
||
102 | */ |
||
103 | public function exists($path) |
||
111 | |||
112 | /** |
||
113 | * @override |
||
114 | * @inheritDoc |
||
115 | */ |
||
116 | public function link($srcPath, $dstPath) |
||
120 | |||
121 | /** |
||
122 | * @override |
||
123 | * @inheritDoc |
||
124 | */ |
||
125 | public function ls($path) |
||
129 | |||
130 | /** |
||
131 | * @override |
||
132 | * @inheritDoc |
||
133 | */ |
||
134 | public function mkdir($path, $mode = 0755) |
||
138 | |||
139 | /** |
||
140 | * @override |
||
141 | * @inheritDoc |
||
142 | */ |
||
143 | public function prepend($path, $data = '') |
||
147 | |||
148 | /** |
||
149 | * @override |
||
150 | * @inheritDoc |
||
151 | */ |
||
152 | public function readlink($path) |
||
156 | |||
157 | /** |
||
158 | * @override |
||
159 | * @inheritDoc |
||
160 | */ |
||
161 | public function realpath($path) |
||
165 | |||
166 | /** |
||
167 | * @override |
||
168 | * @inheritDoc |
||
169 | */ |
||
170 | public function rename($srcPath, $dstPath) |
||
174 | |||
175 | /** |
||
176 | * @override |
||
177 | * @inheritDoc |
||
178 | */ |
||
179 | public function rmdir($path) |
||
183 | |||
184 | /** |
||
185 | * @override |
||
186 | * @inheritDoc |
||
187 | */ |
||
188 | public function stat($path) |
||
201 | |||
202 | /** |
||
203 | * @override |
||
204 | * @inheritDoc |
||
205 | */ |
||
206 | public function symlink($srcPath, $dstPath) |
||
210 | |||
211 | /** |
||
212 | * @override |
||
213 | * @inheritDoc |
||
214 | */ |
||
215 | public function truncate($path, $len = 0) |
||
219 | |||
220 | /** |
||
221 | * @override |
||
222 | * @inheritDoc |
||
223 | */ |
||
224 | public function unlink($path) |
||
228 | |||
229 | /** |
||
230 | * @internal |
||
231 | * @override |
||
232 | * @inheritDoc |
||
233 | */ |
||
234 | public function call($func, $args = []) |
||
257 | |||
258 | /** |
||
259 | * @param string $func |
||
260 | * @param array $args |
||
261 | * @return PromiseInterface |
||
262 | */ |
||
263 | protected function callDelayed($func, $args = []) |
||
292 | |||
293 | protected function register() |
||
302 | |||
303 | protected function unregister() |
||
312 | |||
313 | public function handleEvent() |
||
330 | |||
331 | public function workPendingCount() |
||
335 | |||
336 | /** |
||
337 | * Get path. |
||
338 | * |
||
339 | * @param string $path |
||
340 | * @return string |
||
341 | */ |
||
342 | protected function getPath($path) |
||
346 | |||
347 | /** |
||
348 | * Create valid configuration for the driver. |
||
349 | * |
||
350 | * @param array $options |
||
351 | * @return array |
||
352 | */ |
||
353 | protected function createConfiguration($options = []) |
||
360 | |||
361 | /** |
||
362 | * Create invoker for the driver. |
||
363 | * |
||
364 | * @return InvokerInterface |
||
365 | */ |
||
366 | View Code Duplication | protected function createInvoker() |
|
374 | } |
||
375 |
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.