Completed
Push — master ( ffb14f...00a863 )
by thomas
24:30 queued 22:12
created

ConnectionParams::setBestBlockHeight()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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\Messages\Version;
7
use BitWasp\Bitcoin\Networking\Services;
8
use BitWasp\Bitcoin\Networking\Structure\NetworkAddress;
9
use BitWasp\Bitcoin\Networking\Structure\NetworkAddressInterface;
10
use BitWasp\Buffertools\Buffer;
11
12
class ConnectionParams
13
{
14
    protected $defaultUserAgent = 'bitcoin-php';
15
    protected $defaultProtocolVersion = '70000';
16
    protected $defaultTxRelay = false;
17
    protected $defaultBlockHeight = '0';
18
    protected $defaultLocalIp = '0.0.0.0';
19
    protected $defaultLocalPort = '0';
20
21
    /**
22
     * @var int
23
     */
24
    private $protocolVersion;
25
26
    /**
27
     * @var int
28
     */
29
    private $timestamp;
30
31
    /**
32
     * @var bool
33
     */
34
    private $txRelay;
35
36
    /**
37
     * @var callable
38
     */
39
    private $bestBlockHeightCallback;
40
41
    /**
42
     * @var int
43
     */
44
    private $bestBlockHeight;
45
46
    /**
47
     * @var string
48
     */
49
    private $localIp;
50
51
    /**
52
     * @var int
53
     */
54
    private $localPort;
55
56
    /**
57
     * @var int
58
     */
59
    private $localServices;
60
61
    /**
62
     * @var string
63
     */
64
    private $userAgent;
65
66
    /**
67
     * @param bool $optRelay
68
     * @return $this
69
     */
70 6
    public function requestTxRelay($optRelay = true)
71
    {
72 6
        if (!is_bool($optRelay)) {
73 3
            throw new \InvalidArgumentException('Invalid txrelay setting, must be a boolean');
74
        }
75
76 3
        $this->txRelay = $optRelay;
77 3
        return $this;
78
    }
79
80
    /**
81
     * @param int $blockHeight
82
     * @return $this
83
     */
84 3
    public function setBestBlockHeight($blockHeight)
85
    {
86 3
        $this->bestBlockHeight = $blockHeight;
87 3
        return $this;
88
    }
89
90
    /**
91
     * @param callable $callable
92
     * @return $this
93
     */
94 3
    public function setBestBlockHeightCallback(callable $callable)
95
    {
96 3
        $this->bestBlockHeightCallback = $callable;
97 3
        return $this;
98
    }
99
100
    /**
101
     * @param int $version
102
     * @return $this
103
     */
104 3
    public function setProtocolVersion($version)
105
    {
106 3
        $this->protocolVersion = $version;
107 3
        return $this;
108
    }
109
110
    /**
111
     * @param string $ip
112
     * @return $this
113
     */
114 6
    public function setLocalIp($ip)
115
    {
116 6
        $this->localIp = $ip;
117 6
        return $this;
118
    }
119
120
    /**
121
     * @param int $port
122
     * @return $this
123
     */
124 6
    public function setLocalPort($port)
125
    {
126 6
        $this->localPort = $port;
127 6
        return $this;
128
    }
129
130
    /**
131
     * @param int $services
132
     * @return $this
133
     */
134 6
    public function setLocalServices($services)
135
    {
136 6
        $this->localServices = $services;
137 6
        return $this;
138
    }
139
140
    /**
141
     * @param NetworkAddressInterface $networkAddress
142
     * @return $this
143
     */
144 3
    public function setLocalNetAddr(NetworkAddressInterface $networkAddress)
145
    {
146 3
        return $this
147 3
            ->setLocalIp($networkAddress->getIp())
148 3
            ->setLocalPort($networkAddress->getPort())
149 3
            ->setLocalServices($networkAddress->getServices());
150
    }
151
152
    /**
153
     * @param int $timestamp
154
     * @return $this
155
     */
156 3
    public function setTimestamp($timestamp)
157
    {
158 3
        $this->timestamp = $timestamp;
159 3
        return $this;
160
    }
161
162
    /**
163
     * @param $string
164
     * @return $this
165
     */
166 6
    public function setUserAgent($string)
167
    {
168 6
        if (!is_string($string)) {
169 3
            throw new \InvalidArgumentException('User agent must be a string');
170
        }
171
172 3
        $this->userAgent = new Buffer($string);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \BitWasp\Buffertools\Buffer($string) of type object<BitWasp\Buffertools\Buffer> is incompatible with the declared type string of property $userAgent.

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..

Loading history...
173 3
        return $this;
174
    }
175
176
    /**
177
     * @param MsgFactory $messageFactory
178
     * @param NetworkAddressInterface $remoteAddress
179
     * @return Version
180
     */
181 45
    public function produceVersion(MsgFactory $messageFactory, NetworkAddressInterface $remoteAddress)
182
    {
183 45
        $protocolVersion = is_null($this->protocolVersion) ? $this->defaultProtocolVersion : $this->protocolVersion;
184 45
        $localServices = is_null($this->localServices) ? Services::NONE : $this->localServices;
185 45
        $timestamp = is_null($this->timestamp) ? time() : $this->timestamp;
186 45
        $localAddr = new NetworkAddress(
187 45
            $localServices,
188 45
            is_null($this->localIp) ? $this->defaultLocalIp : $this->localIp,
189 45
            is_null($this->localPort) ? $this->defaultLocalPort : $this->localPort
190 45
        );
191
192 45
        $userAgent = is_null($this->userAgent) ? new Buffer($this->defaultUserAgent) : $this->userAgent;
193
194 45
        if (is_callable($this->bestBlockHeightCallback)) {
195 3
            $cb = $this->bestBlockHeightCallback;
196 3
            $bestHeight = $cb();
197 45
        } elseif (!is_null($this->bestBlockHeight)) {
198 3
            $bestHeight = $this->bestBlockHeight;
199 3
        } else {
200 39
            $bestHeight = $this->defaultBlockHeight;
201
        }
202
203 45
        $relay = is_null($this->txRelay) ? $this->defaultTxRelay : $this->txRelay;
204
205 45
        return $messageFactory->version($protocolVersion, $localServices, $timestamp, $remoteAddress, $localAddr, $userAgent, $bestHeight, $relay);
0 ignored issues
show
Bug introduced by
It seems like $userAgent defined by is_null($this->userAgent...ent) : $this->userAgent on line 192 can also be of type string; however, BitWasp\Bitcoin\Networki...ages\Factory::version() does only seem to accept object<BitWasp\Buffertools\BufferInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
206
    }
207
}
208