Passed
Push — master ( 45b054...06b8db )
by Sam
02:37
created

ByteCodeReader   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 88.24%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
c 1
b 0
f 0
dl 0
loc 65
ccs 30
cts 34
cp 0.8824
rs 10
wmc 14

4 Methods

Rating   Name   Duplication   Size   Complexity  
B readBlock() 0 23 7
A read() 0 15 4
A refreshCommand() 0 4 2
A __construct() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace SPSS;
5
6
/**
7
 * Class ByteCodeCompressor
8
 * Abstracts bytecode (de)compression
9
 * @package SPSS
10
 */
11
class ByteCodeReader
12
{
13
    private const BYTE_SYS_MISSING = 255;
14
    private const BYTE_SPACES = 254;
15
    private const BYTE_LITERAL = 253;
16
    private const BYTE_NUL = 0;
17
    /** @var Buffer  */
18
    private $buffer;
19
20
    private $commandBuffer = [];
21
    private $dataBuffer = '';
22
23 8
    public function __construct(Buffer $buffer)
24
    {
25 8
        $this->buffer = $buffer;
26 8
    }
27
28 7
    private function readBlock(): string
29
    {
30 7
        $this->refreshCommand();
31 7
        if (!is_array($this->commandBuffer)) {
32
            return '';
33
        }
34 7
        switch($cmd = array_shift($this->commandBuffer)) {
35 7
            case self::BYTE_SYS_MISSING:
36
                return pack('d', -PHP_FLOAT_MAX);
37 7
            case self::BYTE_SPACES:
38 4
                return '        ';
39 7
            case self::BYTE_LITERAL:
40 6
                $result = $this->buffer->read(8);
41 6
                if ($result === false) {
42
                     throw new \Exception('Stream read error');
43
                }
44 6
                return $result;
45 4
            case self::BYTE_NUL:
46 4
                return $this->readBlock();
47
                throw new \Exception('0 byte command not supported');
0 ignored issues
show
Unused Code introduced by
ThrowNode is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
48
            default:
49
                // Bias
50
                throw new \Exception('bias encoding not supported: ' . $cmd);
51
        }
52
    }
53
54 7
    private function refreshCommand()
55
    {
56 7
        if (empty($this->commandBuffer)) {
57 7
            $this->commandBuffer = $this->buffer->readBytes(8);
58
        }
59 7
    }
60
61 7
    public function read(int $length): string
62
    {
63 7
        $result = substr($this->dataBuffer, 0, $length);
64 7
        $length -= strlen($result);
65 7
        while ($length > 0 && ($data = $this->readBlock()) !== '')
66
        {
67 7
            if ($length >= strlen($data)) {
68 6
                $result .= $data;
69 6
                $length -= strlen($data);
70
            } else {
71 2
                $this->dataBuffer .= substr($data, $length);
72 2
                return $result . substr($data, 0, $length);
73
            }
74
        }
75 5
        return $result;
76
    }
77
78
}