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

VariableDisplayParam::write()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 9
ccs 8
cts 8
cp 1
crap 3
rs 10
1
<?php
2
3
namespace SPSS\Sav\Record\Info;
4
5
use SPSS\Buffer;
6
use SPSS\Exception;
7
use SPSS\Sav\Record\Info;
8
9
class VariableDisplayParam extends Info
10
{
11
    const SUBTYPE = 11;
12
13
    /**
14
     * @var array
15
     */
16
    public $data = [];
17
18
    /**
19
     * @var int
20
     */
21
    protected $dataSize = 4;
22
23
    /**
24
     * @param Buffer $buffer
25
     * @throws Exception
26
     */
27 5
    public function read(Buffer $buffer)
28
    {
29 5
        parent::read($buffer);
30 5
        if ($this->dataSize != 4) {
31
            throw new Exception(
32
                sprintf('Error reading record type 7 subtype 11: bad data element length [%s]. Expecting 4.',
33
                    $this->dataSize
34
                )
35
            );
36
        }
37 5
        if (($this->dataCount % 3) != 0) {
38
            throw new Exception(
39
                sprintf('Error reading record type 7 subtype 11: number of data elements [%s] is not a multiple of 3.',
40
                    $this->dataCount
41
                )
42
            );
43
        }
44 5
        $itemCount = $this->dataCount / 3;
45 5
        for ($i = 0; $i < $itemCount; $i++) {
46 5
            $this->data[] = [
47 5
                $buffer->readInt(), // The measurement type of the variable
48 5
                $buffer->readInt(), // The width of the display column for the variable in characters.
49 5
                $buffer->readInt(), // The alignment of the variable
50
            ];
51
        }
52 5
    }
53
54
    /**
55
     * @param Buffer $buffer
56
     */
57 5
    public function write(Buffer $buffer)
58
    {
59 5
        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...
60 5
            $this->dataCount = count($this->data) * 3;
61 5
            parent::write($buffer);
62 5
            foreach ($this->data as $item) {
63 5
                $buffer->writeInt(0xFF & $item[0]);
64 5
                $buffer->writeInt(0xFF & $item[1]);
65 5
                $buffer->writeInt(0xFF & $item[2]);
66
            }
67
        }
68 5
    }
69
}
70