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 SftpResource 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 SftpResource, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class SftpResource extends Stream implements SSH2ResourceInterface |
||
|
|
|||
| 17 | { |
||
| 18 | use LoopAwareTrait; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var bool |
||
| 22 | */ |
||
| 23 | protected $writing; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var bool |
||
| 27 | */ |
||
| 28 | protected $reading; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var bool |
||
| 32 | */ |
||
| 33 | protected $readingStarted; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var bool |
||
| 37 | */ |
||
| 38 | protected $paused; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var BufferInterface |
||
| 42 | */ |
||
| 43 | protected $buffer; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @param SSH2DriverInterface $driver |
||
| 47 | * @param resource $resource |
||
| 48 | */ |
||
| 49 | public function __construct(SSH2DriverInterface $driver, $resource) |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @return string |
||
| 63 | */ |
||
| 64 | public function getId() |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @override |
||
| 71 | * @inheritDoc |
||
| 72 | */ |
||
| 73 | public function isPaused() |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @override |
||
| 80 | * @inheritDoc |
||
| 81 | */ |
||
| 82 | public function setBufferSize($bufferSize) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @override |
||
| 89 | * @inheritDoc |
||
| 90 | */ |
||
| 91 | public function getBufferSize() |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @override |
||
| 98 | * @inheritDoc |
||
| 99 | */ |
||
| 100 | public function pause() |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @override |
||
| 112 | * @inheritDoc |
||
| 113 | */ |
||
| 114 | public function resume() |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @override |
||
| 134 | * @inheritDoc |
||
| 135 | */ |
||
| 136 | public function write($text = '') |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @override |
||
| 157 | * @inheritDoc |
||
| 158 | */ |
||
| 159 | public function read($length = null) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @override |
||
| 179 | * @inheritDoc |
||
| 180 | */ |
||
| 181 | public function close() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Handle the outcoming stream. |
||
| 204 | * |
||
| 205 | * @internal |
||
| 206 | */ |
||
| 207 | public function handleWrite() |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Handle the incoming stream. |
||
| 257 | * |
||
| 258 | * @internal |
||
| 259 | */ |
||
| 260 | public function handleRead() |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Handle close. |
||
| 301 | * |
||
| 302 | * @internal |
||
| 303 | */ |
||
| 304 | public function handleClose() |
||
| 310 | |||
| 311 | /** |
||
| 312 | * |
||
| 313 | */ |
||
| 314 | private function writeEnd() |
||
| 334 | } |
||
| 335 |