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 DataStream 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 DataStream, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class DataStream { |
||
| 11 | |||
| 12 | /** |
||
| 13 | * @var string |
||
| 14 | */ |
||
| 15 | private $data; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var int |
||
| 19 | */ |
||
| 20 | private $length; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @param string $binary |
||
| 24 | */ |
||
| 25 | public function __construct($binary) { |
||
| 29 | |||
| 30 | |||
| 31 | /** |
||
| 32 | * Read data from stream. |
||
| 33 | * |
||
| 34 | * @param int $length |
||
| 35 | * @throws \Exception |
||
| 36 | * @return string |
||
| 37 | */ |
||
| 38 | protected function read($length) { |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Read single character. |
||
| 50 | * |
||
| 51 | * @return int |
||
| 52 | */ |
||
| 53 | public function readChar() { |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Read unsigned short. |
||
| 59 | * |
||
| 60 | * @return int |
||
| 61 | */ |
||
| 62 | public function readShort() { |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Read unsigned int. |
||
| 68 | * |
||
| 69 | * @param bool $isCollectionElement |
||
| 70 | * @return int |
||
| 71 | */ |
||
| 72 | public function readInt($isCollectionElement = false) { |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Read unsigned big int; |
||
| 82 | * |
||
| 83 | * @return int; |
||
|
|
|||
| 84 | */ |
||
| 85 | function readBigInt($isCollectionElement = false) { |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Read string. |
||
| 149 | * |
||
| 150 | * @return string |
||
| 151 | */ |
||
| 152 | public function readString() { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Read long string. |
||
| 159 | * |
||
| 160 | * @return string |
||
| 161 | */ |
||
| 162 | public function readLongString() { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Read bytes. |
||
| 169 | * |
||
| 170 | * @param bool $isCollectionElement |
||
| 171 | * @return string |
||
| 172 | */ |
||
| 173 | public function readBytes($isCollectionElement = false) { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Read uuid. |
||
| 186 | * |
||
| 187 | * @param bool $isCollectionElement |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | public function readUuid($isCollectionElement = false) { |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Read timestamp. |
||
| 208 | * |
||
| 209 | * Cassandra is using the default java date representation, which is the |
||
| 210 | * milliseconds since epoch. Since we cannot use 64 bits integers without |
||
| 211 | * extra libraries, we are reading this as two 32 bits numbers and calculate |
||
| 212 | * the seconds since epoch. |
||
| 213 | * |
||
| 214 | * @return int |
||
| 215 | */ |
||
| 216 | public function readTimestamp() { |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Read list. |
||
| 222 | * |
||
| 223 | * @param $valueType |
||
| 224 | * @return array |
||
| 225 | */ |
||
| 226 | View Code Duplication | public function readList($valueType) { |
|
| 234 | |||
| 235 | /** |
||
| 236 | * Read map. |
||
| 237 | * |
||
| 238 | * @param $keyType |
||
| 239 | * @param $valueType |
||
| 240 | * @return array |
||
| 241 | */ |
||
| 242 | View Code Duplication | public function readMap($keyType, $valueType) { |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Read float. |
||
| 253 | * |
||
| 254 | * @param bool $isCollectionElement |
||
| 255 | * @return float |
||
| 256 | */ |
||
| 257 | public function readFloat($isCollectionElement = false) { |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Read double. |
||
| 266 | * |
||
| 267 | * @param bool $isCollectionElement |
||
| 268 | * @return double |
||
| 269 | */ |
||
| 270 | public function readDouble($isCollectionElement = false) { |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Read boolean. |
||
| 279 | * |
||
| 280 | * @return bool |
||
| 281 | */ |
||
| 282 | public function readBoolean() { |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Read inet. |
||
| 288 | * |
||
| 289 | * @param bool $isCollectionElement |
||
| 290 | * @return string |
||
| 291 | */ |
||
| 292 | public function readInet($isCollectionElement = false) { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Read variable length integer. |
||
| 303 | * |
||
| 304 | * @param bool $isCollectionElement |
||
| 305 | * @return string |
||
| 306 | */ |
||
| 307 | public function readVarint($isCollectionElement = false) { |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Read variable length decimal. |
||
| 320 | * |
||
| 321 | * @param bool $isCollectionElement |
||
| 322 | * @return string |
||
| 323 | */ |
||
| 324 | public function readDecimal($isCollectionElement = false) { |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @param array $type |
||
| 336 | * @param bool $isCollectionElement for collection element used other alg. a temporary solution |
||
| 337 | * @return mixed |
||
| 338 | */ |
||
| 339 | public function readByType(array $type, $isCollectionElement = false) { |
||
| 386 | |||
| 387 | /** |
||
| 388 | * |
||
| 389 | * @param string $hex |
||
| 390 | * @return string |
||
| 391 | */ |
||
| 392 | private function bchexdec($hex) { |
||
| 400 | |||
| 401 | } |
||
| 402 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.