Completed
Pull Request — master (#50)
by thomas
20:19
created

Version::__construct()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 34
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 34
ccs 21
cts 21
cp 1
rs 8.5806
cc 4
eloc 27
nc 8
nop 9
crap 4

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace BitWasp\Bitcoin\Networking\Messages;
4
5
use BitWasp\Bitcoin\Networking\Message;
6
use BitWasp\Bitcoin\Networking\Structure\NetworkAddressInterface;
7
use BitWasp\Bitcoin\Networking\Structure\NetworkAddressTimestamp;
8
use BitWasp\Bitcoin\Networking\Serializer\Message\VersionSerializer;
9
use BitWasp\Bitcoin\Networking\Serializer\Structure\NetworkAddressSerializer;
10
use BitWasp\Buffertools\Buffer;
11
use BitWasp\Bitcoin\Crypto\Random\Random;
12
use BitWasp\Bitcoin\Networking\NetworkSerializable;
13
use BitWasp\Bitcoin\Networking\Structure\NetworkAddress;
14
use BitWasp\Buffertools\BufferInterface;
15
16
class Version extends NetworkSerializable
17
{
18
    /**
19
     * @var int|string
20
     */
21
    private $version;
22
23
    /**
24
     * @var int
25
     */
26
    private $services;
27
28
    /**
29
     * @var int|string
30
     */
31
    private $timestamp;
32
33
    /**
34
     * @var NetworkAddressInterface
35
     */
36
    private $addrRecv;
37
38
    /**
39
     * @var NetworkAddressInterface
40
     */
41
    private $addrFrom;
42
43
    /**
44
     * @var BufferInterface
45
     */
46
    private $userAgent;
47
48
    /**
49
     * @var int|string
50
     */
51
    private $startHeight;
52
53
    /**
54
     * @var bool
55
     */
56
    private $relay;
57
58
    /**
59
     * @var integer|string
60
     */
61
    private $nonce;
62
63
    /**
64
     * @param int $version
65
     * @param int $services
66
     * @param int $timestamp
67
     * @param NetworkAddressInterface $addrRecv
68
     * @param NetworkAddressInterface $addrFrom
69
     * @param int $nonce
70
     * @param BufferInterface $userAgent
71
     * @param int $startHeight
72
     * @param bool $relay
73
     */
74 30
    public function __construct(
75
        $version,
76
        $services,
77
        $timestamp,
78
        NetworkAddressInterface $addrRecv,
79
        NetworkAddressInterface $addrFrom,
80
        $nonce,
81
        BufferInterface $userAgent,
82
        $startHeight,
83
        $relay
84
    ) {
85
86 30
        if ($addrRecv instanceof NetworkAddressTimestamp) {
87 3
            $addrRecv = $addrRecv->withoutTimestamp();
88 3
        }
89 30
        if ($addrFrom instanceof NetworkAddressTimestamp) {
90 3
            $addrFrom = $addrFrom->withoutTimestamp();
91 3
        }
92
93 30
        $random = new Random();
94 30
        $this->nonce = $random->bytes(8)->getInt();
95 30
        $this->version = $version;
96 30
        $this->services = $services;
97 30
        $this->timestamp = $timestamp;
98 30
        $this->addrRecv = $addrRecv;
99 30
        $this->nonce = $nonce;
100 30
        $this->addrFrom = $addrFrom;
101 30
        $this->userAgent = $userAgent;
102 30
        $this->startHeight = $startHeight;
103 30
        if (! is_bool($relay)) {
104 3
            throw new \InvalidArgumentException('Relay must be a boolean');
105
        }
106 27
        $this->relay = $relay;
107 27
    }
108
109
    /**
110
     * {@inheritdoc}
111
     * @see \BitWasp\Bitcoin\Network\NetworkSerializableInterface::getNetworkCommand()
112
     */
113 24
    public function getNetworkCommand()
114
    {
115 24
        return Message::VERSION;
116
    }
117
118
    /**
119
     * @return Buffer|int|string
120
     */
121 24
    public function getNonce()
122
    {
123 24
        return $this->nonce;
124
    }
125
126
    /**
127
     * @return int|string
128
     */
129 24
    public function getVersion()
130
    {
131 24
        return $this->version;
132
    }
133
134
    /**
135
     * @return int
136
     */
137 24
    public function getServices()
138
    {
139 24
        return $this->services;
140
    }
141
142
    /**
143
     * @return int|string
144
     */
145 24
    public function getTimestamp()
146
    {
147 24
        return $this->timestamp;
148
    }
149
150
    /**
151
     * @return NetworkAddress
152
     */
153 27
    public function getRecipientAddress()
154
    {
155 27
        return $this->addrRecv;
156
    }
157
158
    /**
159
     * @return NetworkAddress
160
     */
161 27
    public function getSenderAddress()
162
    {
163 27
        return $this->addrFrom;
164
    }
165
166
    /**
167
     * @return BufferInterface
168
     */
169 24
    public function getUserAgent()
170
    {
171 24
        return $this->userAgent;
172
    }
173
174
    /**
175
     * @return int|string
176
     */
177 24
    public function getStartHeight()
178
    {
179 24
        return $this->startHeight;
180
    }
181
182
    /**
183
     * @return bool
184
     */
185 24
    public function getRelay()
186
    {
187 24
        return $this->relay;
188
    }
189
190
    /**
191
     * @return BufferInterface
192
     */
193 21
    public function getBuffer()
194
    {
195 21
        return (new VersionSerializer(new NetworkAddressSerializer()))->serialize($this);
196
    }
197
}
198