Completed
Push — master ( dba287...8d738d )
by Daniel
13:35
created

IntegerReader::readSignedInteger16()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 13
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 13
loc 13
rs 9.4285
cc 3
eloc 7
nc 2
nop 0
1
<?php
2
/**
3
 * This file is part of the stream package
4
 *
5
 * @author Daniel Schröder <[email protected]>
6
 */
7
8
namespace GravityMedia\Stream\Reader;
9
10
use GravityMedia\Stream\ByteOrder\ByteOrder;
11
use GravityMedia\Stream\ByteOrder\ByteOrderAwareTrait;
12
13
/**
14
 * Integer reader
15
 *
16
 * @package GravityMedia\Stream\Reader
17
 */
18
class IntegerReader extends Reader
19
{
20
    /**
21
     * Use byte order aware trait
22
     */
23
    use ByteOrderAwareTrait;
24
25
    /**
26
     * Read unsigned 8-bit integer (char) data from the stream
27
     *
28
     * @return int
29
     */
30
    public function readUnsignedInteger8()
31
    {
32
        list(, $value) = unpack('C', $this->read(1));
33
        return $value;
34
    }
35
36
    /**
37
     * Read signed 8-bit integer (char) data from the stream
38
     *
39
     * @return int
40
     */
41
    public function readSignedInteger8()
42
    {
43
        list(, $value) = unpack('c', $this->read(1));
44
        return $value;
45
    }
46
47
    /**
48
     * Read unsigned 16-bit integer (short) data from the stream
49
     *
50
     * @return int
51
     */
52 View Code Duplication
    public function readUnsignedInteger16()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
53
    {
54
        switch ($this->getByteOrder()) {
55
            case ByteOrder::BIG_ENDIAN:
56
                $format = 'n';
57
                break;
58
            case ByteOrder::LITTLE_ENDIAN:
59
                $format = 'v';
60
                break;
61
            default:
62
                $format = 'S';
63
        }
64
65
        list(, $value) = unpack($format, $this->read(2));
66
        return $value;
67
    }
68
69
    /**
70
     * Read signed 16-bit integer (short) data from the stream
71
     *
72
     * @return int
73
     */
74 View Code Duplication
    public function readSignedInteger16()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
75
    {
76
        $data = $this->read(2);
77
78
        if ($this->getByteOrder() !== ByteOrder::MACHINE_ENDIAN
79
            && $this->getByteOrder() !== $this->getMachineByteOrder()
80
        ) {
81
            $data = strrev($data);
82
        }
83
84
        list(, $value) = unpack('s', $data);
85
        return $value;
86
    }
87
88
    /**
89
     * Read unsigned 24-bit integer (short) data from the stream
90
     *
91
     * @return int
92
     */
93
    public function readUnsignedInteger24()
94
    {
95
        $data = $this->read(3);
96
97
        switch ($this->getByteOrder()) {
98
            case ByteOrder::BIG_ENDIAN:
99
                $format = 'N';
100
                break;
101
            case ByteOrder::LITTLE_ENDIAN:
102
                $format = 'V';
103
                break;
104
            default:
105
                $format = 'L';
106
        }
107
108 View Code Duplication
        switch ($this->getMachineByteOrder()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
            case ByteOrder::BIG_ENDIAN:
110
                $data = $data . "\x00";
111
                break;
112
            case ByteOrder::LITTLE_ENDIAN:
113
                $data = "\x00" . $data;
114
                break;
115
        }
116
117
        list(, $value) = unpack($format, $data);
118
        return $value;
119
    }
120
121
    /**
122
     * Read signed 24-bit integer (short) data from the stream
123
     *
124
     * @return int
125
     */
126
    public function readSignedInteger24()
127
    {
128
        $data = $this->read(3);
129
130
        if ($this->getByteOrder() !== ByteOrder::MACHINE_ENDIAN
131
            && $this->getByteOrder() !== $this->getMachineByteOrder()
132
        ) {
133
            $data = strrev($data);
134
        }
135
136 View Code Duplication
        switch ($this->getMachineByteOrder()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
137
            case ByteOrder::BIG_ENDIAN:
138
                $data = $data . "\x00";
139
                break;
140
            case ByteOrder::LITTLE_ENDIAN:
141
                $data = "\x00" . $data;
142
                break;
143
        }
144
145
        list(, $value) = unpack('l', $data);
146
        return $value;
147
    }
148
149
    /**
150
     * Read unsigned 32-bit integer (long) data from the stream
151
     *
152
     * @return int
153
     */
154 View Code Duplication
    public function readUnsignedInteger32()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
155
    {
156
        switch ($this->getByteOrder()) {
157
            case ByteOrder::BIG_ENDIAN:
158
                $format = 'N';
159
                break;
160
            case ByteOrder::LITTLE_ENDIAN:
161
                $format = 'V';
162
                break;
163
            default:
164
                $format = 'L';
165
        }
166
167
        list(, $value) = unpack($format, $this->read(4));
168
        return $value;
169
    }
170
171
    /**
172
     * Read signed 32-bit integer (long) data from the stream
173
     *
174
     * @return int
175
     */
176 View Code Duplication
    public function readSignedInteger32()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
177
    {
178
        $data = $this->read(4);
179
180
        if ($this->getByteOrder() !== ByteOrder::MACHINE_ENDIAN
181
            && $this->getByteOrder() !== $this->getMachineByteOrder()
182
        ) {
183
            $data = strrev($data);
184
        }
185
186
        list(, $value) = unpack('l', $data);
187
        return $value;
188
    }
189
190
    /**
191
     * Read unsigned 64-bit integer (long long) data from the stream
192
     *
193
     * @return int
194
     */
195 View Code Duplication
    public function readUnsignedInteger64()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
196
    {
197
        switch ($this->getByteOrder()) {
198
            case ByteOrder::BIG_ENDIAN:
199
                $format = 'J';
200
                break;
201
            case ByteOrder::LITTLE_ENDIAN:
202
                $format = 'P';
203
                break;
204
            default:
205
                $format = 'Q';
206
        }
207
208
        list(, $value) = unpack($format, $this->read(8));
209
        return $value;
210
    }
211
212
    /**
213
     * Read signed 64-bit integer (long long) data from the stream
214
     *
215
     * @return int
216
     */
217 View Code Duplication
    public function readSignedInteger64()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
218
    {
219
        $data = $this->read(8);
220
221
        if ($this->getByteOrder() !== ByteOrder::MACHINE_ENDIAN
222
            && $this->getByteOrder() !== $this->getMachineByteOrder()
223
        ) {
224
            $data = strrev($data);
225
        }
226
227
        list(, $value) = unpack('q', $data);
228
        return $value;
229
    }
230
}
231