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

ShortReader::readSigned()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 13
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 13
loc 13
ccs 0
cts 9
cp 0
rs 9.4285
cc 3
eloc 7
nc 2
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
 * Short (16-bit integer) reader
15
 *
16
 * @package GravityMedia\Stream\Reader
17
 */
18 View Code Duplication
class ShortReader 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 short (16-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 = 'S*';
41
        }
42
43
        list(, $value) = unpack($format, $this->getStream()->read(2));
44
        return $value;
45
    }
46
47
    /**
48
     * Read signed short (16-bit integer) data from the stream
49
     *
50
     * @return int
51
     */
52
    protected function readSigned()
53
    {
54
        $data = $this->getStream()->read(2);
55
56
        if (ByteOrder::MACHINE_ENDIAN !== $this->getByteOrder()
57
            && $this->getMachineByteOrder() !== $this->getByteOrder()
58
        ) {
59
            $data = strrev($data);
60
        }
61
62
        list(, $value) = unpack('s*', $data);
63
        return $value;
64
    }
65
}
66