Complex classes like Peer 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 Peer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class Peer extends EventEmitter |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | private $buffer = ''; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var LoopInterface |
||
| 37 | */ |
||
| 38 | private $loop; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var \BitWasp\Bitcoin\Networking\Messages\Factory |
||
| 42 | */ |
||
| 43 | private $msgs; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var Stream |
||
| 47 | */ |
||
| 48 | private $stream; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var Version |
||
| 52 | */ |
||
| 53 | private $localVersion; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var Version |
||
| 57 | */ |
||
| 58 | private $remoteVersion; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var NetworkAddressInterface |
||
| 62 | */ |
||
| 63 | private $peerAddress; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var ConnectionParams |
||
| 67 | */ |
||
| 68 | private $connectionParams; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var bool |
||
| 72 | */ |
||
| 73 | private $exchangedVersion = false; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param \BitWasp\Bitcoin\Networking\Messages\Factory $msgs |
||
| 77 | * @param LoopInterface $loop |
||
| 78 | */ |
||
| 79 | 12 | public function __construct(\BitWasp\Bitcoin\Networking\Messages\Factory $msgs, LoopInterface $loop) |
|
| 84 | |||
| 85 | /** |
||
| 86 | * @return Version |
||
| 87 | */ |
||
| 88 | public function getLocalVersion() |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @return Version |
||
| 95 | */ |
||
| 96 | public function getRemoteVersion() |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Reliably returns the remote peers NetAddr when known through |
||
| 103 | * the connection process. Often better than the data contained |
||
| 104 | * in a Version message. |
||
| 105 | * |
||
| 106 | * @return NetworkAddressInterface |
||
| 107 | */ |
||
| 108 | public function getRemoteAddress() |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @return ConnectionParams |
||
| 115 | */ |
||
| 116 | public function getConnectionParams() |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param NetworkSerializable $msg |
||
| 123 | */ |
||
| 124 | 12 | public function send(NetworkSerializable $msg) |
|
| 130 | |||
| 131 | /** |
||
| 132 | * Handler for incoming data. Buffers possibly fragmented packets since they arrive sequentially. |
||
| 133 | * Before finishing the version exchange, this will only emit Version and VerAck messages. |
||
| 134 | */ |
||
| 135 | 12 | private function onData() |
|
| 150 | |||
| 151 | /** |
||
| 152 | * @param Stream $stream |
||
| 153 | * @return $this |
||
| 154 | */ |
||
| 155 | 12 | public function setupStream(Stream $stream) |
|
| 177 | |||
| 178 | /** |
||
| 179 | * @param Stream $connection |
||
| 180 | * @param ConnectionParams $params |
||
| 181 | * @return \React\Promise\Promise|\React\Promise\PromiseInterface |
||
| 182 | */ |
||
| 183 | 9 | public function inboundHandshake(Stream $connection, ConnectionParams $params) |
|
| 214 | |||
| 215 | /** |
||
| 216 | * @param NetworkAddressInterface $remotePeer |
||
| 217 | * @param ConnectionParams $params |
||
| 218 | * @return \React\Promise\Promise|\React\Promise\PromiseInterface |
||
| 219 | */ |
||
| 220 | 12 | public function outboundHandshake(NetworkAddressInterface $remotePeer, ConnectionParams $params) |
|
| 243 | |||
| 244 | /** |
||
| 245 | * |
||
| 246 | */ |
||
| 247 | public function intentionalClose() |
||
| 252 | |||
| 253 | /** |
||
| 254 | * |
||
| 255 | */ |
||
| 256 | 9 | public function close() |
|
| 262 | |||
| 263 | /** |
||
| 264 | * @param int $protocolVersion |
||
| 265 | * @param BufferInterface $services |
||
| 266 | * @param int $timestamp |
||
| 267 | * @param NetworkAddressInterface $remoteAddr |
||
| 268 | * @param NetworkAddressInterface $localAddr |
||
| 269 | * @param string $userAgent |
||
| 270 | * @param int $blockHeight |
||
| 271 | * @param bool $relayToUs |
||
| 272 | */ |
||
| 273 | public function version( |
||
| 294 | |||
| 295 | /** |
||
| 296 | * |
||
| 297 | */ |
||
| 298 | 12 | public function verack() |
|
| 302 | |||
| 303 | /** |
||
| 304 | * |
||
| 305 | */ |
||
| 306 | public function sendheaders() |
||
| 310 | |||
| 311 | /** |
||
| 312 | * @param Inventory[] $vInv |
||
| 313 | */ |
||
| 314 | public function inv(array $vInv) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @param Inventory[] $vInv |
||
| 321 | */ |
||
| 322 | public function getdata(array $vInv) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @param array $vInv |
||
| 329 | */ |
||
| 330 | public function notfound(array $vInv) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @param NetworkAddressTimestamp[] $vNetAddr |
||
| 337 | */ |
||
| 338 | public function addr(array $vNetAddr) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * |
||
| 345 | */ |
||
| 346 | public function getaddr() |
||
| 350 | |||
| 351 | /** |
||
| 352 | * |
||
| 353 | */ |
||
| 354 | public function ping() |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @param Ping $ping |
||
| 361 | */ |
||
| 362 | public function pong(Ping $ping) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @param TransactionInterface $tx |
||
| 369 | */ |
||
| 370 | public function tx(TransactionInterface $tx) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * @param BlockLocator $locator |
||
| 377 | */ |
||
| 378 | public function getblocks(BlockLocator $locator) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @param BlockLocator $locator |
||
| 388 | */ |
||
| 389 | public function getheaders(BlockLocator $locator) |
||
| 396 | |||
| 397 | /** |
||
| 398 | * @param BlockInterface $block |
||
| 399 | */ |
||
| 400 | public function block(BlockInterface $block) |
||
| 404 | |||
| 405 | /** |
||
| 406 | * @param array $vHeaders |
||
| 407 | */ |
||
| 408 | public function headers(array $vHeaders) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @param AlertDetail $detail |
||
| 415 | * @param SignatureInterface $signature |
||
| 416 | */ |
||
| 417 | public function alert(AlertDetail $detail, SignatureInterface $signature) |
||
| 421 | |||
| 422 | /** |
||
| 423 | * @param int $feeRate |
||
| 424 | */ |
||
| 425 | public function feefilter($feeRate) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * @param BufferInterface $data |
||
| 432 | */ |
||
| 433 | public function filteradd(BufferInterface $data) |
||
| 437 | |||
| 438 | /** |
||
| 439 | * @param BloomFilter $filter |
||
| 440 | */ |
||
| 441 | public function filterload(BloomFilter $filter) |
||
| 445 | |||
| 446 | /** |
||
| 447 | * |
||
| 448 | */ |
||
| 449 | public function filterclear() |
||
| 453 | |||
| 454 | /** |
||
| 455 | * @param FilteredBlock $filtered |
||
| 456 | */ |
||
| 457 | public function merkleblock(FilteredBlock $filtered) |
||
| 461 | |||
| 462 | /** |
||
| 463 | * |
||
| 464 | */ |
||
| 465 | public function mempool() |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Issue a Reject message, with a required $msg, $code, and $reason |
||
| 472 | * |
||
| 473 | * @param BufferInterface $msg |
||
| 474 | * @param int $code |
||
| 475 | * @param BufferInterface $reason |
||
| 476 | * @param BufferInterface $data |
||
| 477 | */ |
||
| 478 | public function reject(BufferInterface $msg, $code, BufferInterface $reason, BufferInterface $data = null) |
||
| 482 | } |
||
| 483 |