Completed
Push — master ( 9033ab...337f95 )
by thomas
38:43
created

ConnectionParams::getRequiredServices()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace BitWasp\Bitcoin\Networking\Peer;
4
5
use BitWasp\Bitcoin\Networking\Ip\IpInterface;
6
use BitWasp\Bitcoin\Networking\Ip\Ipv4;
7
use BitWasp\Bitcoin\Networking\Messages\Factory as MsgFactory;
8
use BitWasp\Bitcoin\Networking\Messages\Version;
9
use BitWasp\Bitcoin\Networking\Services;
10
use BitWasp\Bitcoin\Networking\Structure\NetworkAddress;
11
use BitWasp\Bitcoin\Networking\Structure\NetworkAddressInterface;
12
use BitWasp\Buffertools\Buffer;
13
14
class ConnectionParams
15
{
16
    protected $defaultUserAgent = 'bitcoin-php';
17
    protected $defaultProtocolVersion = '70000';
18
    protected $defaultTxRelay = false;
19
    protected $defaultBlockHeight = '0';
20
    protected $defaultLocalIp = '0.0.0.0';
21
    protected $defaultLocalPort = '0';
22
23
    /**
24
     * @var int
25
     */
26
    private $protocolVersion;
27
28
    /**
29
     * @var int
30
     */
31
    private $timestamp;
32
33
    /**
34
     * @var bool
35
     */
36
    private $txRelay;
37
38
    /**
39
     * @var callable
40
     */
41
    private $bestBlockHeightCallback;
42
43
    /**
44
     * @var int
45
     */
46
    private $bestBlockHeight;
47
48
    /**
49
     * @var string
50
     */
51
    private $localIp;
52
53
    /**
54
     * @var int
55
     */
56
    private $localPort;
57
58
    /**
59
     * @var int
60
     */
61
    private $localServices;
62
63
    /**
64
     * @var string
65
     */
66
    private $userAgent;
67
68
    /**
69
     * @var int
70
     */
71
    private $requiredServices = 0;
72 6
73
    /**
74 6
     * @param bool $optRelay
75 3
     * @return $this
76
     */
77
    public function requestTxRelay($optRelay = true)
78 3
    {
79 3
        if (!is_bool($optRelay)) {
80
            throw new \InvalidArgumentException('Invalid txrelay setting, must be a boolean');
81
        }
82
83
        $this->txRelay = $optRelay;
84
        return $this;
85
    }
86 6
87
    /**
88 6
     * @param int $blockHeight
89 6
     * @return $this
90
     */
91
    public function setBestBlockHeight($blockHeight)
92
    {
93
        $this->bestBlockHeight = $blockHeight;
94
        return $this;
95
    }
96 3
97
    /**
98 3
     * @param callable $callable
99 3
     * @return $this
100
     */
101
    public function setBestBlockHeightCallback(callable $callable)
102
    {
103
        $this->bestBlockHeightCallback = $callable;
104
        return $this;
105
    }
106 3
107
    /**
108 3
     * @param int $version
109 3
     * @return $this
110
     */
111
    public function setProtocolVersion($version)
112
    {
113
        $this->protocolVersion = $version;
114
        return $this;
115
    }
116 9
117
    /**
118 9
     * @param IpInterface $ip
119 9
     * @return $this
120
     */
121
    public function setLocalIp(IpInterface $ip)
122
    {
123
        $this->localIp = $ip;
0 ignored issues
show
Documentation Bug introduced by
It seems like $ip of type object<BitWasp\Bitcoin\Networking\Ip\IpInterface> is incompatible with the declared type string of property $localIp.

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...
124
        return $this;
125
    }
126 6
127
    /**
128 6
     * @param int $port
129 6
     * @return $this
130
     */
131
    public function setLocalPort($port)
132
    {
133
        $this->localPort = $port;
134
        return $this;
135
    }
136 6
137
    /**
138 6
     * @param int $services
139 6
     * @return $this
140
     */
141
    public function setLocalServices($services)
142
    {
143
        $this->localServices = $services;
144
        return $this;
145
    }
146 3
147
    /**
148 2
     * @param NetworkAddressInterface $networkAddress
149 3
     * @return $this
150 3
     */
151 3
    public function setLocalNetAddr(NetworkAddressInterface $networkAddress)
152
    {
153
        return $this
154
            ->setLocalIp($networkAddress->getIp())
155
            ->setLocalPort($networkAddress->getPort())
156
            ->setLocalServices($networkAddress->getServices());
157
    }
158 3
159
    /**
160 3
     * @param int $timestamp
161 3
     * @return $this
162
     */
163
    public function setTimestamp($timestamp)
164
    {
165
        $this->timestamp = $timestamp;
166
        return $this;
167
    }
168 6
169
    /**
170 6
     * @param int $services
171 3
     * @return $this
172
     */
173
    public function setRequiredServices($services)
174 3
    {
175 3
        $this->requiredServices = $services;
176
        return $this;
177
    }
178
179
    /**
180
     * @return int
181
     */
182
    public function getRequiredServices()
183 45
    {
184
        return $this->requiredServices;
185 45
    }
186 45
187 45
    /**
188 45
     * @param string $string
189 30
     * @return $this
190 45
     */
191 45
    public function setUserAgent($string)
192 30
    {
193
        if (!is_string($string)) {
194 45
            throw new \InvalidArgumentException('User agent must be a string');
195
        }
196 45
197 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...
198 3
        return $this;
199 44
    }
200 6
201 4
    /**
202 36
     * @param MsgFactory $messageFactory
203
     * @param NetworkAddressInterface $remoteAddress
204
     * @return Version
205 45
     */
206
    public function produceVersion(MsgFactory $messageFactory, NetworkAddressInterface $remoteAddress)
207 45
    {
208
        $protocolVersion = is_null($this->protocolVersion) ? $this->defaultProtocolVersion : $this->protocolVersion;
209
        $localServices = is_null($this->localServices) ? Services::NONE : $this->localServices;
210
        $timestamp = is_null($this->timestamp) ? time() : $this->timestamp;
211
        $localAddr = new NetworkAddress(
212
            $localServices,
213
            is_null($this->localIp) ? new Ipv4($this->defaultLocalIp) : $this->localIp,
0 ignored issues
show
Bug introduced by
It seems like is_null($this->localIp) ...calIp) : $this->localIp can also be of type string; however, BitWasp\Bitcoin\Networki...kAddress::__construct() does only seem to accept object<BitWasp\Bitcoin\Networking\Ip\IpInterface>, 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...
214
            is_null($this->localPort) ? $this->defaultLocalPort : $this->localPort
215
        );
216
217
        $userAgent = is_null($this->userAgent) ? new Buffer($this->defaultUserAgent) : $this->userAgent;
218
219
        if (is_callable($this->bestBlockHeightCallback)) {
220
            $cb = $this->bestBlockHeightCallback;
221
            $bestHeight = $cb();
222
        } elseif (!is_null($this->bestBlockHeight)) {
223
            $bestHeight = $this->bestBlockHeight;
224
        } else {
225
            $bestHeight = $this->defaultBlockHeight;
226
        }
227
228
        $relay = is_null($this->txRelay) ? $this->defaultTxRelay : $this->txRelay;
229
230
        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 217 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...
231
    }
232
}
233