Completed
Push — master ( f7f683...a725ae )
by thomas
34:30 queued 31:27
created

Connector::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
cc 1
eloc 5
nc 1
nop 4
crap 1
1
<?php
2
3
namespace BitWasp\Bitcoin\Networking\Peer;
4
5
use BitWasp\Bitcoin\Networking\Messages\Factory as MsgFactory;
6
use BitWasp\Bitcoin\Networking\Structure\NetworkAddressInterface;
7
use React\Dns\Resolver\Resolver;
8
use React\EventLoop\LoopInterface;
9
use React\Stream\Stream;
10
11
class Connector extends \React\SocketClient\Connector
12
{
13
    /**
14
     * @var ConnectionParams
15
     */
16
    private $params;
17
18
    /**
19
     * @var MsgFactory
20
     */
21
    private $msgs;
22
23
    /**
24
     * @var LoopInterface
25
     */
26
    private $loop;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
27
28
    /**
29
     * Connector constructor.
30
     * @param MsgFactory $msgs
31
     * @param ConnectionParams $params
32
     * @param LoopInterface $loop
33
     * @param Resolver $resolver
34
     */
35 12
    public function __construct(MsgFactory $msgs, ConnectionParams $params, LoopInterface $loop, Resolver $resolver)
36
    {
37 12
        $this->params = $params;
38 12
        $this->msgs = $msgs;
39 12
        $this->loop = $loop;
40
41 12
        parent::__construct($loop, $resolver);
42 12
    }
43
44
    /**
45
     * @param NetworkAddressInterface $remotePeer
46
     * @return \React\Promise\PromiseInterface|static
47
     */
48 12
    public function rawConnect(NetworkAddressInterface $remotePeer)
49
    {
50 12
        return $this
51 12
            ->create($remotePeer->getIp(), $remotePeer->getPort())
52
            ->then(function (Stream $stream) {
53 12
                $peer = new Peer($this->msgs, $this->loop);
54 12
                $peer->setupStream($stream);
55 12
                return $peer;
56 12
            });
57
    }
58
59
    /**
60
     * @param NetworkAddressInterface $remotePeer
61
     * @return \React\Promise\PromiseInterface|static
62
     */
63 12
    public function connect(NetworkAddressInterface $remotePeer)
64
    {
65 12
        return $this
0 ignored issues
show
Bug introduced by
The method then does only exist in React\Promise\PromiseInterface, but not in BitWasp\Bitcoin\Networking\Peer\Connector.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
66 12
            ->rawConnect($remotePeer)
67 12
            ->then(function (Peer $peer) use ($remotePeer) {
68 12
                return $peer->outboundHandshake($remotePeer, $this->params);
69 12
            });
70
    }
71
}
72