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 |
||
| 30 | class Peer extends EventEmitter |
||
| 31 | { |
||
| 32 | const USER_AGENT = "bitcoin-php/v0.1"; |
||
| 33 | const PROTOCOL_VERSION = "70000"; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | private $buffer = ''; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var NetworkAddressInterface |
||
| 42 | */ |
||
| 43 | private $localAddr; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var NetworkAddressInterface |
||
| 47 | */ |
||
| 48 | private $remoteAddr; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var BloomFilter |
||
| 52 | */ |
||
| 53 | private $filter; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var LoopInterface |
||
| 57 | */ |
||
| 58 | private $loop; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var \BitWasp\Bitcoin\Networking\Messages\Factory |
||
| 62 | */ |
||
| 63 | private $msgs; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var Stream |
||
| 67 | */ |
||
| 68 | private $stream; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var bool |
||
| 72 | */ |
||
| 73 | private $exchangedVersion = false; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var int |
||
| 77 | */ |
||
| 78 | private $lastPongTime; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var bool |
||
| 82 | */ |
||
| 83 | private $inbound; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Whether we want this peer to relay tx's to us. |
||
| 87 | * @var bool |
||
| 88 | */ |
||
| 89 | private $relayToUs = false; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var bool |
||
| 93 | */ |
||
| 94 | private $relayToPeer = false; |
||
| 95 | |||
| 96 | private $downloadPeer = false; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @param NetworkAddressInterface $local |
||
| 100 | * @param \BitWasp\Bitcoin\Networking\Messages\Factory $msgs |
||
| 101 | * @param LoopInterface $loop |
||
| 102 | */ |
||
| 103 | 17 | public function __construct( |
|
| 113 | |||
| 114 | /** |
||
| 115 | * @return bool |
||
| 116 | */ |
||
| 117 | public function isDownloadPeer() |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param $flag |
||
| 124 | */ |
||
| 125 | public function setDownloadPeerFlag($flag) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @return NetworkAddressInterface |
||
| 136 | */ |
||
| 137 | public function getRemoteAddr() |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @return NetworkAddressInterface |
||
| 144 | */ |
||
| 145 | public function getLocalAddr() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Set to true by calling requestRelay() |
||
| 152 | * @return bool |
||
| 153 | */ |
||
| 154 | public function checkWeRequestedRelay() |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Check if peer sent version message requesting relay, or, set a filter. |
||
| 161 | * @return bool |
||
| 162 | */ |
||
| 163 | public function checkPeerRequestedRelay() |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Must be called before connect(). This tells the remote node to relay transactions to us. |
||
| 170 | */ |
||
| 171 | 3 | public function requestRelay() |
|
| 176 | |||
| 177 | /** |
||
| 178 | * @return bool |
||
| 179 | */ |
||
| 180 | public function hasFilter() |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @return BloomFilter |
||
| 187 | * @throws \Exception |
||
| 188 | */ |
||
| 189 | public function getFilter() |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @return bool |
||
| 200 | */ |
||
| 201 | public function ready() |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @param NetworkSerializable $msg |
||
| 208 | */ |
||
| 209 | 14 | public function send(NetworkSerializable $msg) |
|
| 215 | |||
| 216 | /** |
||
| 217 | * Handler for incoming data. Buffers possibly fragmented packets since they arrive sequentially. |
||
| 218 | * Before finishing the version exchange, this will only emit Version and VerAck messages. |
||
| 219 | */ |
||
| 220 | 14 | private function onData() |
|
| 238 | |||
| 239 | /** |
||
| 240 | * Initializes basic peer functionality - used in server and client contexts |
||
| 241 | * |
||
| 242 | * @return $this |
||
| 243 | */ |
||
| 244 | 14 | private function setupConnection() |
|
| 265 | |||
| 266 | /** |
||
| 267 | * @param int $timeout |
||
| 268 | * @return $this |
||
| 269 | */ |
||
| 270 | public function timeoutWithoutVersion($timeout = 20) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * @param Connection $connection |
||
| 286 | * @return \React\Promise\Promise|\React\Promise\PromiseInterface |
||
| 287 | */ |
||
| 288 | 9 | public function inboundConnection(Connection $connection) |
|
| 311 | |||
| 312 | /** |
||
| 313 | * @param Connector $connector |
||
| 314 | * @param NetworkAddressInterface $remoteAddr |
||
| 315 | * @return \React\Promise\Promise|\React\Promise\PromiseInterface |
||
| 316 | */ |
||
| 317 | 14 | public function connect(Connector $connector, NetworkAddressInterface $remoteAddr) |
|
| 350 | |||
| 351 | /** |
||
| 352 | * |
||
| 353 | */ |
||
| 354 | public function intentionalClose() |
||
| 359 | |||
| 360 | /** |
||
| 361 | * |
||
| 362 | */ |
||
| 363 | 9 | public function close() |
|
| 369 | |||
| 370 | /** |
||
| 371 | * @return \BitWasp\Bitcoin\Networking\Messages\Version |
||
| 372 | */ |
||
| 373 | 14 | public function version() |
|
| 386 | |||
| 387 | /** |
||
| 388 | * |
||
| 389 | */ |
||
| 390 | 14 | public function verack() |
|
| 394 | |||
| 395 | /** |
||
| 396 | * |
||
| 397 | */ |
||
| 398 | public function sendheaders() |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @param Inventory[] $vInv |
||
| 405 | */ |
||
| 406 | public function inv(array $vInv) |
||
| 410 | |||
| 411 | /** |
||
| 412 | * @param Inventory[] $vInv |
||
| 413 | */ |
||
| 414 | public function getdata(array $vInv) |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @param array $vInv |
||
| 421 | */ |
||
| 422 | public function notfound(array $vInv) |
||
| 426 | |||
| 427 | /** |
||
| 428 | * @param NetworkAddressTimestamp[] $vNetAddr |
||
| 429 | */ |
||
| 430 | public function addr(array $vNetAddr) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * |
||
| 437 | */ |
||
| 438 | public function getaddr() |
||
| 442 | |||
| 443 | /** |
||
| 444 | * |
||
| 445 | */ |
||
| 446 | public function ping() |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @param Ping $ping |
||
| 453 | */ |
||
| 454 | public function pong(Ping $ping) |
||
| 458 | |||
| 459 | /** |
||
| 460 | * @param TransactionInterface $tx |
||
| 461 | */ |
||
| 462 | public function tx(TransactionInterface $tx) |
||
| 466 | |||
| 467 | /** |
||
| 468 | * @param BlockLocator $locator |
||
| 469 | */ |
||
| 470 | public function getblocks(BlockLocator $locator) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * @param BlockLocator $locator |
||
| 480 | */ |
||
| 481 | public function getheaders(BlockLocator $locator) |
||
| 488 | |||
| 489 | /** |
||
| 490 | * @param BlockInterface $block |
||
| 491 | */ |
||
| 492 | public function block(BlockInterface $block) |
||
| 496 | |||
| 497 | /** |
||
| 498 | * @param array $vHeaders |
||
| 499 | */ |
||
| 500 | public function headers(array $vHeaders) |
||
| 504 | |||
| 505 | /** |
||
| 506 | * @param AlertDetail $detail |
||
| 507 | * @param SignatureInterface $signature |
||
| 508 | */ |
||
| 509 | public function alert(AlertDetail $detail, SignatureInterface $signature) |
||
| 513 | |||
| 514 | /** |
||
| 515 | * @param int $feeRate |
||
| 516 | */ |
||
| 517 | public function feefilter($feeRate) |
||
| 521 | |||
| 522 | /** |
||
| 523 | * @param BufferInterface $data |
||
| 524 | */ |
||
| 525 | public function filteradd(BufferInterface $data) |
||
| 529 | |||
| 530 | /** |
||
| 531 | * @param BloomFilter $filter |
||
| 532 | */ |
||
| 533 | public function filterload(BloomFilter $filter) |
||
| 537 | |||
| 538 | /** |
||
| 539 | * |
||
| 540 | */ |
||
| 541 | public function filterclear() |
||
| 545 | |||
| 546 | /** |
||
| 547 | * @param FilteredBlock $filtered |
||
| 548 | */ |
||
| 549 | public function merkleblock(FilteredBlock $filtered) |
||
| 553 | |||
| 554 | /** |
||
| 555 | * |
||
| 556 | */ |
||
| 557 | public function mempool() |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Issue a Reject message, with a required $msg, $code, and $reason |
||
| 564 | * |
||
| 565 | * @param BufferInterface $msg |
||
| 566 | * @param int $code |
||
| 567 | * @param BufferInterface $reason |
||
| 568 | * @param BufferInterface $data |
||
| 569 | */ |
||
| 570 | public function reject(BufferInterface $msg, $code, BufferInterface $reason, BufferInterface $data = null) |
||
| 574 | } |
||
| 575 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: