Completed
Push — master ( 903512...18e987 )
by Daniel
13:06
created

IntegerReader::readUnsignedInteger24()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 3
eloc 9
nc 4
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
        $byteOrder = $this->getByteOrder();
98
        if ($byteOrder === ByteOrder::MACHINE_ENDIAN) {
99
            $byteOrder = $this->getMachineByteOrder();
100
        }
101
102
        if ($byteOrder !== $this->getMachineByteOrder()) {
103
            $data = strrev($data);
104
        }
105
106
        $values = unpack('C3', $data);
107
        return $values[1] | $values[2] << 8 | $values[3] << 16;
108
    }
109
110
    /**
111
     * Read signed 24-bit integer (short) data from the stream
112
     *
113
     * @return int
114
     */
115
    public function readSignedInteger24()
116
    {
117
        $value = $this->readUnsignedInteger24();
118
119
        if ($value & 0x800000) {
120
            return $value - 2 ** 24;
121
        }
122
123
        return $value;
124
    }
125
126
    /**
127
     * Read unsigned 32-bit integer (long) data from the stream
128
     *
129
     * @return int
130
     */
131 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...
132
    {
133
        switch ($this->getByteOrder()) {
134
            case ByteOrder::BIG_ENDIAN:
135
                $format = 'N';
136
                break;
137
            case ByteOrder::LITTLE_ENDIAN:
138
                $format = 'V';
139
                break;
140
            default:
141
                $format = 'L';
142
        }
143
144
        list(, $value) = unpack($format, $this->read(4));
145
        return $value;
146
    }
147
148
    /**
149
     * Read signed 32-bit integer (long) data from the stream
150
     *
151
     * @return int
152
     */
153 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...
154
    {
155
        $data = $this->read(4);
156
157
        if ($this->getByteOrder() !== ByteOrder::MACHINE_ENDIAN
158
            && $this->getByteOrder() !== $this->getMachineByteOrder()
159
        ) {
160
            $data = strrev($data);
161
        }
162
163
        list(, $value) = unpack('l', $data);
164
        return $value;
165
    }
166
167
    /**
168
     * Read unsigned 64-bit integer (long long) data from the stream
169
     *
170
     * @return int
171
     */
172 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...
173
    {
174
        switch ($this->getByteOrder()) {
175
            case ByteOrder::BIG_ENDIAN:
176
                $format = 'J';
177
                break;
178
            case ByteOrder::LITTLE_ENDIAN:
179
                $format = 'P';
180
                break;
181
            default:
182
                $format = 'Q';
183
        }
184
185
        list(, $value) = unpack($format, $this->read(8));
186
        return $value;
187
    }
188
189
    /**
190
     * Read signed 64-bit integer (long long) data from the stream
191
     *
192
     * @return int
193
     */
194 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...
195
    {
196
        $data = $this->read(8);
197
198
        if ($this->getByteOrder() !== ByteOrder::MACHINE_ENDIAN
199
            && $this->getByteOrder() !== $this->getMachineByteOrder()
200
        ) {
201
            $data = strrev($data);
202
        }
203
204
        list(, $value) = unpack('q', $data);
205
        return $value;
206
    }
207
}
208