Completed
Push — master ( fc4613...2e6493 )
by thomas
30:51
created

ConnectionParams::setTimestamp()   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 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
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\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
73
    /**
74
     * @param bool $optRelay
75
     * @return $this
76
     */
77 6
    public function requestTxRelay($optRelay = true)
78
    {
79 6
        if (!is_bool($optRelay)) {
80 3
            throw new \InvalidArgumentException('Invalid txrelay setting, must be a boolean');
81
        }
82
83 3
        $this->txRelay = $optRelay;
84 3
        return $this;
85
    }
86
87
    /**
88
     * @param int $blockHeight
89
     * @return $this
90
     */
91 6
    public function setBestBlockHeight($blockHeight)
92
    {
93 6
        $this->bestBlockHeight = $blockHeight;
94 6
        return $this;
95
    }
96
97
    /**
98
     * @param callable $callable
99
     * @return $this
100
     */
101 3
    public function setBestBlockHeightCallback(callable $callable)
102
    {
103 3
        $this->bestBlockHeightCallback = $callable;
104 3
        return $this;
105
    }
106
107
    /**
108
     * @param int $version
109
     * @return $this
110
     */
111 3
    public function setProtocolVersion($version)
112
    {
113 3
        $this->protocolVersion = $version;
114 3
        return $this;
115
    }
116
117
    /**
118
     * @param IpInterface $ip
119
     * @return $this
120
     */
121 9
    public function setLocalIp(IpInterface $ip)
122
    {
123 9
        $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 9
        return $this;
125
    }
126
127
    /**
128
     * @param int $port
129
     * @return $this
130
     */
131 6
    public function setLocalPort($port)
132
    {
133 6
        $this->localPort = $port;
134 6
        return $this;
135
    }
136
137
    /**
138
     * @param int $services
139
     * @return $this
140
     */
141 6
    public function setLocalServices($services)
142
    {
143 6
        $this->localServices = $services;
144 6
        return $this;
145
    }
146
147
    /**
148
     * @param NetworkAddressInterface $networkAddress
149
     * @return $this
150
     */
151 3
    public function setLocalNetAddr(NetworkAddressInterface $networkAddress)
152
    {
153 2
        return $this
154 3
            ->setLocalIp($networkAddress->getIp())
155 3
            ->setLocalPort($networkAddress->getPort())
156 3
            ->setLocalServices($networkAddress->getServices());
157
    }
158
159
    /**
160
     * @param int $timestamp
161
     * @return $this
162
     */
163 3
    public function setTimestamp($timestamp)
164
    {
165 3
        $this->timestamp = $timestamp;
166 3
        return $this;
167
    }
168
169
    /**
170
     * @param int $services
171
     * @return $this
172
     */
173
    public function setRequiredServices($services)
174
    {
175
        $this->requiredServices = $services;
176
        return $this;
177
    }
178
179
    /**
180
     * @return int
181
     */
182 9
    public function getRequiredServices()
183
    {
184 9
        return $this->requiredServices;
185
    }
186
187
    /**
188
     * @param string $string
189
     * @return $this
190
     */
191 6
    public function setUserAgent($string)
192
    {
193 6
        if (!is_string($string)) {
194 3
            throw new \InvalidArgumentException('User agent must be a string');
195
        }
196
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
    }
200
201
    /**
202
     * @param MsgFactory $messageFactory
203
     * @param NetworkAddressInterface $remoteAddress
204
     * @return Version
205
     */
206 45
    public function produceVersion(MsgFactory $messageFactory, NetworkAddressInterface $remoteAddress)
207
    {
208 45
        $protocolVersion = is_null($this->protocolVersion) ? $this->defaultProtocolVersion : $this->protocolVersion;
209 45
        $localServices = is_null($this->localServices) ? Services::NONE : $this->localServices;
210 45
        $timestamp = is_null($this->timestamp) ? time() : $this->timestamp;
211 45
        $localAddr = new NetworkAddress(
212 30
            $localServices,
213 45
            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 45
            is_null($this->localPort) ? $this->defaultLocalPort : $this->localPort
215 30
        );
216
217 45
        $userAgent = is_null($this->userAgent) ? new Buffer($this->defaultUserAgent) : $this->userAgent;
218
219 45
        if (is_callable($this->bestBlockHeightCallback)) {
220 3
            $cb = $this->bestBlockHeightCallback;
221 3
            $bestHeight = $cb();
222 44
        } elseif (!is_null($this->bestBlockHeight)) {
223 6
            $bestHeight = $this->bestBlockHeight;
224 4
        } else {
225 36
            $bestHeight = $this->defaultBlockHeight;
226
        }
227
228 45
        $relay = is_null($this->txRelay) ? $this->defaultTxRelay : $this->txRelay;
229
230 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 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