LongStringMissingValues   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 25
c 2
b 0
f 0
dl 0
loc 44
ccs 0
cts 26
cp 0
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A read() 0 12 3
A write() 0 17 4
1
<?php
2
3
namespace SPSS\Sav\Record\Info;
4
5
use SPSS\Buffer;
6
use SPSS\Sav\Record\Info;
7
8
class LongStringMissingValues extends Info
9
{
10
    const SUBTYPE = 22;
11
12
    /**
13
     * @param \SPSS\Buffer $buffer
14
     * @throws \SPSS\Exception
15
     */
16
    public function read(Buffer $buffer)
17
    {
18
        parent::read($buffer);
19
        $buffer = $buffer->allocate($this->dataCount * $this->dataSize);
20
        while ($varNameLength = $buffer->readInt()) {
21
            $varName = trim($buffer->readString($varNameLength));
0 ignored issues
show
Bug introduced by
It seems like $buffer->readString($varNameLength) can also be of type false; however, parameter $str of trim() 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

21
            $varName = trim(/** @scrutinizer ignore-type */ $buffer->readString($varNameLength));
Loading history...
22
            $count = ord($buffer->read(1));
23
            $this->data[$varName] = [];
24
            $valueLength = $buffer->readInt();
25
            for ($i = 0; $i < $count; $i++) {
26
                $value = $buffer->readString($valueLength);
27
                $this->data[$varName][] = rtrim($value);
0 ignored issues
show
Bug introduced by
It seems like $value 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

27
                $this->data[$varName][] = rtrim(/** @scrutinizer ignore-type */ $value);
Loading history...
28
            }
29
        }
30
    }
31
32
    /**
33
     * @param Buffer $buffer
34
     */
35
    public function write(Buffer $buffer)
36
    {
37
        if ($this->data) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->data of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
38
            $localBuffer = Buffer::factory();
39
            foreach ($this->data as $varName => $values) {
40
                $localBuffer->writeInt(mb_strlen($varName));
41
                $localBuffer->writeString($varName);
42
                $localBuffer->write(chr(count($values)), 1);
43
                $localBuffer->writeInt(8);
44
                foreach ($values as $value) {
45
                    $localBuffer->writeString($value, 8);
46
                }
47
            }
48
            $this->dataCount = $localBuffer->position();
49
            parent::write($buffer);
50
            $localBuffer->rewind();
51
            $buffer->writeStream($localBuffer->getStream(), $this->dataCount);
52
        }
53
    }
54
}
55