Factory::version()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 20
ccs 10
cts 10
cp 1
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 8
crap 1

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
declare(strict_types=1);
4
5
namespace BitWasp\Bitcoin\Networking\Messages;
6
7
use BitWasp\Bitcoin\Block\FilteredBlock;
8
use BitWasp\Bitcoin\Bloom\BloomFilter;
9
use BitWasp\Bitcoin\Chain\BlockLocator;
10
use BitWasp\Bitcoin\Crypto\EcAdapter\Signature\SignatureInterface;
11
use BitWasp\Bitcoin\Crypto\Random\Random;
12
use BitWasp\Bitcoin\Network\NetworkInterface;
13
use BitWasp\Bitcoin\Networking\NetworkMessage;
14
use BitWasp\Bitcoin\Networking\Serializer\NetworkMessageSerializer;
15
use BitWasp\Bitcoin\Networking\Structure\AlertDetail;
16
use BitWasp\Bitcoin\Networking\Structure\Inventory;
17
use BitWasp\Bitcoin\Networking\Structure\NetworkAddress;
18
use BitWasp\Bitcoin\Networking\Structure\NetworkAddressTimestamp;
19
use BitWasp\Buffertools\Buffer;
20
use BitWasp\Buffertools\BufferInterface;
21
use BitWasp\Buffertools\Parser;
22
23
class Factory
24
{
25
    /**
26
     * @var NetworkInterface
27
     */
28
    private $network;
29
30
    /**
31
     * @var Random
32
     */
33
    private $random;
34
35
    /**
36
     * @var NetworkMessageSerializer
37
     */
38
    private $serializer;
39 126
40
    /**
41 126
     * @param NetworkInterface $network
42 126
     * @param Random $random
43 126
     */
44
    public function __construct(NetworkInterface $network, Random $random)
45
    {
46
        $this->network = $network;
47
        $this->random = $random;
48
        $this->serializer = new NetworkMessageSerializer($this->network);
49
    }
50
51
    /**
52
     * @param int $version
53
     * @param int $services
54
     * @param int $timestamp
55
     * @param NetworkAddress $addrRecv
56 51
     * @param NetworkAddress $addrFrom
57
     * @param BufferInterface $userAgent
58
     * @param int $startHeight
59
     * @param bool $relay
60
     * @return Version
61
     */
62
    public function version(
63
        int $version,
64
        $services,
65
        int $timestamp,
66 51
        NetworkAddress $addrRecv,
67 34
        NetworkAddress $addrFrom,
68 34
        BufferInterface $userAgent,
69 34
        $startHeight,
70 34
        $relay
71 34
    ): Version {
72 51
        return new Version(
73 34
            $version,
74 34
            $services,
75
            $timestamp,
76 34
            $addrRecv,
77
            $addrFrom,
78
            (int) $this->random->bytes(8)->getInt(),
79
            $userAgent,
80
            $startHeight,
81
            $relay
82 15
        );
83
    }
84 15
85
    /**
86
     * @return VerAck
87
     */
88
    public function verack(): VerAck
89
    {
90 3
        return new VerAck();
91
    }
92 3
93
    /**
94
     * @return SendHeaders
95
     */
96
    public function sendheaders(): SendHeaders
97
    {
98
        return new SendHeaders();
99 3
    }
100
101 3
    /**
102
     * @param NetworkAddressTimestamp[] $addrs
103
     * @return Addr
104
     */
105
    public function addr(array $addrs): Addr
106
    {
107
        return new Addr($addrs);
108 3
    }
109
110 3
    /**
111
     * @param Inventory[] $vectors
112
     * @return Inv
113
     */
114
    public function inv(array $vectors): Inv
115
    {
116
        return new Inv($vectors);
117 3
    }
118
119 3
    /**
120
     * @param Inventory[] $vectors
121
     * @return GetData
122
     */
123
    public function getdata(array $vectors): GetData
124
    {
125
        return new GetData($vectors);
126 3
    }
127
128 3
    /**
129
     * @param Inventory[] $vectors
130
     * @return NotFound
131
     */
132
    public function notfound(array $vectors): NotFound
133
    {
134
        return new NotFound($vectors);
135
    }
136 3
137
    /**
138 3
     * @param int $version
139
     * @param BlockLocator $blockLocator
140
     * @return GetBlocks
141
     */
142
    public function getblocks(int $version, BlockLocator $blockLocator): GetBlocks
143
    {
144
        return new GetBlocks($version, $blockLocator);
145
    }
146 3
147
    /**
148 3
     * @param int $version
149
     * @param BlockLocator $blockLocator
150
     * @return GetHeaders
151
     */
152
    public function getheaders(int $version, BlockLocator $blockLocator): GetHeaders
153
    {
154
        return new GetHeaders($version, $blockLocator);
155 3
    }
156
157 3
    /**
158
     * @param BufferInterface $txData
159
     * @return Tx
160
     */
161
    public function tx(BufferInterface $txData): Tx
162
    {
163
        return new Tx($txData);
164 3
    }
165
166 3
    /**
167
     * @param BufferInterface $blockData
168
     * @return Block
169
     */
170
    public function block(BufferInterface $blockData): Block
171
    {
172
        return new Block($blockData);
173 3
    }
174
175 3
    /**
176
     * @param BufferInterface ...$headers
177
     * @return Headers
178
     */
179
    public function headers(BufferInterface ...$headers): Headers
180
    {
181 3
        return new Headers(...$headers);
182
    }
183 3
184
    /**
185
     * @return GetAddr
186
     */
187
    public function getaddr(): GetAddr
188
    {
189 3
        return new GetAddr();
190
    }
191 3
192
    /**
193
     * @return MemPool
194
     */
195
    public function mempool(): MemPool
196
    {
197
        return new MemPool();
198 3
    }
199
200 3
    /**
201
     * @param int $feeRate
202
     * @return FeeFilter
203
     */
204
    public function feefilter(int $feeRate): FeeFilter
205
    {
206
        return new FeeFilter($feeRate);
207 3
    }
208
209 3
    /**
210
     * @param BufferInterface $data
211
     * @return FilterAdd
212
     */
213
    public function filteradd(BufferInterface $data): FilterAdd
214
    {
215
        return new FilterAdd($data);
216 3
    }
217
218 3
    /**
219
     * @param BloomFilter $filter
220
     * @return FilterLoad
221
     */
222
    public function filterload(BloomFilter $filter): FilterLoad
223
    {
224 3
        return new FilterLoad($filter);
225
    }
226 3
227
    /**
228
     * @return FilterClear
229
     */
230
    public function filterclear(): FilterClear
231
    {
232
        return new FilterClear();
233 3
    }
234
235 3
    /**
236
     * @param FilteredBlock $filtered
237
     * @return MerkleBlock
238
     */
239
    public function merkleblock(FilteredBlock $filtered): MerkleBlock
240
    {
241 6
        return new MerkleBlock($filtered);
242
    }
243 6
    /**
244
     * @return Ping
245
     * @throws \BitWasp\Bitcoin\Exceptions\RandomBytesFailure
246
     */
247
    public function ping(): Ping
248
    {
249
        return Ping::generate($this->random);
250 3
    }
251
252 3
    /**
253
     * @param Ping $ping
254
     * @return Pong
255
     */
256
    public function pong(Ping $ping): Pong
257
    {
258
        return new Pong($ping->getNonce());
259
    }
260
261
    /**
262 9
     * @param BufferInterface $message
263
     * @param int $code
264
     * @param BufferInterface $reason
265
     * @param BufferInterface|null $data
266
     * @return Reject
267
     */
268 9
    public function reject(
269 3
        BufferInterface $message,
270 2
        $code,
271
        BufferInterface $reason,
272 9
        BufferInterface $data = null
273 6
    ): Reject {
274 6
        if (null === $data) {
275 6
            $data = new Buffer();
276
        }
277 6
278
        return new Reject(
279
            $message,
280
            $code,
281
            $reason,
282
            $data
283
        );
284
    }
285 3
286
    /**
287 3
     * @param AlertDetail $detail
288 2
     * @param SignatureInterface $sig
289
     * @return Alert
290 2
     */
291
    public function alert(AlertDetail $detail, SignatureInterface $sig): Alert
292
    {
293
        return new Alert(
294
            $detail,
295
            $sig
296
        );
297 36
    }
298
299 36
    /**
300
     * @param Parser $parser
301
     * @return NetworkMessage
302
     */
303
    public function parse(Parser $parser): NetworkMessage
304
    {
305 12
        return $this->serializer->fromParser($parser);
306
    }
307 12
308
    /**
309
     * @return NetworkMessageSerializer
310
     */
311
    public function getSerializer(): NetworkMessageSerializer
312
    {
313
        return $this->serializer;
314
    }
315
316
    /**
317
     * @return NetworkInterface
318
     */
319
    public function getNetwork(): NetworkInterface
320
    {
321
        return $this->network;
322
    }
323
}
324