GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Test Failed
Push — master ( 18c025...2702d5 )
by Charlotte
03:32
created

MessageUtility::writeInt8()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 14
ccs 0
cts 9
cp 0
crap 12
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Plasma Driver MySQL component
4
 * Copyright 2018 PlasmaPHP, All Rights Reserved
5
 *
6
 * Website: https://github.com/PlasmaPHP
7
 * License: https://github.com/PlasmaPHP/driver-mysql/blob/master/LICENSE
8
*/
9
10
namespace Plasma\Drivers\MySQL\Messages;
11
12
/**
13
 * Utilities for messages.
14
 * @internal
15
 */
16
class MessageUtility {
17
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $buffer should have a doc-comment as per coding-style.
Loading history...
18
     * Parses a 1 byte / 8 bit integer (0 to 255).
19
     * @return int
20
     */
21 1
    static function readInt1(string &$buffer): int {
22 1
        return \ord(static::readBuffer($buffer, 1));
23
    }
24
    
25
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $buffer should have a doc-comment as per coding-style.
Loading history...
26
     * Parses a 2 byte / 16 bit integer (0 to 64 K / 0xFFFF).
27
     * @return int
28
     */
29
    static function readInt2(string &$buffer): int {
30
        return \unpack('v', static::readBuffer($buffer, 2))[1];
31
    }
32
    
33
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $buffer should have a doc-comment as per coding-style.
Loading history...
34
     * Parses a 3 byte / 24 bit integer (0 to 16 M / 0xFFFFFF).
35
     * @return int
36
     */
37 1
    static function readInt3(string &$buffer): int {
38 1
        return \unpack('V', static::readBuffer($buffer, 3)."\0")[1];
39
    }
40
    
41
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $buffer should have a doc-comment as per coding-style.
Loading history...
42
     * Parses a 4 byte / 32 bit integer (0 to 4 G / 0xFFFFFFFF).
43
     * @return int
44
     */
45
    static function readInt4(string &$buffer): int {
46
        return \unpack('V', static::readBuffer($buffer, 4))[1];
47
    }
48
    
49
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $buffer should have a doc-comment as per coding-style.
Loading history...
50
     * Parses a 8 byte / 64 bit integer (0 to 2^64-1).
51
     * @return int|string
52
     */
53
    static function readInt8(string &$buffer) {
54
        $strInt = static::readBuffer($buffer, 8);
55
        
56
        if(\PHP_INT_SIZE > 4) {
57
            return \unpack('P', $strInt)[1];
58
        }
59
        
60
        $result = \bcadd('0', \unpack('n', \substr($strInt, 0, 2)));
0 ignored issues
show
Bug introduced by
unpack('n', substr($strInt, 0, 2)) of type array is incompatible with the type string expected by parameter $right_operand of bcadd(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

60
        $result = \bcadd('0', /** @scrutinizer ignore-type */ \unpack('n', \substr($strInt, 0, 2)));
Loading history...
61
        $result = \bcmul($result, '65536');
62
        $result = \bcadd($result, \unpack('n', \substr($strInt, 2, 2)));
63
        $result = \bcmul($result, '65536');
64
        $result = \bcadd($result, \unpack('n', \substr($strInt, 4, 2)));
65
        $result = \bcmul($result, '65536');
66
        $result = \bcadd($result, \unpack('n', \substr($strInt, 6, 2)));
67
        
68
        // 9223372036854775808 is equal to (1 << 63)
69
        if(\bccomp($result, '9223372036854775808') !== -1) {
70
            $result = \bcsub($result, '18446744073709551616'); // $result -= (1 << 64)
71
        }
72
        
73
        return $result;
74
    }
75
    
76
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $buffer should have a doc-comment as per coding-style.
Loading history...
77
     * Parses length-encoded binary integer.
78
     * Returns the decoded integer 0 to 2^64 or `null` for special null int.
79
     * @return int|null
80
     */
81
    static function readIntLength(string &$buffer): ?int {
0 ignored issues
show
Unused Code introduced by
The parameter $buffer is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

81
    static function readIntLength(/** @scrutinizer ignore-unused */ string &$buffer): ?int {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
82
        $f = static::readInt1();
0 ignored issues
show
Bug introduced by
The call to Plasma\Drivers\MySQL\Mes...sageUtility::readInt1() has too few arguments starting with buffer. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

82
        /** @scrutinizer ignore-call */ 
83
        $f = static::readInt1();

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
83
        if($f <= 250) {
84
            return $f;
85
        }
86
        
87
        if($f === 251) {
88
            return null;
89
        }
90
        
91
        if($f === 252) {
92
            return static::readInt2();
0 ignored issues
show
Bug introduced by
The call to Plasma\Drivers\MySQL\Mes...sageUtility::readInt2() has too few arguments starting with buffer. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

92
            return static::/** @scrutinizer ignore-call */ readInt2();

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
93
        }
94
        
95
        if($f === 253) {
96
            return static::readInt3();
0 ignored issues
show
Bug introduced by
The call to Plasma\Drivers\MySQL\Mes...sageUtility::readInt3() has too few arguments starting with buffer. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

96
            return static::/** @scrutinizer ignore-call */ readInt3();

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
97
        }
98
        
99
        return static::readInt8();
0 ignored issues
show
Bug Best Practice introduced by
The expression return static::readInt8() could return the type string which is incompatible with the type-hinted return integer|null. Consider adding an additional type-check to rule them out.
Loading history...
Bug introduced by
The call to Plasma\Drivers\MySQL\Mes...sageUtility::readInt8() has too few arguments starting with buffer. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

99
        return static::/** @scrutinizer ignore-call */ readInt8();

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
100
    }
101
    
102
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $buffer should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $length should have a doc-comment as per coding-style.
Loading history...
103
     * Parses a length-encoded binary string. If length is null, `null` will be returned.
104
     * @return string|null
105
     */
106
    static function readStringLength(string &$buffer, ?int $length = null): ?string {
107
        $length = ($length !== null ? $length : static::readIntLength());
0 ignored issues
show
Bug introduced by
The call to Plasma\Drivers\MySQL\Mes...tility::readIntLength() has too few arguments starting with buffer. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

107
        $length = ($length !== null ? $length : static::/** @scrutinizer ignore-call */ readIntLength());

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
108
        if($length === null) {
109
            return null;
110
        }
111
        
112
        return static::readBuffer($buffer, $length);
0 ignored issues
show
Bug introduced by
It seems like $length can also be of type string; however, parameter $length of Plasma\Drivers\MySQL\Mes...geUtility::readBuffer() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

112
        return static::readBuffer($buffer, /** @scrutinizer ignore-type */ $length);
Loading history...
113
    }
114
    
115
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $buffer should have a doc-comment as per coding-style.
Loading history...
116
     * Reads NULL-terminated C string.
117
     * @return string
118
     * @throws \InvalidArgumentException
119
     */
120
    static function readStringNull(string &$buffer): string {
121
        $pos = \strpos($buffer, "\0");
122
        if($pos === false) {
123
            throw new \InvalidArgumentException('Missing NULL character');
124
        }
125
        
126
        return static::readBuffer($buffer, $pos);
127
    }
128
    
129
    /**
130
     * @param int $int
131
     * @return string
132
     */
133
    static function writeInt1(int $int): string {
134
        return \chr($int);
135
    }
136
    
137
    /**
138
     * @param int $int
139
     * @return string
140
     */
141
    static function writeInt2(int $int): string {
142
        return \pack('v', $int);
143
    }
144
    
145
    /**
146
     * @param int $int
147
     * @return string
148
     */
149
    static function writeInt3(int $int): string {
150
        return \substr(\pack('V', $int), 0, 3);
151
    }
152
    
153
    /**
154
     * @param int $int
155
     * @return string
156
     */
157
    static function writeInt4(int $int): string {
158
        return \pack('V', $int);
159
    }
160
    
161
    /**
162
     * @param string|int $int
163
     * @return string
164
     */
165
    static function writeInt8($int): string {
166
        if(\PHP_INT_SIZE > 4) {
167
            return \pack('P', ((int) $int));
168
        }
169
        
170
        if(\bccomp($int, '0') === -1) {
171
            // 18446744073709551616 is equal to (1 << 64)
172
            $int = \bcadd($int, '18446744073709551616');
173
        }
174
        
175
        return \pack('v', \bcmod(\bcdiv($int, '281474976710656'), '65536')).
176
            \pack('v', \bcmod(\bcdiv($int, '4294967296'), '65536')).
177
            \pack('v', \bcdiv($int, '65536'), '65536').
178
            \pack('v', \bcmod($int, '65536'));
179
    }
180
    
181
    /**
182
     * @param float  $float
183
     * @return string
184
     */
185
    static function writeFloat(float $float): string {
186
        return \pack('e', $float);
187
    }
188
    
189
    /**
190
     * Builds length-encoded binary string.
191
     * @param string|null $s
192
     * @return string
193
     */
194
    static function writeStringLength(?string $s): string {
195
        if($s === NULL) {
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
196
            // \xFB (251)
197
            return "\xFB";
198
        }
199
        
200
        $l = \strlen($s);
201
        if($l <= 250) {
202
            return static::writeInt1($l).$s;
203
        }
204
        
205
        if($l <= 0xFFFF) { // max 2^16: \xFC (252)
206
            return "\xFC".static::writeInt2($l).$s;
207
        }
208
        
209
        if($l <= 0xFFFFFF) { // max 2^24: \xFD (253)
210
            return "\xFD".static::writeInt3($l).$s;
211
        }
212
        
213
        return "\xFE".static::writeInt8($l).$s; // max 2^64: \xFE (254)
214
    }
215
    
216
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $buffer should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $length should have a doc-comment as per coding-style.
Loading history...
217
     * Reads a specified length from the buffer and discards the read part.
218
     * @return string
219
     */
220 1
    static function readBuffer(string &$buffer, int $length): string {
221 1
        $str = \substr($buffer, 0, $length);
222 1
        $buffer = \substr($buffer, ($length + 1));
223
        
224 1
        return $str;
225
    }
226
}
227