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 | const USER_AGENT = "bitcoin-php/v0.1"; |
||
32 | const PROTOCOL_VERSION = "70000"; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | private $buffer = ''; |
||
38 | |||
39 | /** |
||
40 | * @var NetworkAddressInterface |
||
41 | */ |
||
42 | private $localAddr; |
||
43 | |||
44 | /** |
||
45 | * @var NetworkAddressInterface |
||
46 | */ |
||
47 | private $remoteAddr; |
||
48 | |||
49 | /** |
||
50 | * @var BloomFilter |
||
51 | */ |
||
52 | private $filter; |
||
53 | |||
54 | /** |
||
55 | * @var LoopInterface |
||
56 | */ |
||
57 | private $loop; |
||
58 | |||
59 | /** |
||
60 | * @var \BitWasp\Bitcoin\Networking\Messages\Factory |
||
61 | */ |
||
62 | private $msgs; |
||
63 | |||
64 | /** |
||
65 | * @var Stream |
||
66 | */ |
||
67 | private $stream; |
||
68 | |||
69 | /** |
||
70 | * @var bool |
||
71 | */ |
||
72 | private $exchangedVersion = false; |
||
73 | |||
74 | /** |
||
75 | * @var int |
||
76 | */ |
||
77 | private $lastPongTime; |
||
78 | |||
79 | /** |
||
80 | * @var bool |
||
81 | */ |
||
82 | private $inbound; |
||
83 | |||
84 | /** |
||
85 | * Whether we want this peer to relay tx's to us. |
||
86 | * @var bool |
||
87 | */ |
||
88 | private $relayToUs = false; |
||
89 | |||
90 | /** |
||
91 | * @var bool |
||
92 | */ |
||
93 | private $relayToPeer = false; |
||
94 | |||
95 | private $downloadPeer = false; |
||
96 | |||
97 | /** |
||
98 | * @param NetworkAddressInterface $local |
||
99 | * @param \BitWasp\Bitcoin\Networking\Messages\Factory $msgs |
||
100 | * @param LoopInterface $loop |
||
101 | */ |
||
102 | 18 | public function __construct( |
|
112 | |||
113 | /** |
||
114 | * @return bool |
||
115 | */ |
||
116 | public function isDownloadPeer() |
||
120 | |||
121 | /** |
||
122 | * @param $flag |
||
123 | */ |
||
124 | public function setDownloadPeerFlag($flag) |
||
132 | |||
133 | /** |
||
134 | * @return NetworkAddressInterface |
||
135 | */ |
||
136 | public function getRemoteAddr() |
||
140 | |||
141 | /** |
||
142 | * @return NetworkAddressInterface |
||
143 | */ |
||
144 | public function getLocalAddr() |
||
148 | |||
149 | /** |
||
150 | * Set to true by calling requestRelay() |
||
151 | * @return bool |
||
152 | */ |
||
153 | public function checkWeRequestedRelay() |
||
157 | |||
158 | /** |
||
159 | * Check if peer sent version message requesting relay, or, set a filter. |
||
160 | * @return bool |
||
161 | */ |
||
162 | public function checkPeerRequestedRelay() |
||
166 | |||
167 | /** |
||
168 | * Must be called before connect(). This tells the remote node to relay transactions to us. |
||
169 | */ |
||
170 | 3 | public function requestRelay() |
|
175 | |||
176 | /** |
||
177 | * @return bool |
||
178 | */ |
||
179 | public function hasFilter() |
||
183 | |||
184 | /** |
||
185 | * @return BloomFilter |
||
186 | * @throws \Exception |
||
187 | */ |
||
188 | public function getFilter() |
||
196 | |||
197 | /** |
||
198 | * @return bool |
||
199 | */ |
||
200 | public function ready() |
||
204 | |||
205 | /** |
||
206 | * @param NetworkSerializable $msg |
||
207 | */ |
||
208 | 15 | public function send(NetworkSerializable $msg) |
|
214 | |||
215 | /** |
||
216 | * Handler for incoming data. Buffers possibly fragmented packets since they arrive sequentially. |
||
217 | * Before finishing the version exchange, this will only emit Version and VerAck messages. |
||
218 | */ |
||
219 | 15 | private function onData() |
|
237 | |||
238 | /** |
||
239 | * Initializes basic peer functionality - used in server and client contexts |
||
240 | * |
||
241 | * @return $this |
||
242 | */ |
||
243 | 15 | private function setupConnection() |
|
264 | |||
265 | /** |
||
266 | * @param int $timeout |
||
267 | * @return $this |
||
268 | */ |
||
269 | public function timeoutWithoutVersion($timeout = 20) |
||
282 | |||
283 | /** |
||
284 | * @param Connection $connection |
||
285 | * @return \React\Promise\Promise|\React\Promise\PromiseInterface |
||
286 | */ |
||
287 | 9 | public function inboundConnection(Connection $connection) |
|
310 | |||
311 | /** |
||
312 | * @param Connector $connector |
||
313 | * @param NetworkAddressInterface $remoteAddr |
||
314 | * @return \React\Promise\Promise|\React\Promise\PromiseInterface |
||
315 | */ |
||
316 | 15 | public function connect(Connector $connector, NetworkAddressInterface $remoteAddr) |
|
349 | |||
350 | /** |
||
351 | * |
||
352 | */ |
||
353 | public function intentionalClose() |
||
358 | |||
359 | /** |
||
360 | * |
||
361 | */ |
||
362 | 9 | public function close() |
|
368 | |||
369 | /** |
||
370 | * @return \BitWasp\Bitcoin\Networking\Messages\Version |
||
371 | */ |
||
372 | 15 | public function version() |
|
385 | |||
386 | /** |
||
387 | * |
||
388 | */ |
||
389 | 15 | public function verack() |
|
393 | |||
394 | /** |
||
395 | * @param Inventory[] $vInv |
||
396 | */ |
||
397 | public function inv(array $vInv) |
||
401 | |||
402 | /** |
||
403 | * @param Inventory[] $vInv |
||
404 | */ |
||
405 | public function getdata(array $vInv) |
||
409 | |||
410 | /** |
||
411 | * @param array $vInv |
||
412 | */ |
||
413 | public function notfound(array $vInv) |
||
417 | |||
418 | /** |
||
419 | * @param NetworkAddressTimestamp[] $vNetAddr |
||
420 | */ |
||
421 | public function addr(array $vNetAddr) |
||
425 | |||
426 | /** |
||
427 | * |
||
428 | */ |
||
429 | public function getaddr() |
||
433 | |||
434 | /** |
||
435 | * |
||
436 | */ |
||
437 | public function ping() |
||
441 | |||
442 | /** |
||
443 | * @param Ping $ping |
||
444 | */ |
||
445 | public function pong(Ping $ping) |
||
449 | |||
450 | /** |
||
451 | * @param TransactionInterface $tx |
||
452 | */ |
||
453 | public function tx(TransactionInterface $tx) |
||
457 | |||
458 | /** |
||
459 | * @param BlockLocator $locator |
||
460 | */ |
||
461 | public function getblocks(BlockLocator $locator) |
||
468 | |||
469 | /** |
||
470 | * @param BlockLocator $locator |
||
471 | */ |
||
472 | public function getheaders(BlockLocator $locator) |
||
479 | |||
480 | /** |
||
481 | * @param BlockInterface $block |
||
482 | */ |
||
483 | public function block(BlockInterface $block) |
||
487 | |||
488 | /** |
||
489 | * @param array $vHeaders |
||
490 | */ |
||
491 | public function headers(array $vHeaders) |
||
495 | |||
496 | /** |
||
497 | * @param AlertDetail $detail |
||
498 | * @param SignatureInterface $signature |
||
499 | */ |
||
500 | public function alert(AlertDetail $detail, SignatureInterface $signature) |
||
504 | |||
505 | /** |
||
506 | * @param Buffer $data |
||
507 | */ |
||
508 | public function filteradd(Buffer $data) |
||
512 | |||
513 | /** |
||
514 | * @param BloomFilter $filter |
||
515 | */ |
||
516 | public function filterload(BloomFilter $filter) |
||
520 | |||
521 | /** |
||
522 | * |
||
523 | */ |
||
524 | public function filterclear() |
||
528 | |||
529 | /** |
||
530 | * @param FilteredBlock $filtered |
||
531 | */ |
||
532 | public function merkleblock(FilteredBlock $filtered) |
||
536 | |||
537 | /** |
||
538 | * |
||
539 | */ |
||
540 | public function mempool() |
||
544 | |||
545 | /** |
||
546 | * Issue a Reject message, with a required $msg, $code, and $reason |
||
547 | * |
||
548 | * @param Buffer $msg |
||
549 | * @param int $code |
||
550 | * @param Buffer $reason |
||
551 | * @param Buffer $data |
||
552 | */ |
||
553 | public function reject(Buffer $msg, $code, Buffer $reason, Buffer $data = null) |
||
557 | } |
||
558 |
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: