Completed
Push — master ( 44b0be...4a5c62 )
by Daniel
02:13
created

LongReader::readUnsigned()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 12

Duplication

Lines 16
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 16
loc 16
ccs 0
cts 13
cp 0
rs 9.4285
cc 3
eloc 12
nc 3
nop 0
crap 12
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\Enum\ByteOrder;
11
use GravityMedia\Stream\Exception;
12
13
/**
14
 * Long (32-bit integer) reader
15
 *
16
 * @package GravityMedia\Stream\Reader
17
 */
18 View Code Duplication
class LongReader extends AbstractIntegerReader
0 ignored issues
show
Duplication introduced by
This class 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...
19
{
20
    /**
21
     * Use byte order aware trait
22
     */
23
    use ByteOrderAwareTrait;
24
25
    /**
26
     * Read unsigned long (32-bit integer) data from the stream
27
     *
28
     * @return int
29
     */
30
    protected function readUnsigned()
31
    {
32
        switch ($this->getByteOrder()) {
33
            case ByteOrder::BIG_ENDIAN:
34
                $format = 'N*';
35
                break;
36
            case ByteOrder::LITTLE_ENDIAN:
37
                $format = 'V*';
38
                break;
39
            default:
40
                $format = 'L*';
41
        }
42
43
        list(, $value) = unpack($format, $this->getStream()->read(4));
44
        return $value;
45
    }
46
47
    /**
48
     * Read signed long (32-bit integer) data from the stream
49
     *
50
     * @return int
51
     */
52
    protected function readSigned()
53
    {
54
        $data = $this->stream->read(4);
55
56
        if (ByteOrder::MACHINE_ENDIAN !== $this->getByteOrder()
57
            && $this->getMachineByteOrder() !== $this->getByteOrder()
58
        ) {
59
            $data = strrev($data);
60
        }
61
62
        list(, $value) = unpack('l*', $data);
63
        return $value;
64
    }
65
}
66