Completed
Push — master ( dd1269...21c3dd )
by Sam
02:11
created

VariableAttributes::write()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 13
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 19
ccs 14
cts 14
cp 1
crap 5
rs 9.5222
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 2
    public function read(Buffer $buffer)
21
    {
22 2
        parent::read($buffer);
23 2
        $data = $buffer->readString($this->dataSize * $this->dataCount);
24 2
        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 2
            list($var, $value) = explode(':', $item);
26 2
            if (preg_match_all('#(.+)\((.+)\)#Uis', $value, $matches)) {
27 2
                $this->data[$var] = [];
28 2
                foreach ($matches[1] as $key => $val) {
29 2
                    $this->data[$var][$val] = trim(trim($matches[2][$key]), '\'');
30
                }
31
            } else {
32
                $this->data[$var] = $value;
33
            }
34
        }
35 2
    }
36
37
    /**
38
     * @param Buffer $buffer
39
     */
40 5
    public function write(Buffer $buffer)
41
    {
42 5
        $lines = [];
43 5
        foreach ($this->data as $var => $value) {
44 2
            if (is_array($value)) {
45 2
                $_tmpString = '';
46 2
                foreach ($value as $key => $val) {
47 2
                    $_tmpString .= sprintf("%s('%s'\n)", $key, $val);
48
                }
49 2
                $value = $_tmpString;
50
            }
51 2
            $lines[] = sprintf('%s:%s', $var, $value);
52
        }
53
54 5
        if ($lines) {
55 2
            $data = implode('/', $lines);
56 2
            $this->dataCount = mb_strlen($data);
57 2
            parent::write($buffer);
58 2
            $buffer->writeString($data);
59
        }
60 5
    }
61
}
62