Reader   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 38
c 2
b 0
f 0
dl 0
loc 128
ccs 0
cts 36
cp 0
rs 10
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
A fromFile() 0 3 1
A readData() 0 5 1
A fromString() 0 3 1
A read() 0 3 1
A __construct() 0 4 1
B readBody() 0 31 7
A readHeader() 0 5 1
1
<?php
2
3
namespace SPSS\Sav;
4
5
use SPSS\Buffer;
6
7
class Reader
8
{
9
    /**
10
     * @var \SPSS\Sav\Record\Header
11
     */
12
    public $header;
13
14
    /**
15
     * @var \SPSS\Sav\Record\Variable[]
16
     */
17
    public $variables = [];
18
19
    /**
20
     * @var \SPSS\Sav\Record\ValueLabel[]
21
     */
22
    public $valueLabels = [];
23
24
    /**
25
     * @var array
26
     */
27
    public $documents = [];
28
29
    /**
30
     * @var \SPSS\Sav\Record\Info[]
31
     */
32
    public $info = [];
33
34
    /**
35
     * @var array
36
     */
37
    public $data = [];
38
39
    /**
40
     * @var \SPSS\Buffer
41
     */
42
    public $_buffer;
43
44
    /**
45
     * Reader constructor.
46
     *
47
     * @param \SPSS\Buffer $buffer
48
     */
49
    private function __construct(Buffer $buffer)
50
    {
51
        $this->_buffer = $buffer;
52
        $this->_buffer->context = $this;
53
    }
54
55
    /**
56
     * @param string $file
57
     * @return \SPSS\Sav\Reader
58
     */
59
    public static function fromFile($file)
60
    {
61
        return new self(Buffer::factory(fopen($file, 'r')));
0 ignored issues
show
Bug introduced by
It seems like fopen($file, 'r') can also be of type false; however, parameter $resource of SPSS\Buffer::factory() does only seem to accept resource|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

61
        return new self(Buffer::factory(/** @scrutinizer ignore-type */ fopen($file, 'r')));
Loading history...
62
    }
63
64
    /**
65
     * @param string $str
66
     * @return \SPSS\Sav\Reader
67
     */
68
    public static function fromString($str)
69
    {
70
        return new self(Buffer::factory($str));
71
    }
72
73
    /**
74
     * @return $this
75
     */
76
    public function read()
77
    {
78
        return $this->readHeader()->readBody()->readData();
79
    }
80
81
    /**
82
     * @return $this
83
     */
84
    public function readHeader()
85
    {
86
        $this->header = Record\Header::fill($this->_buffer);
87
88
        return $this;
89
    }
90
91
    /**
92
     * @return $this
93
     */
94
    public function readBody()
95
    {
96
        if (! $this->header) {
97
            $this->readHeader();
98
        }
99
100
        // TODO: refactory
101
        $infoCollection = new Record\InfoCollection();
102
103
        do {
104
            $recType = $this->_buffer->readInt();
105
            switch ($recType) {
106
                case Record\Variable::TYPE:
107
                    $this->variables[] = Record\Variable::fill($this->_buffer);
108
                    break;
109
                case Record\ValueLabel::TYPE:
110
                    $this->valueLabels[] = Record\ValueLabel::fill($this->_buffer, [
111
                        // TODO: refactory
112
                        'variables' => $this->variables,
113
                    ]);
114
                    break;
115
                case Record\Info::TYPE:
116
                    $this->info = $infoCollection->fill($this->_buffer);
117
                    break;
118
                case Record\Document::TYPE:
119
                    $this->documents = Record\Document::fill($this->_buffer)->toArray();
120
                    break;
121
            }
122
        } while ($recType != Record\Data::TYPE);
123
124
        return $this;
125
    }
126
127
    /**
128
     * @return $this
129
     */
130
    public function readData()
131
    {
132
        $this->data = Record\Data::fill($this->_buffer)->toArray();
133
134
        return $this;
135
    }
136
}
137