LongStringValueLabels   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 39
c 2
b 0
f 0
dl 0
loc 67
ccs 0
cts 38
cp 0
rs 10
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A read() 0 18 3
B write() 0 29 6
1
<?php
2
3
namespace SPSS\Sav\Record\Info;
4
5
use SPSS\Buffer;
6
use SPSS\Sav\Record\Info;
7
8
class LongStringValueLabels extends Info
9
{
10
    const SUBTYPE = 21;
11
12
    /**
13
     * @var array
14
     */
15
    public $data = [];
16
17
    /**
18
     * @param \SPSS\Buffer $buffer
19
     * @throws \SPSS\Exception
20
     */
21
    public function read(Buffer $buffer)
22
    {
23
        parent::read($buffer);
24
        $buffer = $buffer->allocate($this->dataCount * $this->dataSize);
25
        while ($varNameLength = $buffer->readInt()) {
26
            $varName = $buffer->readString($varNameLength);
27
            $varWidth = $buffer->readInt(); // The width of the variable, in bytes, which will be between 9 and 32767
28
            $valuesCount = $buffer->readInt();
29
            $this->data[$varName] = [
30
                'width' => $varWidth,
31
                'values' => [],
32
            ];
33
            for ($i = 0; $i < $valuesCount; $i++) {
34
                $valueLength = $buffer->readInt();
35
                $value = rtrim($buffer->readString($valueLength));
0 ignored issues
show
Bug introduced by
It seems like $buffer->readString($valueLength) can also be of type false; however, parameter $str of rtrim() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

35
                $value = rtrim(/** @scrutinizer ignore-type */ $buffer->readString($valueLength));
Loading history...
36
                $labelLength = $buffer->readInt();
37
                $label = rtrim($buffer->readString($labelLength));
38
                $this->data[$varName]['values'][$value] = $label;
39
            }
40
        }
41
    }
42
43
    /**
44
     * @param \SPSS\Buffer $buffer
45
     */
46
    public function write(Buffer $buffer)
47
    {
48
        $localBuffer = Buffer::factory('', ['memory' => true]);
49
        foreach ($this->data as $varName => $data) {
50
            if (! isset($data['width'])) {
51
                throw new \InvalidArgumentException('width required');
52
            }
53
            if (! isset($data['values'])) {
54
                throw new \InvalidArgumentException('values required');
55
            }
56
            $width = (int) $data['width'];
57
            $localBuffer->writeInt(mb_strlen($varName));
58
            $localBuffer->writeString($varName);
59
            $localBuffer->writeInt($width);
60
            $localBuffer->writeInt(count($data['values']));
61
            foreach ($data['values'] as $value => $label) {
62
                $localBuffer->writeInt($width);
63
                $localBuffer->writeString($value, $width);
64
                $localBuffer->writeInt(mb_strlen($label));
65
                $localBuffer->writeString($label);
66
            }
67
        }
68
69
        // retrieve bytes count
70
        $this->dataCount = $localBuffer->position();
71
        if ($this->dataCount > 0) {
72
            parent::write($buffer);
73
            $localBuffer->rewind();
74
            $buffer->writeStream($localBuffer->getStream());
75
        }
76
    }
77
}
78