Completed
Pull Request — master (#21)
by Eugene
09:41
created

BufferUnpacker::setTransformers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 * This file is part of the rybakit/msgpack.php package.
5
 *
6
 * (c) Eugene Leonovich <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace MessagePack;
13
14
use MessagePack\Exception\InsufficientDataException;
15
use MessagePack\Exception\IntegerOverflowException;
16
use MessagePack\Exception\InvalidOptionException;
17
use MessagePack\Exception\UnpackingFailedException;
18
use MessagePack\TypeTransformer\Extension;
19
20
class BufferUnpacker
21
{
22
    private $buffer;
23
    private $offset = 0;
24
    private $isBigIntAsStr;
25
    private $isBigIntAsGmp;
26
27
    /**
28
     * @var Extension[]|null
29
     */
30
    private $transformers;
31
32
    /**
33
     * @param string $buffer
34
     * @param UnpackOptions|int|null $options
35
     *
36
     * @throws InvalidOptionException
37
     */
38 123
    public function __construct($buffer = '', $options = null)
39
    {
40 123
        if (!$options instanceof UnpackOptions) {
41 123
            $options = UnpackOptions::fromBitmask($options);
42
        }
43
44 123
        $this->isBigIntAsStr = $options->isBigIntAsStrMode();
45 123
        $this->isBigIntAsGmp = $options->isBigIntAsGmpMode();
46
47 123
        $this->buffer = $buffer;
48 123
    }
49
50
    public function registerTransformer(Extension $ext)
51
    {
52
        $this->transformers[$ext->getType()] = $ext;
53 2
    }
54
55 2
    /**
56 2
     * @param string $data
57
     *
58
     * @return $this
59
     */
60
    public function append($data)
61 1
    {
62
        $this->buffer .= $data;
63 1
64
        return $this;
65
    }
66
67
    /**
68
     * @param string $buffer
69
     *
70
     * @return $this
71 5
     */
72
    public function reset($buffer = '')
73 5
    {
74
        $this->buffer = $buffer;
75 5
        $this->offset = 0;
76
77
        return $this;
78
    }
79
80
    /**
81
     * @param int $length
82
     *
83 109
     * @return $this
84
     */
85 109
    public function skip($length)
86 109
    {
87
        $this->offset += $length;
88 109
89
        return $this;
90
    }
91
92
    /**
93
     * @return array
94 3
     */
95
    public function tryUnpack()
96 3
    {
97 3
        $data = [];
98
        $offset = $this->offset;
99
100
        try {
101 3
            do {
102 3
                $data[] = $this->unpack();
103 3
                $offset = $this->offset;
104 1
            } while (isset($this->buffer[$this->offset]));
105 1
        } catch (InsufficientDataException $e) {
106
            $this->offset = $offset;
107
        }
108 3
109 3
        if ($this->offset) {
110 3
            $this->buffer = isset($this->buffer[$this->offset]) ? \substr($this->buffer, $this->offset) : '';
111
            $this->offset = 0;
112
        }
113 3
114
        return $data;
115
    }
116
117
    /**
118
     * @throws UnpackingFailedException
119
     *
120
     * @return mixed
121 118
     */
122
    public function unpack()
123 118
    {
124 4
        if (!isset($this->buffer[$this->offset])) {
125
            throw InsufficientDataException::fromOffset($this->buffer, $this->offset, 1);
126
        }
127 116
128 116
        $c = \ord($this->buffer[$this->offset]);
129
        ++$this->offset;
130
131 116
        // fixint
132 20
        if ($c <= 0x7f) {
133
            return $c;
134
        }
135 113
        // fixstr
136 13
        if ($c >= 0xa0 && $c <= 0xbf) {
137
            return ($c & 0x1f) ? $this->unpackStr($c & 0x1f) : '';
138
        }
139 107
        // fixarray
140 6
        if ($c >= 0x90 && $c <= 0x9f) {
141
            return ($c & 0xf) ? $this->unpackArray($c & 0xf) : [];
142
        }
143 104
        // fixmap
144 11
        if ($c >= 0x80 && $c <= 0x8f) {
145
            return ($c & 0xf) ? $this->unpackMap($c & 0xf) : [];
146
        }
147 100
        // negfixint
148 4
        if ($c >= 0xe0) {
149
            return $c - 256;
150
        }
151 96
152 96
        switch ($c) {
153 95
            case 0xc0: return null;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
154 93
            case 0xc2: return false;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
155
            case 0xc3: return true;
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
156
157 88
            // MP_BIN
158 84
            case 0xc4: return $this->unpackStr($this->unpackUint8());
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
159 83
            case 0xc5: return $this->unpackStr($this->unpackUint16());
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
160
            case 0xc6: return $this->unpackStr($this->unpackUint32());
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
161
162 82
            // MP_FLOAT
163 79
            case 0xca: return $this->unpackFloat32();
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
164
            case 0xcb: return $this->unpackFloat64();
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
165
166 75
            // MP_UINT
167 71
            case 0xcc: return $this->unpackUint8();
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
168 66
            case 0xcd: return $this->unpackUint16();
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
169 62
            case 0xce: return $this->unpackUint32();
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
170
            case 0xcf: return $this->unpackUint64();
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
171
172 52
            // MP_INT
173 47
            case 0xd0: return $this->unpackInt8();
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
174 42
            case 0xd1: return $this->unpackInt16();
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
175 37
            case 0xd2: return $this->unpackInt32();
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
176
            case 0xd3: return $this->unpackInt64();
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
177
178 32
            // MP_STR
179 28
            case 0xd9: return $this->unpackStr($this->unpackUint8());
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
180 26
            case 0xda: return $this->unpackStr($this->unpackUint16());
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
181
            case 0xdb: return $this->unpackStr($this->unpackUint32());
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
182
183 25
            // MP_ARRAY
184 23
            case 0xdc: return $this->unpackArray($this->unpackUint16());
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
185
            case 0xdd: return $this->unpackArray($this->unpackUint32());
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
186
187 22
            // MP_MAP
188 20
            case 0xde: return $this->unpackMap($this->unpackUint16());
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
189
            case 0xdf: return $this->unpackMap($this->unpackUint32());
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
190
191 19
            // MP_EXT
192 16
            case 0xd4: return $this->unpackExt(1);
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
193 14
            case 0xd5: return $this->unpackExt(2);
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
194 12
            case 0xd6: return $this->unpackExt(4);
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
195 10
            case 0xd7: return $this->unpackExt(8);
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
196 8
            case 0xd8: return $this->unpackExt(16);
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
197 5
            case 0xc7: return $this->unpackExt($this->unpackUint8());
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
198 3
            case 0xc8: return $this->unpackExt($this->unpackUint16());
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
199
            case 0xc9: return $this->unpackExt($this->unpackUint32());
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
Terminating statement must be on a line by itself

As per the PSR-2 coding standard, the break (or other terminating) statement must be on a line of its own.

switch ($expr) {
     case "A":
         doSomething();
         break; //wrong
     case "B":
         doSomething();
         break; //right
     case "C:":
         doSomething();
         return true; //right
 }

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
200
        }
201 1
202
        throw new UnpackingFailedException(\sprintf('Unknown code: 0x%x.', $c));
203
    }
204 18
205
    public function unpackUint8()
206 18
    {
207 2
        if (!isset($this->buffer[$this->offset])) {
208
            throw InsufficientDataException::fromOffset($this->buffer, $this->offset, 1);
209
        }
210 16
211 16
        return \ord($this->buffer[$this->offset++]);
212
    }
213 16
214
    public function unpackUint16()
215
    {
216 15
        if (!isset($this->buffer[$this->offset + 1])) {
217
            throw InsufficientDataException::fromOffset($this->buffer, $this->offset, 2);
218 15
        }
219 2
220
        $hi = \ord($this->buffer[$this->offset]);
221
        $lo = \ord($this->buffer[$this->offset + 1]);
222 13
        $this->offset += 2;
223 13
224 13
        return $hi << 8 | $lo;
225
    }
226 13
227 View Code Duplication
    public function unpackUint32()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
228
    {
229 11
        if (!isset($this->buffer[$this->offset + 3])) {
230
            throw InsufficientDataException::fromOffset($this->buffer, $this->offset, 4);
231 11
        }
232 2
233
        $num = \substr($this->buffer, $this->offset, 4);
234
        $this->offset += 4;
235 9
236 9
        $num = \unpack('N', $num);
237
238 9
        return $num[1];
239
    }
240 9
241
    public function unpackUint64()
242
    {
243 10
        if (!isset($this->buffer[$this->offset + 7])) {
244
            throw InsufficientDataException::fromOffset($this->buffer, $this->offset, 8);
245 10
        }
246 1
247
        $num = \substr($this->buffer, $this->offset, 8);
248
        $this->offset += 8;
249 9
250 9
        //$num = \unpack('J', $num);
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
251
252
        $set = \unpack('N2', $num);
253
        $value = $set[1] << 32 | $set[2];
254 9
255 9
        // PHP does not support unsigned integers.
256
        // If a number is bigger than 2^63, it will be interpreted as a float.
257
        // @link http://php.net/manual/en/language.types.integer.php#language.types.integer.overflow
258
259
        return $value < 0 ? $this->handleIntOverflow($value) : $value;
260
    }
261 9
262
    public function unpackInt8()
263
    {
264 15
        if (!isset($this->buffer[$this->offset])) {
265
            throw InsufficientDataException::fromOffset($this->buffer, $this->offset, 1);
266 15
        }
267 1
268
        $num = \ord($this->buffer[$this->offset]);
269
        ++$this->offset;
270 14
271 14
        return $num > 0x7f ? $num - 256 : $num;
272
    }
273 14
274 3
    public function unpackInt16()
275
    {
276
        if (!isset($this->buffer[$this->offset + 1])) {
277 11
            throw InsufficientDataException::fromOffset($this->buffer, $this->offset, 2);
278
        }
279
280 5
        $hi = \ord($this->buffer[$this->offset]);
281
        $lo = \ord($this->buffer[$this->offset + 1]);
282 5
        $this->offset += 2;
283 1
284
        if ($hi > 0x7f) {
285
            return -(0x010000 - ($hi << 8 | $lo));
286 4
        }
287 4
288 4
        return $hi << 8 | $lo;
289
    }
290 4
291 3 View Code Duplication
    public function unpackInt32()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
292
    {
293
        if (!isset($this->buffer[$this->offset + 3])) {
294 1
            throw InsufficientDataException::fromOffset($this->buffer, $this->offset, 4);
295
        }
296
297 5
        $num = \substr($this->buffer, $this->offset, 4);
298
        $this->offset += 4;
299 5
300 1
        $num = \unpack('i', \strrev($num));
301
302
        return $num[1];
303 4
    }
304 4
305 View Code Duplication
    public function unpackInt64()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
306 4
    {
307
        if (!isset($this->buffer[$this->offset + 7])) {
308 4
            throw InsufficientDataException::fromOffset($this->buffer, $this->offset, 8);
309
        }
310
311 5
        $num = \substr($this->buffer, $this->offset, 8);
312
        $this->offset += 8;
313 5
314 1
        $set = \unpack('N2', $num);
315
316
        return $set[1] << 32 | $set[2];
317 4
    }
318 4
319 View Code Duplication
    public function unpackFloat32()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
320 4
    {
321
        if (!isset($this->buffer[$this->offset + 3])) {
322 4
            throw InsufficientDataException::fromOffset($this->buffer, $this->offset, 4);
323
        }
324
325 3
        $num = \substr($this->buffer, $this->offset, 4);
326
        $this->offset += 4;
327 3
328 1
        $num = \unpack('f', \strrev($num));
329
330
        return $num[1];
331 2
    }
332 2
333 View Code Duplication
    public function unpackFloat64()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
334 2
    {
335
        if (!isset($this->buffer[$this->offset + 7])) {
336 2
            throw InsufficientDataException::fromOffset($this->buffer, $this->offset, 8);
337
        }
338
339 4
        $num = \substr($this->buffer, $this->offset, 8);
340
        $this->offset += 8;
341 4
342 1
        $num = \unpack('d', \strrev($num));
343
344
        return $num[1];
345 3
    }
346 3
347
    public function unpackStr($length)
348 3
    {
349
        if (!isset($this->buffer[$this->offset + $length - 1])) {
350 3
            throw InsufficientDataException::fromOffset($this->buffer, $this->offset, $length);
351
        }
352
353 25
        $str = \substr($this->buffer, $this->offset, $length);
354
        $this->offset += $length;
355 25
356 1
        return $str;
357
    }
358
359 25 View Code Duplication
    public function unpackArrayLength()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
360 25
    {
361
        $c = $this->unpackUint8();
362 25
363
        if ($c >= 0x90 && $c <= 0x9f) {
364
            return $c & 0xf;
365 8
        }
366
        if (0xdc === $c) {
367 8
            return $this->unpackUint16();
368 8
        }
369 8
        if (0xdd === $c) {
370
            return $this->unpackUint32();
371
        }
372 8
373
        throw new UnpackingFailedException(\sprintf('Unknown array header code: 0x%x.', $c));
374
    }
375 13
376
    public function unpackArray($size)
377 13
    {
378 13
        $array = [];
379 13
        while ($size--) {
380
            $array[] = $this->unpack();
381
        }
382 13
383
        return $array;
384
    }
385 15
386 View Code Duplication
    public function unpackMapLength()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
387 15
    {
388 5
        $c = $this->unpackUint8();
389
390
        if ($c >= 0x80 && $c <= 0x8f) {
391 10
            return $c & 0xf;
392
        }
393 10
        if (0xde === $c) {
394 1
            return $this->unpackUint16();
395
        }
396
        if (0xdf === $c) {
397 9
            return $this->unpackUint32();
398 9
        }
399
400 9
        throw new UnpackingFailedException(\sprintf('Unknown map header code: 0x%x.', $c));
401
    }
402
403 5
    public function unpackMap($size)
404
    {
405 5
        $map = [];
406 3
        while ($size--) {
407
            $map[$this->unpack()] = $this->unpack();
408 2
        }
409 1
410
        return $map;
411
    }
412 1
413
    public function unpackExt($length)
414
    {
415
        if (!isset($this->buffer[$this->offset + $length - 1])) {
416
            throw InsufficientDataException::fromOffset($this->buffer, $this->offset, $length);
417
        }
418
419
        // int8
420
        $num = \ord($this->buffer[$this->offset]);
421
        ++$this->offset;
422
423
        $type = $num > 0x7f ? $num - 256 : $num;
424
425
        if (isset($this->transformers[$type])) {
426
            return $this->transformers[$type]->unpack($this, $length);
427
        }
428
429
        $data = \substr($this->buffer, $this->offset, $length);
430
        $this->offset += $length;
431
432
        return new Ext($type, $data);
433
    }
434
435
    private function handleIntOverflow($value)
436
    {
437
        if ($this->isBigIntAsStr) {
438
            return \sprintf('%u', $value);
439
        }
440
        if ($this->isBigIntAsGmp) {
441
            return \gmp_init(\sprintf('%u', $value));
442
        }
443
444
        throw new IntegerOverflowException($value);
445
    }
446
}
447