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 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | private $buffer = ''; |
||
36 | |||
37 | /** |
||
38 | * @var LoopInterface |
||
39 | */ |
||
40 | private $loop; |
||
41 | |||
42 | /** |
||
43 | * @var \BitWasp\Bitcoin\Networking\Messages\Factory |
||
44 | */ |
||
45 | private $msgs; |
||
46 | |||
47 | /** |
||
48 | * @var Stream |
||
49 | */ |
||
50 | private $stream; |
||
51 | |||
52 | /** |
||
53 | * @var Version |
||
54 | */ |
||
55 | private $localVersion; |
||
56 | |||
57 | /** |
||
58 | * @var Version |
||
59 | */ |
||
60 | private $remoteVersion; |
||
61 | |||
62 | /** |
||
63 | * @var NetworkAddressInterface |
||
64 | */ |
||
65 | private $peerAddress; |
||
66 | |||
67 | /** |
||
68 | * @var ConnectionParams |
||
69 | */ |
||
70 | private $connectionParams; |
||
71 | |||
72 | /** |
||
73 | * @var bool |
||
74 | */ |
||
75 | private $exchangedVersion = false; |
||
76 | |||
77 | /** |
||
78 | * @var Header |
||
79 | */ |
||
80 | private $incomingMsgHeader; |
||
81 | |||
82 | /** |
||
83 | * @param \BitWasp\Bitcoin\Networking\Messages\Factory $msgs |
||
84 | * @param LoopInterface $loop |
||
85 | */ |
||
86 | 11 | public function __construct(\BitWasp\Bitcoin\Networking\Messages\Factory $msgs, LoopInterface $loop) |
|
91 | |||
92 | /** |
||
93 | * @return Version |
||
94 | */ |
||
95 | 3 | public function getLocalVersion() |
|
99 | |||
100 | /** |
||
101 | * @return Version |
||
102 | */ |
||
103 | 3 | public function getRemoteVersion() |
|
107 | |||
108 | /** |
||
109 | * Reliably returns the remote peers NetAddr when known through |
||
110 | * the connection process. Often better than the data contained |
||
111 | * in a Version message. |
||
112 | * |
||
113 | * @return NetworkAddressInterface |
||
114 | */ |
||
115 | 3 | public function getRemoteAddress() |
|
119 | |||
120 | /** |
||
121 | * @return ConnectionParams |
||
122 | */ |
||
123 | 3 | public function getConnectionParams() |
|
127 | |||
128 | /** |
||
129 | * @param NetworkSerializable $msg |
||
130 | */ |
||
131 | 11 | public function send(NetworkSerializable $msg) |
|
138 | |||
139 | /** |
||
140 | * @param Stream $stream |
||
141 | * @return $this |
||
142 | */ |
||
143 | 11 | public function setupStream(ConnectionInterface $stream) |
|
192 | |||
193 | /** |
||
194 | * @param ConnectionInterface $connection |
||
195 | * @param ConnectionParams $params |
||
196 | * @return \React\Promise\Promise|\React\Promise\PromiseInterface |
||
197 | */ |
||
198 | 9 | public function inboundHandshake(ConnectionInterface $connection, ConnectionParams $params) |
|
221 | |||
222 | /** |
||
223 | * @param NetworkAddressInterface $remotePeer |
||
224 | * @param ConnectionParams $params |
||
225 | * @return \React\Promise\Promise|\React\Promise\PromiseInterface |
||
226 | */ |
||
227 | 11 | public function outboundHandshake(NetworkAddressInterface $remotePeer, ConnectionParams $params) |
|
260 | |||
261 | /** |
||
262 | * |
||
263 | */ |
||
264 | public function intentionalClose() |
||
269 | |||
270 | /** |
||
271 | * |
||
272 | */ |
||
273 | 8 | public function close() |
|
279 | |||
280 | /** |
||
281 | * @param int $protocolVersion |
||
282 | * @param int $services |
||
283 | * @param int $timestamp |
||
284 | * @param NetworkAddressInterface $remoteAddr |
||
285 | * @param NetworkAddressInterface $localAddr |
||
286 | * @param string $userAgent |
||
287 | * @param int $blockHeight |
||
288 | * @param bool $relayToUs |
||
289 | */ |
||
290 | public function version( |
||
311 | |||
312 | /** |
||
313 | * |
||
314 | */ |
||
315 | 11 | public function verack() |
|
319 | |||
320 | /** |
||
321 | * |
||
322 | */ |
||
323 | public function sendheaders() |
||
327 | |||
328 | /** |
||
329 | * @param Inventory[] $vInv |
||
330 | */ |
||
331 | public function inv(array $vInv) |
||
335 | |||
336 | /** |
||
337 | * @param Inventory[] $vInv |
||
338 | */ |
||
339 | public function getdata(array $vInv) |
||
343 | |||
344 | /** |
||
345 | * @param array $vInv |
||
346 | */ |
||
347 | public function notfound(array $vInv) |
||
351 | |||
352 | /** |
||
353 | * @param NetworkAddressTimestamp[] $vNetAddr |
||
354 | */ |
||
355 | public function addr(array $vNetAddr) |
||
359 | |||
360 | /** |
||
361 | * |
||
362 | */ |
||
363 | public function getaddr() |
||
367 | |||
368 | /** |
||
369 | * |
||
370 | */ |
||
371 | public function ping() |
||
375 | |||
376 | /** |
||
377 | * @param Ping $ping |
||
378 | */ |
||
379 | public function pong(Ping $ping) |
||
383 | |||
384 | /** |
||
385 | * @param TransactionInterface $tx |
||
386 | */ |
||
387 | public function tx(TransactionInterface $tx) |
||
391 | |||
392 | /** |
||
393 | * @param BlockLocator $locator |
||
394 | */ |
||
395 | public function getblocks(BlockLocator $locator) |
||
402 | |||
403 | /** |
||
404 | * @param BlockLocator $locator |
||
405 | */ |
||
406 | public function getheaders(BlockLocator $locator) |
||
413 | |||
414 | /** |
||
415 | * @param BlockInterface $block |
||
416 | */ |
||
417 | public function block(BlockInterface $block) |
||
421 | |||
422 | /** |
||
423 | * @param array $vHeaders |
||
424 | */ |
||
425 | public function headers(array $vHeaders) |
||
429 | |||
430 | /** |
||
431 | * @param AlertDetail $detail |
||
432 | * @param SignatureInterface $signature |
||
433 | */ |
||
434 | public function alert(AlertDetail $detail, SignatureInterface $signature) |
||
438 | |||
439 | /** |
||
440 | * @param int $feeRate |
||
441 | */ |
||
442 | public function feefilter($feeRate) |
||
446 | |||
447 | /** |
||
448 | * @param BufferInterface $data |
||
449 | */ |
||
450 | public function filteradd(BufferInterface $data) |
||
454 | |||
455 | /** |
||
456 | * @param BloomFilter $filter |
||
457 | */ |
||
458 | public function filterload(BloomFilter $filter) |
||
462 | |||
463 | /** |
||
464 | * |
||
465 | */ |
||
466 | public function filterclear() |
||
470 | |||
471 | /** |
||
472 | * @param FilteredBlock $filtered |
||
473 | */ |
||
474 | public function merkleblock(FilteredBlock $filtered) |
||
478 | |||
479 | /** |
||
480 | * |
||
481 | */ |
||
482 | public function mempool() |
||
486 | |||
487 | /** |
||
488 | * Issue a Reject message, with a required $msg, $code, and $reason |
||
489 | * |
||
490 | * @param BufferInterface $msg |
||
491 | * @param int $code |
||
492 | * @param BufferInterface $reason |
||
493 | * @param BufferInterface $data |
||
494 | */ |
||
495 | public function reject(BufferInterface $msg, $code, BufferInterface $reason, BufferInterface $data = null) |
||
499 | } |
||
500 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..