Completed
Push — master ( 55c1c2...23e08d )
by Matt
8s
created

Single::read()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 22
ccs 14
cts 14
cp 1
rs 8.6737
cc 5
eloc 13
nc 5
nop 2
crap 5
1
<?php
2
3
namespace PhpBinaryReader\Type;
4
5
use PhpBinaryReader\BinaryReader;
6
7
class Single implements TypeInterface
8
{
9
    /**
10
     * Returns a 4-bytes floating-point
11
     *
12
     * @param \PhpBinaryReader\BinaryReader $br
13
     * @param null $length
14
     *
15
     * @return float
16
     * @throws \OutOfBoundsException
17
     */
18 14
    public function read(BinaryReader &$br, $length = null)
19
    {
20 14
        if (!$br->canReadBytes(4)) {
21 4
            throw new \OutOfBoundsException('Cannot read 4-bytes floating-point, it exceeds the boundary of the file');
22
        }
23
24 10
        $segment = $br->readFromHandle(4);
25
26 10
        if ($br->getCurrentBit() !== 0) {
27 4
            $data = unpack('N', $segment)[1];
28 4
            $data = $this->bitReader($br, $data);
29
30 4
            $endian = $br->getMachineByteOrder() === $br->getEndian() ? 'N' : 'V';
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison === seems to always evaluate to false as the types of $br->getMachineByteOrder() (integer) and $br->getEndian() (string) can never be identical. Maybe you want to use a loose comparison == instead?
Loading history...
31 4
            $segment = pack($endian, $data);
32 10
        } elseif ($br->getMachineByteOrder() !== $br->getEndian()) {
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison !== seems to always evaluate to true as the types of $br->getMachineByteOrder() (integer) and $br->getEndian() (string) can never be identical. Maybe you want to use a loose comparison != instead?
Loading history...
33 4
            $segment = pack('N', unpack('V', $segment)[1]);
34 4
        }
35
36 10
        $value = unpack('f', $segment)[1];
37
38 10
        return $value;
39
    }
40
41
    /**
42
     * @param \PhpBinaryReader\BinaryReader $br
43
     * @param int $data
44
     *
45
     * @return int
46
     */
47 4
    private function bitReader(BinaryReader $br, $data)
48
    {
49 4
        $mask = 0x7FFFFFFF >> ($br->getCurrentBit() - 1);
50 4
        $value = (($data >> (8 - $br->getCurrentBit())) & $mask) | ($br->getNextByte() << (24 + $br->getCurrentBit()));
51 4
        $br->setNextByte($data & 0xFF);
52
53 4
        return $value;
54
    }
55
}
56