Failed Conditions
Pull Request — master (#14)
by
unknown
02:57
created

Int64::getEndianLittle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace PhpBinaryReader\Type;
4
5
use PhpBinaryReader\BinaryReader;
6
use PhpBinaryReader\BitMask;
7
use PhpBinaryReader\Endian;
8
9
class Int64 implements TypeInterface
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 64-bit Integer
23
     *
24
     * @param  \PhpBinaryReader\BinaryReader $br
25
     * @param  null                          $length
26
     * @return int
27
     * @throws \OutOfBoundsException
28
     */
29 20
    public function read(BinaryReader &$br, $length = null)
30
    {
31 20
        if (!$br->canReadBytes(8)) {
32 4
            throw new \OutOfBoundsException('Cannot read 64-bit int, it exceeds the boundary of the file');
33
        }
34
35 16
        $endian = $br->getEndian() == Endian::ENDIAN_BIG ? $this->endianBig : $this->endianLittle;
36 16
        $firstSegment = $br->readFromHandle(4);
37 16
        $secondSegment = $br->readFromHandle(4);
38
39 16
        $firstHalf = unpack($endian, $firstSegment)[1];
40 16
        $secondHalf = unpack($endian, $secondSegment)[1];
41
42 16
        if ($br->getEndian() == Endian::ENDIAN_BIG)
43 16
            $value = bcadd($secondHalf, bcmul($firstHalf, "4294967296"));
44
        else
45 10
            $value = bcadd($firstHalf, bcmul($secondHalf, "4294967296"));
46
47 16
        if ($br->getCurrentBit() != 0) {
48 4
            $data = $this->bitReader($br, $data);
0 ignored issues
show
Bug introduced by
The variable $data seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
Unused Code introduced by
$data is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
49
        }
50
51 12
        return $value;
52
    }
53
54
    /**
55
     * Returns a Signed 64-Bit Integer
56
     *
57
     * @param  \PhpBinaryReader\BinaryReader $br
58
     * @return int
59
     */
60 8
    public function readSigned(&$br)
61
    {
62 8
        $value = $this->read($br);
63 8
        if(bccomp($value, bcpow(2, 63)) >= 0) $value = bcsub($value, bcpow(2, 64));
64
65 8
        return $value;
66
    }
67
68
    /**
69
     * @param  \PhpBinaryReader\BinaryReader $br
70
     * @param  int                           $data
71
     * @return int
72
     */
73
    private function bitReader(&$br, $data)
74
    {
75
        $bitmask = new BitMask();
76
        $loMask = $bitmask->getMask($br->getCurrentBit(), BitMask::MASK_LO);
77
        $hiMask = $bitmask->getMask($br->getCurrentBit(), BitMask::MASK_HI);
78
        $hiBits = ($br->getNextByte() & $hiMask) << 56;
79
        $miBits = ($data & 0xFFFFFFFFFFFFFF00) >> (8 - $br->getCurrentBit());
80
        $loBits = ($data & $loMask);
81
        $br->setNextByte($data & 0xFF);
82
83
        return $hiBits | $miBits | $loBits;
84
    }
85
86
    /**
87
     * @param string $endianBig
88
     */
89 1
    public function setEndianBig($endianBig)
90
    {
91 1
        $this->endianBig = $endianBig;
92 1
    }
93
94
    /**
95
     * @return string
96
     */
97 1
    public function getEndianBig()
98
    {
99 1
        return $this->endianBig;
100
    }
101
102
    /**
103
     * @param string $endianLittle
104
     */
105 1
    public function setEndianLittle($endianLittle)
106
    {
107 1
        $this->endianLittle = $endianLittle;
108 1
    }
109
110
    /**
111
     * @return string
112
     */
113 1
    public function getEndianLittle()
114
    {
115 1
        return $this->endianLittle;
116
    }
117
}
118