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
Branch master (b4dd81)
by Charlotte
04:21
created

BinaryProtocolValues::encode()   B

Complexity

Conditions 11
Paths 12

Size

Total Lines 42
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 59.1037

Importance

Changes 0
Metric Value
cc 11
eloc 35
nc 12
nop 1
dl 0
loc 42
ccs 9
cts 34
cp 0.2647
crap 59.1037
rs 7.3166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Plasma Driver MySQL component
4
 * Copyright 2018-2019 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;
11
12
/**
13
 * Binary protocol rowset values decoder and encoder.
14
 * @internal
15
 */
16
class BinaryProtocolValues {
17
    /**
18
     * Standard encode value, if type extensions failed.
19
     * @param mixed  $param
20
     * @return array
21
     * @throws \Plasma\Exception
22
     */
23 2
    static function encode($param): array {
24 2
        $unsigned = false;
25
        
26 2
        switch(\gettype($param)) {
27 2
            case 'boolean':
28
                $type = \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_TINY;
29
                $value = ($param ? "\x01" : "\0");
30
            break;
31 2
            case 'integer':
32
                if($param >= 0) {
33
                    $unsigned = true;
34
                }
35
                
36
                if($param >= 0 && $param < (1 << 15)) {
37
                    $type = \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_SHORT;
38
                    $value = \Plasma\BinaryBuffer::writeInt2($param);
39
                } elseif(\PHP_INT_SIZE === 4) {
40
                    $type = \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_LONG;
41
                    $value = \Plasma\BinaryBuffer::writeInt4($param);
42
                } else {
43
                    $type = \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_LONGLONG;
44
                    $value = \Plasma\BinaryBuffer::writeInt8($param);
45
                }
46
            break;
47 2
            case 'double':
48
                $type = \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_DOUBLE;
49
                $value = \Plasma\BinaryBuffer::writeDouble($param);
50
            break;
51 2
            case 'string':
52
                $type = \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_LONG_BLOB;
53
                $value = \Plasma\BinaryBuffer::writeStringLength($param);
54
            break;
55 2
            case 'NULL':
56
                $type = \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_NULL;
57
                $value = '';
58
            break;
59
            default:
60 2
                throw new \Plasma\Exception('Unexpected type for binding parameter: '.\gettype($param));
61
            break;
62
        }
63
        
64
        return array($unsigned, $type, $value);
65
    }
66
    
67
    /**
68
     * Standard decode value, if type extensions failed.
69
     * @param \Plasma\ColumnDefinitionInterface  $column
70
     * @param \Plasma\BinaryBuffer               $buffer
71
     * @return mixed
72
     * @throws \Plasma\Exception
73
     */
74
    static function decode(\Plasma\ColumnDefinitionInterface $column, \Plasma\BinaryBuffer $buffer) {
75
        $flags = $column->getFlags();
76
        $type = $column->getType();
77
        
78
        switch(true) {
79
            case static::isTypeString($type):
80
                $value = $buffer->readStringLength();
81
            break;
82
            case ($type === 'TINY'):
83
                $value = $buffer->readInt1();
84
                $value = static::zeroFillInts($column, $value);
85
            break;
86
            case static::isTypeShortOrYear($type):
87
                $value = $buffer->readInt2();
88
                $value = static::zeroFillInts($column, $value);
89
            break;
90
            case static::isTypeInt24orLong($type):
91
                $value = $buffer->readInt4();
92
                
93
                if($column->isUnsigned() && \PHP_INT_SIZE <= 4) {
94
                    $value = \bcadd($value, '18446744073709551616');
95
                }
96
                
97
                $value = static::zeroFillInts($column, $value);
98
            break;
99
            case ($type === 'LONGLONG'):
100
                $value = $buffer->readInt8();
101
                
102
                if($column->isUnsigned()) {
103
                    $value = \bcadd($value, '18446744073709551616');
104
                } elseif(\PHP_INT_SIZE > 4) {
105
                    $value = (int) $value;
106
                }
107
                
108
                $value = static::zeroFillInts($column, $value);
109
            break;
110
            case ($type === 'FLOAT'):
111
                $value = $buffer->readFloat();
112
            break;
113
            case ($type === 'DOUBLE'):
114
                $value = $buffer->readDouble();
115
            break;
116
            case static::isTypeDate($type):
117
                $length = $buffer->readIntLength();
118
                if($length > 0) {
119
                    $year = $buffer->readInt2();
120
                    $month = $buffer->readInt1();
121
                    $day = $buffer->readInt1();
122
                    
123
                    $value = \sprintf('%04d-%02d-%02d', $year, $month, $day);
124
                } else {
125
                    $value = '0000-00-00';
126
                }
127
            break;
128
            case static::isTypeDateTime($type):
129
                $value = static::parseDateTime($type, $buffer);
130
            break;
131
            case ($type === 'TIME'):
132
                $value = static::parseTime($buffer);
133
            break;
134
            default:
135
                throw new \Plasma\Exception('Unknown column type (flags: '.$flags.', type: '.$type.')');
136
            break;
137
        }
138
        
139
        return $value;
140
    }
141
    
142
    /**
143
     * @param string  $type
144
     * @return bool
145
     */
146
    static function isTypeString(string $type): bool {
147
        $types = array(
148
            'STRING', 'VARCHAR', 'VARSTRING', 'ENUM', 'SET', 'LONGBLOB',
149
            'MEDIUMBLOB', 'BLOB', 'TINYBLOB', 'GEMOTERY', 'BIT', 'DECIMAL',
150
            'NEWDECIMAL', 'JSON'
151
        );
152
        
153
        return \in_array($type, $types, true);
154
    }
155
    
156
    /**
157
     * @param string  $type
158
     * @return bool
159
     */
160
    static function isTypeShortOrYear(string $type): bool {
161
        $types = array('SHORT', 'YEAR');
162
        return \in_array($type, $types, true);
163
    }
164
    
165
    /**
166
     * @param string  $type
167
     * @return bool
168
     */
169
    static function isTypeInt24orLong(string $type): bool {
170
        $types = array('INT24', 'LONG');
171
        return \in_array($type, $types, true);
172
    }
173
    
174
    /**
175
     * @param string  $type
176
     * @return bool
177
     */
178
    static function isTypeDate(string $type): bool {
179
        $types = array('DATE', 'NEWDATE');
180
        return \in_array($type, $types, true);
181
    }
182
    
183
    /**
184
     * @param string  $type
185
     * @return bool
186
     */
187
    static function isTypeDateTime(string $type): bool {
188
        $types = array('DATETIME', 'TIMESTAMP');
189
        return \in_array($type, $types, true);
190
    }
191
    
192
    /**
193
     * Parses a DATETIME or TIMESTAMP value.
194
     * @param string                $type
195
     * @param \Plasma\BinaryBuffer  $buffer
196
     * @return mixed
197
     */
198
    static function parseDateTime(string $type, \Plasma\BinaryBuffer $buffer) {
199
        $length = $buffer->readIntLength();
200
        if($length > 0) {
201
            $year = $buffer->readInt2();
202
            $month = $buffer->readInt1();
203
            $day = $buffer->readInt1();
204
            
205
            if($length > 4) {
206
                $hour = $buffer->readInt1();
207
                $min = $buffer->readInt1();
208
                $sec = $buffer->readInt1();
209
            } else {
210
                $hour = 0;
211
                $min = 0;
212
                $sec = 0;
213
            }
214
            
215
            if($length > 7) {
216
                $microI = $buffer->readInt4();
217
            } else {
218
                $microI = 0;
219
            }
220
            
221
            $micro = \str_pad($microI, 6, '0', \STR_PAD_LEFT);
222
            $micro = \substr($micro, 0, 3).' '.\substr($micro, 3);
223
            
224
            $value = \sprintf('%04d-%02d-%02d %02d:%02d:%02d'.($microI > 0 ? '.%s' : ''), $year, $month, $day, $hour, $min, $sec, $micro);
225
            
226
            if($type === 'TIMESTAMP') {
227
                $value = \DateTime::createFromFormat('Y-m-d H:i:s'.($microI > 0 ? '.u' : ''), $value)->getTimestamp();
228
            }
229
        } else {
230
            $value = '0000-00-00 00:00:00.000 000';
231
        }
232
        
233
        return $value;
234
    }
235
    
236
    /**
237
     * Parses a TIME value.
238
     * @param \Plasma\BinaryBuffer  $buffer
239
     * @return mixed
240
     */
241
    static function parseTime(\Plasma\BinaryBuffer $buffer) {
242
        $length = $buffer->readIntLength();
243
        if($length > 1) {
244
            $sign = $buffer->readInt1();
245
            $days = $buffer->readInt4();
246
            
247
            if($sign === 1) {
248
                $days *= -1;
249
            }
250
            
251
            $hour = $buffer->readInt1();
252
            $min = $buffer->readInt1();
253
            $sec = $buffer->readInt1();
254
            
255
            if($length > 8) {
256
                $microI = $buffer->readInt4();
257
            } else {
258
                $microI = 0;
259
            }
260
            
261
            $micro = \str_pad($microI, 6, '0', \STR_PAD_LEFT);
262
            $micro = \substr($micro, 0, 3).' '.\substr($micro, 3);
263
            
264
            $value = \sprintf('%dd %02d:%02d:%02d'.($microI > 0 ? '.%s' : ''), $days, $hour, $min, $sec, $micro);
265
        } else {
266
            $value = '0d 00:00:00';
267
        }
268
        
269
        return $value;
270
    }
271
    
272
    /**
273
     * @param \Plasma\ColumnDefinitionInterface  $column
274
     * @param string|int                         $value
275
     * @return string|int
276
     */
277
    static function zeroFillInts(\Plasma\ColumnDefinitionInterface $column, $value) {
278
        if($column->isZerofilled()) {
279
            $value = \str_pad(((string) $value), $column->getLength(), '0', \STR_PAD_LEFT);
280
        }
281
        
282
        return $value;
283
    }
284
}
285