ByteCodeReader::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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 3
    public function __construct(Buffer $buffer)
24
    {
25 3
        $this->buffer = $buffer;
26 3
    }
27
28 3
    private function readBlock(): string
29
    {
30 3
        $this->refreshCommand();
31 3
        if (!is_array($this->commandBuffer)) {
32
            return '';
33
        }
34 3
        switch($cmd = array_shift($this->commandBuffer)) {
35 3
            case self::BYTE_SYS_MISSING:
36
                return pack('d', -PHP_FLOAT_MAX);
37 3
            case self::BYTE_SPACES:
38
                return '        ';
39 3
            case self::BYTE_LITERAL:
40 3
                $result = $this->buffer->read(8);
41 3
                if ($result === false) {
42
                     throw new \Exception('Stream read error');
43
                }
44 3
                return $result;
45
            case self::BYTE_NUL:
46
                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 3
    private function refreshCommand()
55
    {
56 3
        if (empty($this->commandBuffer)) {
57 3
            $this->commandBuffer = $this->buffer->readBytes(8);
58
        }
59 3
    }
60
61 3
    public function read(int $length): string
62
    {
63 3
        $result = substr($this->dataBuffer, 0, $length);
64 3
        $length -= strlen($result);
65 3
        while ($length > 0 && ($data = $this->readBlock()) !== '')
66
        {
67 3
            if ($length >= strlen($data)) {
68 2
                $result .= $data;
69 2
                $length -= strlen($data);
70
            } else {
71 2
                $this->dataBuffer .= substr($data, $length);
72 2
                return $result . substr($data, 0, $length);
73
            }
74
        }
75 1
        return $result;
76
    }
77
78
}