ByteCodeReader   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 79.41%

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A read() 0 15 4
A refreshCommand() 0 4 2
A __construct() 0 3 1
B readBlock() 0 23 7
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
}