Reader   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 224
Duplicated Lines 6.25 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 78.21%

Importance

Changes 0
Metric Value
wmc 24
lcom 1
cbo 0
dl 14
loc 224
ccs 61
cts 78
cp 0.7821
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
setPosition() 0 1 ?
getPosition() 0 1 ?
getLength() 0 1 ?
readString() 0 1 ?
A readBytes() 0 18 3
A readByte() 0 6 1
A readUInt16() 0 6 1
A readUInt32() 7 15 3
A readInt32() 0 14 4
B readUInt64() 7 29 8
A readGUID() 0 17 1
A readCompressedUInt32() 0 15 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace CHMLib\Reader;
4
5
/**
6
 * Read data from a generic source.
7
 */
8
abstract class Reader
9
{
10
    /**
11
     * Set the current position.
12
     *
13
     * @param int $position
14
     *
15
     * @throws \Exception Throws an Exception in case of errors.
16
     */
17
    abstract public function setPosition($position);
18
19
    /**
20
     * Get the current position.
21
     *
22
     * @throws \Exception Throws an Exception in case of errors.
23
     *
24
     * @return int
25
     */
26
    abstract public function getPosition();
27
28
    /**
29
     * Get the total length of the data.
30
     *
31
     * @return int
32
     */
33
    abstract public function getLength();
34
35
    /**
36
     * Read a fixed number of bytes as a raw string.
37
     *
38
     * @param int $length The number of bytes to read.
39
     *
40
     * @throws \Exception Throws an Exception in case of errors.
41
     *
42
     * @return string
43
     */
44
    abstract public function readString($length);
45
46
    /**
47
     * Read a fixed number of bytes and return it as a byte array.
48
     *
49
     * @param int $length The number of bytes to read.
50
     *
51
     * @throws \Exception Throws an Exception in case of errors.
52
     *
53
     * @return int[]
54
     */
55 4
    public function readBytes($length)
56
    {
57 4
        $data = $this->readString($length);
58
59
        switch ($length) {
60 4
            case 0:
61
                $result = array();
62
                break;
63 4
            case 1:
64 4
                $result = array(ord($data[0]));
65 4
                break;
66
            default:
67
                $result = unpack('C*', $data);
68
                break;
69
        }
70
71 4
        return $result;
72
    }
73
74
    /**
75
     * Read a byte.
76
     *
77
     * @throws \Exception Throws an Exception in case of errors.
78
     *
79
     * @return int
80
     */
81 4
    public function readByte()
82
    {
83 4
        $bytes = $this->readBytes(1);
84
85 4
        return $bytes[0];
86
    }
87
88
    /**
89
     * Read an unsigned 16-bit integer (little endian).
90
     *
91
     * @throws \Exception Throws an Exception in case of errors.
92
     *
93
     * @return int
94
     */
95 26
    public function readUInt16()
96
    {
97 26
        $chunk = unpack('v', $this->readString(2));
98
99 26
        return array_pop($chunk);
100
    }
101
102
    /**
103
     * Read an unsigned 32-bit integer (little endian).
104
     *
105
     * @throws \Exception Throws an Exception in case of errors.
106
     *
107
     * @return int
108
     */
109 4
    public function readUInt32()
110
    {
111 4
        $bytes = $this->readString(4);
112 4
        $chunk = unpack('V', $bytes);
113 4
        $int = array_pop($chunk);
114 4 View Code Duplication
        if ($int < 0) {
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...
115
            $bits = decbin($int);
116
            $int = bindec(substr($bits, 0, -1)) * 2;
117
            if (substr($bits, -1) === '1') {
118
                $int += 1;
119
            }
120
        }
121
122 4
        return $int;
123
    }
124
125
    /**
126
     * Read a signed 32-bit integer (little endian).
127
     *
128
     * @throws \Exception Throws an Exception in case of errors.
129
     *
130
     * @return int
131
     */
132 4
    public function readInt32()
133
    {
134 4
        static $osIsBigEndian;
135 4
        if (!isset($osIsBigEndian)) {
136 1
            $osIsBigEndian = (pack('L', 1) === pack('N', 1)) ? true : false;
137
        }
138 4
        $data = $this->readString(4);
139 4
        if ($osIsBigEndian) {
140
            $data = strrev($data);
141
        }
142 4
        $chunk = unpack('l', $data);
143
144 4
        return array_pop($chunk);
145
    }
146
147
    /**
148
     * Read an unsigned 64-bit integer (little endian).
149
     *
150
     * @throws \Exception Throws an Exception in case of errors.
151
     *
152
     * @return int|float
153
     */
154 4
    public function readUInt64()
155
    {
156 4
        static $nativeUnpack;
157
158 4
        if (!isset($nativeUnpack)) {
159 1
            $nativeUnpack = (PHP_INT_SIZE >= 8 && version_compare(PHP_VERSION, '5.6.3') >= 0) ? true : false;
160
        }
161 4
        if ($nativeUnpack) {
162 4
            $chunk = unpack('P', $this->readString(8));
163 4
            $result = array_pop($chunk);
164 4 View Code Duplication
            if ($result < 0) {
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...
165
                $bits = decbin($result);
166
                $result = bindec(substr($bits, 0, -1)) * 2;
167
                if (substr($bits, -1) === '1') {
168 4
                    $result += 1;
169
                }
170
            }
171
        } else {
172
            $n1 = $this->readUInt32();
173
            $n2 = $this->readUInt32();
174
175
            $result = $n2 * 0x100000000 + $n1;
176
            if ($result <= PHP_INT_MAX) {
177
                $result = (int) $result;
178
            }
179
        }
180
181 4
        return $result;
182
    }
183
184
    /**
185
     * Read a GUID.
186
     *
187
     * @throws \Exception Throws an Exception in case of errors.
188
     *
189
     * @return string
190
     *
191
     * @example '{5D02926A-212E-11D0-9DF9-00A0C922E6EC}'
192
     */
193 4
    public function readGUID()
194
    {
195 4
        return sprintf(
196 4
            '{%1$08X-%2$04X-%3$04X-%4$02X%5$02X-%6$02X%7$02X-%8$02X%9$02X-%10$02X%11$02X}',
197 4
            $this->readUInt32(),
198 4
            $this->readUInt16(),
199 4
            $this->readUInt16(),
200 4
            $this->readByte(),
201 4
            $this->readByte(),
202 4
            $this->readByte(),
203 4
            $this->readByte(),
204 4
            $this->readByte(),
205 4
            $this->readByte(),
206 4
            $this->readByte(),
207 4
            $this->readByte()
208
        );
209
    }
210
211
    /**
212
     * Read a compressed unsigned 32-bit integer (little endian).
213
     *
214
     * @return number
215
     */
216 4
    public function readCompressedUInt32()
217
    {
218 4
        $result = 0;
219 4
        for (; ;) {
220 4
            $result <<= 7;
221 4
            $byte = $this->readByte();
222 4
            if ($byte < 0x80) {
223 4
                $result += $byte;
224 4
                break;
225
            }
226 4
            $result += $byte & 0x7f;
227
        }
228
229 4
        return $result;
230
    }
231
}
232