Int16   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 113
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 113
loc 113
ccs 39
cts 39
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A readSigned() 17 17 3
A bitReader() 12 12 1
A setEndianBig() 4 4 1
A getEndianBig() 4 4 1
A setEndianLittle() 4 4 1
A getEndianLittle() 4 4 1
A read() 18 18 4

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 PhpBinaryReader\Type;
4
5
use PhpBinaryReader\BinaryReader;
6
use PhpBinaryReader\BitMask;
7
use PhpBinaryReader\Endian;
8
9 View Code Duplication
class Int16 implements TypeInterface
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...
10
{
11
    /**
12
     * @var string
13
     */
14
    private $endianBig = 'n';
15
16
    /**
17
     * @var string
18
     */
19
    private $endianLittle = 'v';
20
21
    /**
22
     * Returns an Unsigned 16-bit Integer
23
     *
24
     * @param  \PhpBinaryReader\BinaryReader $br
25
     * @param  null                          $length
26
     * @return int
27
     * @throws \OutOfBoundsException
28
     */
29 26
    public function read(BinaryReader &$br, $length = null)
30
    {
31 26
        if (!$br->canReadBytes(2)) {
32 4
            throw new \OutOfBoundsException('Cannot read 16-bit int, it exceeds the boundary of the file');
33
        }
34
35 22
        $endian = $br->getEndian() == Endian::ENDIAN_BIG ? $this->endianBig : $this->endianLittle;
36 22
        $segment = $br->readFromHandle(2);
37
38 22
        $data = unpack($endian, $segment);
39 22
        $data = $data[1];
40
41 22
        if ($br->getCurrentBit() != 0) {
42 4
            $data = $this->bitReader($br, $data);
43
        }
44
45 22
        return $data;
46
    }
47
48
    /**
49
     * Returns a Signed 16-bit Integer
50
     *
51
     * @param  \PhpBinaryReader\BinaryReader $br
52
     * @return int
53
     */
54 10
    public function readSigned(&$br)
55
    {
56 10
        $this->setEndianBig('s');
57 10
        $this->setEndianLittle('s');
58
59 10
        $value = $this->read($br);
60
61 10
        $this->setEndianBig('n');
62 10
        $this->setEndianLittle('v');
63
64 10
        $endian = new Endian();
65 10
        if ($br->getMachineByteOrder() != Endian::ENDIAN_LITTLE && $br->getEndian() == Endian::ENDIAN_LITTLE) {
66 2
            return $endian->convert($value);
67
        } else {
68 8
            return $value;
69
        }
70
    }
71
72
    /**
73
     * @param  \PhpBinaryReader\BinaryReader $br
74
     * @param  int                           $data
75
     * @return int
76
     */
77 4
    private function bitReader(&$br, $data)
78
    {
79 4
        $bitmask = new BitMask();
80 4
        $loMask = $bitmask->getMask($br->getCurrentBit(), BitMask::MASK_LO);
81 4
        $hiMask = $bitmask->getMask($br->getCurrentBit(), BitMask::MASK_HI);
82 4
        $hiBits = ($br->getNextByte() & $hiMask) << 8;
83 4
        $miBits = ($data & 0xFF00) >> (8 - $br->getCurrentBit());
84 4
        $loBits = ($data & $loMask);
85 4
        $br->setNextByte($data & 0xFF);
86
87 4
        return $hiBits | $miBits | $loBits;
88
    }
89
90
    /**
91
     * @param string $endianBig
92
     */
93 11
    public function setEndianBig($endianBig)
94
    {
95 11
        $this->endianBig = $endianBig;
96 11
    }
97
98
    /**
99
     * @return string
100
     */
101 1
    public function getEndianBig()
102
    {
103 1
        return $this->endianBig;
104
    }
105
106
    /**
107
     * @param string $endianLittle
108
     */
109 11
    public function setEndianLittle($endianLittle)
110
    {
111 11
        $this->endianLittle = $endianLittle;
112 11
    }
113
114
    /**
115
     * @return string
116
     */
117 1
    public function getEndianLittle()
118
    {
119 1
        return $this->endianLittle;
120
    }
121
}
122