VariableAttributes::read()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 10
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 13
ccs 0
cts 10
cp 0
crap 20
rs 9.9332
1
<?php
2
3
namespace SPSS\Sav\Record\Info;
4
5
use SPSS\Buffer;
6
use SPSS\Sav\Record\Info;
7
8
class VariableAttributes extends Info
9
{
10
    const SUBTYPE = 18;
11
12
    /**
13
     * @var array
14
     */
15
    public $data = [];
16
17
    /**
18
     * @param Buffer $buffer
19
     */
20
    public function read(Buffer $buffer)
21
    {
22
        parent::read($buffer);
23
        $data = $buffer->readString($this->dataSize * $this->dataCount);
24
        foreach (explode('/', $data) as $item) {
0 ignored issues
show
Bug introduced by
It seems like $data can also be of type false; however, parameter $string of explode() 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

24
        foreach (explode('/', /** @scrutinizer ignore-type */ $data) as $item) {
Loading history...
25
            list($var, $value) = explode(':', $item);
26
            if (preg_match_all('#(.+)\((.+)\)#Uis', $value, $matches)) {
27
                $this->data[$var] = [];
28
                foreach ($matches[1] as $key => $val) {
29
                    $this->data[$var][$val] = trim(trim($matches[2][$key]), '\'');
30
                }
31
            } else {
32
                $this->data[$var] = $value;
33
            }
34
        }
35
    }
36
37
    /**
38
     * @param Buffer $buffer
39
     */
40
    public function write(Buffer $buffer)
41
    {
42
        $lines = [];
43
        foreach ($this->data as $var => $value) {
44
            if (is_array($value)) {
45
                $_tmpString = '';
46
                foreach ($value as $key => $val) {
47
                    $_tmpString .= sprintf("%s('%s'\n)", $key, $val);
48
                }
49
                $value = $_tmpString;
50
            }
51
            $lines[] = sprintf('%s:%s', $var, $value);
52
        }
53
54
        if ($lines) {
55
            $data = implode('/', $lines);
56
            $this->dataCount = mb_strlen($data);
57
            parent::write($buffer);
58
            $buffer->writeString($data);
59
        }
60
    }
61
}
62