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 StreamIO 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 StreamIO, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class StreamIO implements IOInterface, LoggerAwareInterface |
||
| 14 | { |
||
| 15 | use LoggerAwareTrait; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var resource|null |
||
| 19 | */ |
||
| 20 | private $stream; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | private $buffer; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var int|float |
||
| 29 | */ |
||
| 30 | private $connectionTimeout; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var int|float |
||
| 34 | */ |
||
| 35 | private $readingTimeout; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var int |
||
| 39 | */ |
||
| 40 | private $readAheadSize; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Initialize default logger. |
||
| 44 | * |
||
| 45 | * @param int|float $connectionTimeout |
||
| 46 | * @param int|float $readingTimeout |
||
| 47 | * @param int $readAheadSize |
||
| 48 | */ |
||
| 49 | 25 | public function __construct($connectionTimeout = 30, $readingTimeout = 1, $readAheadSize = 130000) |
|
| 56 | |||
| 57 | /** |
||
| 58 | * {@inheritdoc} |
||
| 59 | */ |
||
| 60 | 25 | public function open($protocol, $host, $port, array $parameters = []) |
|
| 112 | |||
| 113 | /** |
||
| 114 | * @param float|int $timeout |
||
| 115 | * |
||
| 116 | * @return $this |
||
| 117 | */ |
||
| 118 | 25 | private function setReadingTimeout($timeout) |
|
| 128 | |||
| 129 | /** |
||
| 130 | * @param float|int $connectionTimeout |
||
| 131 | * |
||
| 132 | * @return $this |
||
| 133 | */ |
||
| 134 | 25 | private function setConnectionTimeout($connectionTimeout) |
|
| 140 | |||
| 141 | /** |
||
| 142 | * @return $this |
||
| 143 | */ |
||
| 144 | 12 | public function close() |
|
| 158 | |||
| 159 | /** |
||
| 160 | * @param string $data |
||
| 161 | * @param int|null $length |
||
| 162 | * |
||
| 163 | * @return $this |
||
| 164 | * |
||
| 165 | * @throws IOException |
||
| 166 | */ |
||
| 167 | 22 | public function write($data, $length = null) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * {@inheritdoc} |
||
| 198 | */ |
||
| 199 | 23 | public function peek($length, $blocking = true) |
|
| 215 | |||
| 216 | /** |
||
| 217 | * {@inheritdoc} |
||
| 218 | */ |
||
| 219 | 22 | View Code Duplication | public function read($length, $blocking = true) |
| 231 | |||
| 232 | /** |
||
| 233 | * @param int $length |
||
| 234 | * @param bool $blocking |
||
| 235 | * |
||
| 236 | * @return string |
||
| 237 | * |
||
| 238 | * @throws IOException |
||
| 239 | */ |
||
| 240 | 23 | private function recv($length, $blocking) |
|
| 270 | |||
| 271 | /** |
||
| 272 | * Apply reading timeout to active stream. |
||
| 273 | */ |
||
| 274 | 25 | private function applyReadingTimeout() |
|
| 280 | |||
| 281 | /** |
||
| 282 | * @return bool |
||
| 283 | */ |
||
| 284 | 25 | public function isOpen() |
|
| 288 | |||
| 289 | /** |
||
| 290 | * @param array $parameters |
||
| 291 | * |
||
| 292 | * @return resource |
||
| 293 | */ |
||
| 294 | 25 | protected function createStreamContext(array $parameters) |
|
| 324 | } |
||
| 325 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.