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 Shell 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 Shell, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class Shell implements SSH2DriverInterface |
||
18 | { |
||
19 | use BaseEventEmitterTrait; |
||
20 | use LoopAwareTrait; |
||
21 | |||
22 | /** |
||
23 | * @var int |
||
24 | */ |
||
25 | const BUFFER_SIZE = 4096; |
||
26 | |||
27 | /** |
||
28 | * @var SSH2Interface |
||
29 | */ |
||
30 | protected $ssh2; |
||
31 | |||
32 | /** |
||
33 | * @var resource |
||
34 | */ |
||
35 | protected $conn; |
||
36 | |||
37 | /** |
||
38 | * @var float |
||
39 | */ |
||
40 | protected $interval; |
||
41 | |||
42 | /** |
||
43 | * @var resource |
||
44 | */ |
||
45 | protected $resource; |
||
46 | |||
47 | /** |
||
48 | * @var ShellResource[]|SSH2ResourceInterface[] |
||
49 | */ |
||
50 | protected $resources; |
||
51 | |||
52 | /** |
||
53 | * @var bool |
||
54 | */ |
||
55 | protected $paused; |
||
56 | |||
57 | /** |
||
58 | * @var TimerInterface|null |
||
59 | */ |
||
60 | private $timer; |
||
61 | |||
62 | /** |
||
63 | * @var int |
||
64 | */ |
||
65 | private $resourcesCounter; |
||
66 | |||
67 | /** |
||
68 | * @var string |
||
69 | */ |
||
70 | private $buffer; |
||
71 | |||
72 | /** |
||
73 | * @var string |
||
74 | */ |
||
75 | private $prefix; |
||
76 | |||
77 | /** |
||
78 | * @param SSH2Interface $ssh2 |
||
79 | * @param resource $conn |
||
80 | * @param float $interval |
||
81 | */ |
||
82 | 23 | View Code Duplication | public function __construct(SSH2Interface $ssh2, $conn, $interval = 1e-1) |
101 | |||
102 | /** |
||
103 | * |
||
104 | */ |
||
105 | 13 | public function __destruct() |
|
109 | |||
110 | /** |
||
111 | * @override |
||
112 | * @inheritDoc |
||
113 | */ |
||
114 | 3 | public function getName() |
|
118 | |||
119 | /** |
||
120 | * @override |
||
121 | * @inheritDoc |
||
122 | */ |
||
123 | 6 | View Code Duplication | public function connect() |
142 | |||
143 | /** |
||
144 | * @override |
||
145 | * @inheritDoc |
||
146 | */ |
||
147 | 18 | View Code Duplication | public function disconnect() |
164 | |||
165 | /** |
||
166 | * @override |
||
167 | * @inheritDoc |
||
168 | */ |
||
169 | 5 | public function isConnected() |
|
173 | |||
174 | /** |
||
175 | * @override |
||
176 | * @inheritDoc |
||
177 | */ |
||
178 | 5 | View Code Duplication | public function pause() |
191 | |||
192 | /** |
||
193 | * @override |
||
194 | * @inheritDoc |
||
195 | */ |
||
196 | 23 | View Code Duplication | public function resume() |
208 | |||
209 | /** |
||
210 | * @override |
||
211 | * @inheritDoc |
||
212 | */ |
||
213 | 6 | public function isPaused() |
|
217 | |||
218 | /** |
||
219 | * @override |
||
220 | * @inheritDoc |
||
221 | */ |
||
222 | 2 | public function open($resource = null, $flags = 'r') |
|
244 | |||
245 | /** |
||
246 | * Handle data. |
||
247 | * |
||
248 | * @internal |
||
249 | */ |
||
250 | 2 | public function handleHeartbeat() |
|
259 | |||
260 | /** |
||
261 | * Handle incoming data. |
||
262 | * |
||
263 | * @internal |
||
264 | */ |
||
265 | 2 | protected function handleRead() |
|
266 | { |
||
267 | 2 | if ($this->paused) |
|
268 | { |
||
269 | return; |
||
270 | } |
||
271 | |||
272 | 2 | $data = @fread($this->resource, static::BUFFER_SIZE); |
|
273 | |||
274 | 2 | if ($data === false || $data === '') |
|
275 | { |
||
276 | return; |
||
277 | } |
||
278 | |||
279 | 2 | $this->buffer .= $data; |
|
280 | |||
281 | 2 | while ($this->buffer !== '') |
|
282 | { |
||
283 | 2 | if ($this->prefix !== '' && !isset($this->resources[$this->prefix])) |
|
284 | { |
||
285 | $this->prefix = ''; |
||
286 | } |
||
287 | |||
288 | 2 | if ($this->prefix === '') |
|
289 | { |
||
290 | 2 | if (!preg_match('/([a-zA-Z0-9]{32})\r?\n(.*)/s', $this->buffer, $matches)) |
|
291 | { |
||
292 | 2 | return; |
|
293 | } |
||
294 | |||
295 | 2 | $this->prefix = $matches[1]; |
|
296 | 2 | $this->buffer = $matches[2]; |
|
297 | } |
||
298 | |||
299 | 2 | $resource = $this->resources[$this->prefix]; |
|
300 | 2 | $data = ''; |
|
301 | 2 | $status = -1; |
|
302 | 2 | $successSuffix = $resource->getSuccessSuffix(); |
|
303 | 2 | $failureSuffix = $resource->getFailureSuffix(); |
|
304 | |||
305 | 2 | $this->buffer = preg_replace_callback( |
|
306 | 2 | sprintf('/(.*)(%s|%s):(\d*)\r?\n/s', $successSuffix, $failureSuffix), |
|
307 | 2 | function($matches) use($resource, &$data, &$status) { |
|
308 | 2 | $data = $matches[1]; |
|
309 | 2 | $status = (int) $matches[3]; |
|
310 | 2 | return ''; |
|
311 | 2 | }, |
|
312 | 2 | $this->buffer |
|
313 | ); |
||
314 | |||
315 | 2 | if ($status === -1) |
|
316 | { |
||
317 | $data = $this->buffer; |
||
318 | $this->buffer = ''; |
||
319 | } |
||
320 | else |
||
321 | { |
||
322 | 2 | $this->removeResource($this->prefix); |
|
323 | 2 | $this->prefix = ''; |
|
324 | } |
||
325 | |||
326 | 2 | $parts = str_split($data, $resource->getBufferSize()); |
|
327 | 2 | unset($data); |
|
328 | |||
329 | 2 | foreach ($parts as &$part) |
|
330 | { |
||
331 | 2 | $resource->emit('data', [ $resource, $part ]); |
|
332 | } |
||
333 | 2 | unset($parts); |
|
334 | 2 | unset($part); |
|
335 | |||
336 | 2 | if ($status === 0) |
|
337 | { |
||
338 | 2 | $resource->emit('end', [ $resource ]); |
|
339 | 2 | $resource->close(); |
|
340 | } |
||
341 | else if ($status > 0) |
||
342 | { |
||
343 | $resource->emit('error', [ $resource, new ReadException($status) ]); |
||
344 | $resource->close(); |
||
345 | } |
||
346 | } |
||
347 | |||
348 | $this->handleRead(); |
||
349 | } |
||
350 | |||
351 | /** |
||
352 | * |
||
353 | */ |
||
354 | 3 | protected function handleDisconnect() |
|
359 | |||
360 | /** |
||
361 | * @param resource $conn |
||
362 | * @return resource |
||
363 | */ |
||
364 | 2 | protected function createConnection($conn) |
|
368 | |||
369 | /** |
||
370 | * Remove resource from known collection. |
||
371 | * |
||
372 | * @param string $prefix |
||
373 | */ |
||
374 | 2 | View Code Duplication | private function removeResource($prefix) |
389 | } |
||
390 |
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.