Vector   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
c 0
b 0
f 0
lcom 1
cbo 4
dl 0
loc 60
ccs 19
cts 19
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A write() 0 12 2
A read() 0 12 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BitWasp\Buffertools\Types;
6
7
use BitWasp\Buffertools\Parser;
8
9
class Vector extends AbstractType
10
{
11
    /**
12
     * @var VarInt
13
     */
14
    private $varint;
15
16
    /**
17
     * @var callable
18
     */
19
    private $readFxn;
20
21
    /**
22
     * @param VarInt   $varInt
23
     * @param callable $readFunction
24
     */
25 8
    public function __construct(VarInt $varInt, callable $readFunction)
26
    {
27 8
        $this->varint = $varInt;
28 8
        $this->readFxn = $readFunction;
29 8
        parent::__construct($varInt->getByteOrder());
30 8
    }
31
32
    /**
33
     * {@inheritdoc}
34
     * @see \BitWasp\Buffertools\Types\TypeInterface::write()
35
     */
36 4
    public function write($items): string
37
    {
38 4
        if (false === is_array($items)) {
39 2
            throw new \InvalidArgumentException('Vector::write() must be supplied with an array');
40
        }
41
42 2
        $parser = new Parser();
43
        return $parser
44 2
            ->writeArray($items)
45 2
            ->getBuffer()
46 2
            ->getBinary();
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     * @see \BitWasp\Buffertools\Types\TypeInterface::read()
52
     * @param Parser $parser
53
     * @return array
54
     * @throws \Exception
55
     */
56 2
    public function read(Parser $parser): array
57
    {
58 2
        $results = array();
59 2
        $handler = $this->readFxn;
60
61 2
        $varInt = $this->varint->read($parser);
62 2
        for ($i = 0; $i < $varInt; $i++) {
63 2
            $results[] = $handler($parser);
64
        }
65
66 2
        return $results;
67
    }
68
}
69