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 |
||
| 29 | class Peer extends EventEmitter |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | private $buffer = ''; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var LoopInterface |
||
| 38 | */ |
||
| 39 | private $loop; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var \BitWasp\Bitcoin\Networking\Messages\Factory |
||
| 43 | */ |
||
| 44 | private $msgs; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var Stream |
||
| 48 | */ |
||
| 49 | private $stream; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var Version |
||
| 53 | */ |
||
| 54 | private $localVersion; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var Version |
||
| 58 | */ |
||
| 59 | private $remoteVersion; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var NetworkAddressInterface |
||
| 63 | */ |
||
| 64 | private $peerAddress; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var ConnectionParams |
||
| 68 | */ |
||
| 69 | private $connectionParams; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var bool |
||
| 73 | */ |
||
| 74 | private $exchangedVersion = false; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param \BitWasp\Bitcoin\Networking\Messages\Factory $msgs |
||
| 78 | * @param LoopInterface $loop |
||
| 79 | */ |
||
| 80 | 12 | public function __construct(\BitWasp\Bitcoin\Networking\Messages\Factory $msgs, LoopInterface $loop) |
|
| 85 | |||
| 86 | /** |
||
| 87 | * @return Version |
||
| 88 | */ |
||
| 89 | 3 | public function getLocalVersion() |
|
| 90 | { |
||
| 91 | 3 | return $this->localVersion; |
|
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @return Version |
||
| 96 | */ |
||
| 97 | 3 | public function getRemoteVersion() |
|
| 98 | { |
||
| 99 | 3 | return $this->remoteVersion; |
|
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Reliably returns the remote peers NetAddr when known through |
||
| 104 | * the connection process. Often better than the data contained |
||
| 105 | * in a Version message. |
||
| 106 | * |
||
| 107 | * @return NetworkAddressInterface |
||
| 108 | */ |
||
| 109 | 3 | public function getRemoteAddress() |
|
| 110 | { |
||
| 111 | 3 | return $this->peerAddress; |
|
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @return ConnectionParams |
||
| 116 | */ |
||
| 117 | 3 | public function getConnectionParams() |
|
| 118 | { |
||
| 119 | 3 | return $this->connectionParams; |
|
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param NetworkSerializable $msg |
||
| 124 | */ |
||
| 125 | 12 | public function send(NetworkSerializable $msg) |
|
| 131 | |||
| 132 | /** |
||
| 133 | * Handler for incoming data. Buffers possibly fragmented packets since they arrive sequentially. |
||
| 134 | * Before finishing the version exchange, this will only emit Version and VerAck messages. |
||
| 135 | */ |
||
| 136 | 12 | private function onData() |
|
| 151 | |||
| 152 | /** |
||
| 153 | * @param Stream $stream |
||
| 154 | * @return $this |
||
| 155 | */ |
||
| 156 | 12 | public function setupStream(Stream $stream) |
|
| 178 | |||
| 179 | /** |
||
| 180 | * @param Stream $connection |
||
| 181 | * @param ConnectionParams $params |
||
| 182 | * @return \React\Promise\Promise|\React\Promise\PromiseInterface |
||
| 183 | */ |
||
| 184 | 9 | public function inboundHandshake(Stream $connection, ConnectionParams $params) |
|
| 216 | |||
| 217 | /** |
||
| 218 | * @param NetworkAddressInterface $remotePeer |
||
| 219 | * @param ConnectionParams $params |
||
| 220 | * @return \React\Promise\Promise|\React\Promise\PromiseInterface |
||
| 221 | */ |
||
| 222 | 12 | public function outboundHandshake(NetworkAddressInterface $remotePeer, ConnectionParams $params) |
|
| 247 | |||
| 248 | /** |
||
| 249 | * |
||
| 250 | */ |
||
| 251 | public function intentionalClose() |
||
| 256 | |||
| 257 | /** |
||
| 258 | * |
||
| 259 | */ |
||
| 260 | 9 | public function close() |
|
| 266 | |||
| 267 | /** |
||
| 268 | * @param int $protocolVersion |
||
| 269 | * @param int $services |
||
| 270 | * @param int $timestamp |
||
| 271 | * @param NetworkAddressInterface $remoteAddr |
||
| 272 | * @param NetworkAddressInterface $localAddr |
||
| 273 | * @param string $userAgent |
||
| 274 | * @param int $blockHeight |
||
| 275 | * @param bool $relayToUs |
||
| 276 | */ |
||
| 277 | public function version( |
||
| 298 | |||
| 299 | /** |
||
| 300 | * |
||
| 301 | */ |
||
| 302 | 12 | public function verack() |
|
| 306 | |||
| 307 | /** |
||
| 308 | * |
||
| 309 | */ |
||
| 310 | public function sendheaders() |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @param Inventory[] $vInv |
||
| 317 | */ |
||
| 318 | public function inv(array $vInv) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @param Inventory[] $vInv |
||
| 325 | */ |
||
| 326 | public function getdata(array $vInv) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @param array $vInv |
||
| 333 | */ |
||
| 334 | public function notfound(array $vInv) |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @param NetworkAddressTimestamp[] $vNetAddr |
||
| 341 | */ |
||
| 342 | public function addr(array $vNetAddr) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * |
||
| 349 | */ |
||
| 350 | public function getaddr() |
||
| 354 | |||
| 355 | /** |
||
| 356 | * |
||
| 357 | */ |
||
| 358 | public function ping() |
||
| 362 | |||
| 363 | /** |
||
| 364 | * @param Ping $ping |
||
| 365 | */ |
||
| 366 | public function pong(Ping $ping) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * @param TransactionInterface $tx |
||
| 373 | */ |
||
| 374 | public function tx(TransactionInterface $tx) |
||
| 378 | |||
| 379 | /** |
||
| 380 | * @param BlockLocator $locator |
||
| 381 | */ |
||
| 382 | public function getblocks(BlockLocator $locator) |
||
| 389 | |||
| 390 | /** |
||
| 391 | * @param BlockLocator $locator |
||
| 392 | */ |
||
| 393 | public function getheaders(BlockLocator $locator) |
||
| 400 | |||
| 401 | /** |
||
| 402 | * @param BlockInterface $block |
||
| 403 | */ |
||
| 404 | public function block(BlockInterface $block) |
||
| 408 | |||
| 409 | /** |
||
| 410 | * @param array $vHeaders |
||
| 411 | */ |
||
| 412 | public function headers(array $vHeaders) |
||
| 416 | |||
| 417 | /** |
||
| 418 | * @param AlertDetail $detail |
||
| 419 | * @param SignatureInterface $signature |
||
| 420 | */ |
||
| 421 | public function alert(AlertDetail $detail, SignatureInterface $signature) |
||
| 425 | |||
| 426 | /** |
||
| 427 | * @param int $feeRate |
||
| 428 | */ |
||
| 429 | public function feefilter($feeRate) |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @param BufferInterface $data |
||
| 436 | */ |
||
| 437 | public function filteradd(BufferInterface $data) |
||
| 441 | |||
| 442 | /** |
||
| 443 | * @param BloomFilter $filter |
||
| 444 | */ |
||
| 445 | public function filterload(BloomFilter $filter) |
||
| 449 | |||
| 450 | /** |
||
| 451 | * |
||
| 452 | */ |
||
| 453 | public function filterclear() |
||
| 457 | |||
| 458 | /** |
||
| 459 | * @param FilteredBlock $filtered |
||
| 460 | */ |
||
| 461 | public function merkleblock(FilteredBlock $filtered) |
||
| 465 | |||
| 466 | /** |
||
| 467 | * |
||
| 468 | */ |
||
| 469 | public function mempool() |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Issue a Reject message, with a required $msg, $code, and $reason |
||
| 476 | * |
||
| 477 | * @param BufferInterface $msg |
||
| 478 | * @param int $code |
||
| 479 | * @param BufferInterface $reason |
||
| 480 | * @param BufferInterface $data |
||
| 481 | */ |
||
| 482 | public function reject(BufferInterface $msg, $code, BufferInterface $reason, BufferInterface $data = null) |
||
| 486 | } |
||
| 487 |