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 ( dbd29a...6908d2 )
by Charlotte
03:46
created

src/Commands/StatementExecuteCommand.php (40 issues)

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\Commands;
11
12
/**
13
 * Statement Execute command.
14
 * @internal
15
 */
16
class StatementExecuteCommand extends QueryCommand {
17
    /**
18
     * The identifier for this command.
19
     * @var int
20
     * @source
21
     */
22
    const COMMAND_ID = 0x17;
23
    
24
    /**
25
     * @var mixed
26
     */
27
    protected $id;
28
    
29
    /**
30
     * @var array
31
     */
32
    protected $params;
33
    
34
    /**
35
     * Constructor.
36
     * @param \Plasma\DriverInterface  $driver
37
     * @param mixed                    $id
38
     * @param string                   $query
39
     * @param array                    $params
40
     */
41 3
    function __construct(\Plasma\DriverInterface $driver, $id, string $query, array $params) {
42 3
        parent::__construct($driver, $query);
43
        
44 3
        $this->id = $id;
45 3
        $this->params = $params;
46 3
    }
47
    
48
    /**
49
     * Get the encoded message for writing to the database connection.
50
     * @return string
51
     */
52 2
    function getEncodedMessage(): string {
53 2
        $packet = \chr(static::COMMAND_ID);
54 2
        $packet .= \Plasma\BinaryBuffer::writeInt4($this->id);
55
        
56 2
        $packet .= "\x00"; // Cursor type flag
57 2
        $packet .= \Plasma\BinaryBuffer::writeInt4(1); // Iteration count is always 1
58
        
59 2
        $bitmapOffset = \strlen($packet);
60 2
        $packet .= \str_repeat("\x00", ((\count($this->params) + 7) >> 3));
61
        
62 2
        $bound = 0;
63
        
64 2
        $types = '';
65 2
        $values = '';
66
        
67 2
        foreach($this->params as $id => $param) {
68 2
            if($param === null) {
69
                $offset = $bitmapOffset + ($id >> 3);
70
                $packet[$offset] = $packet[$offset] | \chr((1 << ($id % 8)));
71
            } else {
72 2
                $bound = 1;
73
            }
74
            
75
            try {
76 2
                $manager = \Plasma\Types\TypeExtensionsManager::getManager('driver-mysql');
77
                $encode = $manager->encodeType($param);
78
                
79
                $unsigned = $encode->isUnsigned();
80
                $type = $encode->getSQLType();
81
                $value = $encode->getValue();
82 2
            } catch (\Plasma\Exception $e) {
83 2
                [ $unsigned, $type, $value ] = $this->stdEncodeValue($param);
84
            }
85
            
86 2
            $types .= \chr($type).($unsigned ? "\x80" : "\x00");
87 2
            $values .= $value;
88
        }
89
        
90 2
        $packet .= \chr($bound);
91
        
92 2
        if($bound) {
93 2
            $packet .= $types;
94 2
            $packet .= $values;
95
        }
96
        
97 2
        return $packet;
98
    }
99
    
100
    /**
101
     * Whether the sequence ID should be resetted.
102
     * @return bool
103
     */
104 2
    function resetSequence(): bool {
105 2
        return true;
106
    }
107
    
108
    /**
109
     * Parses the binary resultset row and returns the row.
110
     * @param \Plasma\BinaryBuffer  $buffer
111
     * @return array
112
     */
113 2
    protected function parseResultsetRow(\Plasma\BinaryBuffer $buffer): array {
114 2
        $buffer->read(1); // remove packet header
115
        
116 2
        $nullRow = array();
117 2
        $i = 0;
118
        
119
        /** @var \Plasma\ColumnDefinitionInterface  $column */
120 2
        foreach($this->fields as $column) { // Handle NULL-bitmap
121 2
            if((\ord($buffer[(($i + 2) >> 3)]) & (1 << (($i + 2) % 8))) !== 0) {
122 2
                $nullRow[$column->getName()] = null;
123
            }
124
            
125 2
            $i++;
126
        }
127
        
128 2
        $buffer->read(((\count($this->fields) + 9) >> 3)); // Remove NULL-bitmap
129 2
        $row = array();
130
        
131
        /** @var \Plasma\ColumnDefinitionInterface  $column */
132 2
        foreach($this->fields as $column) {
133 2
            if(\array_key_exists($column->getName(), $nullRow)) {
134 2
                $row[$column->getName()] = null;
135 2
                continue;
136
            }
137
            
138 2
            $value = $this->stdDecodeBinaryValue($column, $buffer);
139 2
            $parsedValue = $column->parseValue($value);
140
            
141 2
            if($value !== $parsedValue) {
142
                $value = $parsedValue;
143
            }
144
            
145 2
            $row[$column->getName()] = $value;
146
        }
147
        
148 2
        return $row;
149
    }
150
    
151
    /**
152
     * Standard encode value, if type extensions failed.
153
     * @param mixed                              $param
154
     * @return array
155
     * @throws \Plasma\Exception
156
     */
157 2
    protected function stdEncodeValue($param): array {
158 2
        $unsigned = false;
159
        
160 2
        switch(\gettype($param)) {
161
            case 'boolean':
162
                $type = \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_TINY;
163
                $value = ($param ? "\x01" : "\0");
164
            break;
165
            case 'integer':
166
                if($param >= 0) {
167
                    $unsigned = true;
168
                }
169
                
170
                if($param >= 0 && $param < (1 << 15)) {
171
                    $type = \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_SHORT;
172
                    $value = \Plasma\BinaryBuffer::writeInt2($param);
173
                } elseif(\PHP_INT_SIZE === 4) {
174
                    $type = \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_LONG;
175
                    $value = \Plasma\BinaryBuffer::writeInt4($param);
176
                } else {
177
                    $type = \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_LONGLONG;
178
                    $value = \Plasma\BinaryBuffer::writeInt8($param);
179
                }
180
            break;
181
            case 'double':
182
                $type = \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_DOUBLE;
183
                $value = \Plasma\BinaryBuffer::writeDouble($param);
184
            break;
185
            case 'string':
186 2
                $type = \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_LONG_BLOB;
187 2
                $value = \Plasma\BinaryBuffer::writeStringLength($param);
188 2
            break;
189
            case 'NULL':
190
                $type = \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_NULL;
191
                $value = '';
192
            break;
193
            default:
194
                throw new \Plasma\Exception('Unexpected type for binding parameter: '.\gettype($param));
195
            break;
196
        }
197
        
198 2
        return array($unsigned, $type, $value);
199
    }
200
    
201
    /**
202
     * Standard decode value, if type extensions failed.
203
     * @param \Plasma\ColumnDefinitionInterface  $column
204
     * @param \Plasma\BinaryBuffer               $buffer
205
     * @return mixed
206
     * @throws \Plasma\Exception
207
     */
208 2
    protected function stdDecodeBinaryValue(\Plasma\ColumnDefinitionInterface $column, \Plasma\BinaryBuffer $buffer) {
209 2
        $flags = $column->getFlags();
210 2
        $type = $column->getType();
211
        
212
        switch(true) {
213 2
            case ($type === 'STRING'):
1 ignored issue
show
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
214 2
            case ($type === 'VARCHAR'):
1 ignored issue
show
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
215 2
            case ($type === 'VARSTRING'):
1 ignored issue
show
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
216
            case ($type === 'ENUM'):
1 ignored issue
show
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
217
            case ($type === 'SET'):
1 ignored issue
show
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
218
            case ($type === 'LONGBLOB'):
1 ignored issue
show
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
219
            case ($type === 'MEDIUMBLOB'):
1 ignored issue
show
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
220
            case ($type === 'BLOB'):
1 ignored issue
show
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
221
            case ($type === 'TINY_BLOB'):
1 ignored issue
show
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
222
            case ($type === 'GEOMETRY'):
1 ignored issue
show
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
223
            case ($type === 'BIT'):
1 ignored issue
show
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
224
            case ($type === 'DECIMAL'):
1 ignored issue
show
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
225
            case ($type === 'NEWDECIMAL'):
1 ignored issue
show
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
226
            case ($type === 'JSON'):
1 ignored issue
show
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
227 2
                $value = $buffer->readStringLength();
228 2
            break;
1 ignored issue
show
Case breaking statement indented incorrectly; expected 16 spaces, found 12
Loading history...
229
            case ($type === 'TINY'):
1 ignored issue
show
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
230
                $value = $buffer->readInt1();
231
                $value = $this->zeroFillInts($column, $value);
232
            break;
1 ignored issue
show
Case breaking statement indented incorrectly; expected 16 spaces, found 12
Loading history...
233
            case ($type === 'SHORT'):
1 ignored issue
show
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
234
            case ($type === 'YEAR'):
1 ignored issue
show
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
235
                $value = $buffer->readInt2();
236
                $value = $this->zeroFillInts($column, $value);
237
            break;
1 ignored issue
show
Case breaking statement indented incorrectly; expected 16 spaces, found 12
Loading history...
238
            case ($type === 'INT24'):
1 ignored issue
show
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
239
            case ($type === 'LONG'):
1 ignored issue
show
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
240
                $value = $buffer->readInt4();
241
                
242
                if(($flags & \Plasma\Drivers\MySQL\FieldFlags::UNSIGNED_FLAG) !== 0 && \PHP_INT_SIZE <= 4) {
243
                    $value = \bcadd($value, '18446744073709551616');
244
                }
245
                
246
                $value = $this->zeroFillInts($column, $value);
247
            break;
1 ignored issue
show
Case breaking statement indented incorrectly; expected 16 spaces, found 12
Loading history...
248
            case ($type === 'LONGLONG'):
1 ignored issue
show
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
249
                $value = $buffer->readInt8();
250
                
251
                if(($flags & \Plasma\Drivers\MySQL\FieldFlags::UNSIGNED_FLAG) !== 0) {
252
                    $value = \bcadd($value, '18446744073709551616');
253
                } elseif(\PHP_INT_SIZE > 4) {
254
                    $value = (int) $value;
255
                }
256
                
257
                $value = $this->zeroFillInts($column, $value);
258
            break;
1 ignored issue
show
Case breaking statement indented incorrectly; expected 16 spaces, found 12
Loading history...
259
            case ($type === 'FLOAT'):
1 ignored issue
show
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
260
                $value = $buffer->readFloat();
261
            break;
1 ignored issue
show
Case breaking statement indented incorrectly; expected 16 spaces, found 12
Loading history...
262
            case ($type === 'DOUBLE'):
1 ignored issue
show
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
263
                $value = $buffer->readDouble();
264
            break;
1 ignored issue
show
Case breaking statement indented incorrectly; expected 16 spaces, found 12
Loading history...
265
            case ($type === 'DATE'):
1 ignored issue
show
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
266
            case ($type === 'NEWDATE'):
1 ignored issue
show
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
267
                $length = $buffer->readIntLength();
268
                if($length > 0) {
269
                    $year = $buffer->readInt2();
270
                    $month = $buffer->readInt1();
271
                    $day = $buffer->readInt1();
272
                    
273
                    $value = \sprintf('%04d-%02d-%02d', $year, $month, $day);
274
                } else {
275
                    $value = '0000-00-00';
276
                }
1 ignored issue
show
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
277
            break;
1 ignored issue
show
Case breaking statement indented incorrectly; expected 16 spaces, found 12
Loading history...
278
            case ($type === 'DATETIME'):
1 ignored issue
show
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
279
            case ($type === 'TIMESTAMP'):
1 ignored issue
show
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
280
                $length = $buffer->readIntLength();
281
                if($length > 0) {
282
                    $year = $buffer->readInt2();
283
                    $month = $buffer->readInt1();
284
                    $day = $buffer->readInt1();
285
                    
286
                    if($length > 4) {
287
                        $hour = $buffer->readInt1();
288
                        $min = $buffer->readInt1();
289
                        $sec = $buffer->readInt1();
290
                    } else {
291
                        $hour = 0;
292
                        $min = 0;
293
                        $sec = 0;
294
                    }
295
                    
296
                    if($length > 7) {
297
                        $microI = $buffer->readInt4();
298
                    } else {
299
                        $microI = 0;
300
                    }
301
                    
302
                    $micro = \str_pad($microI, 6, '0', \STR_PAD_LEFT);
303
                    $micro = \substr($micro, 0, 3).' '.\substr($micro, 3);
304
                    
305
                    $value = \sprintf('%04d-%02d-%02d %02d:%02d:%02d'.($microI > 0 ? '.%s' : ''), $year, $month, $day, $hour, $min, $sec, $micro);
306
                    
307
                    if($type === 'TIMESTAMP') {
308
                        $value = \DateTime::createFromFormat('Y-m-d H:i:s'.($microI > 0 ? '.u' : ''), $value)->getTimestamp();
309
                    }
310
                } else {
311
                    $value = '0000-00-00 00:00:00.000 000';
312
                }
313
            break;
1 ignored issue
show
Case breaking statement indented incorrectly; expected 16 spaces, found 12
Loading history...
314
            case ($type === 'TIME'):
1 ignored issue
show
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
315
                $length = $buffer->readIntLength();
316
                if($length > 1) {
317
                    $sign = $buffer->readInt1();
318
                    $days = $buffer->readInt4();
319
                    
320
                    if($sign === 1) {
321
                        $days *= -1;
322
                    }
323
                    
324
                    $hour = $buffer->readInt1();
325
                    $min = $buffer->readInt1();
326
                    $sec = $buffer->readInt1();
327
                    
328
                    if($length > 8) {
329
                        $microI = $buffer->readInt4();
330
                    } else {
331
                        $microI = 0;
332
                    }
1 ignored issue
show
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
333
                    
334
                    $micro = \str_pad($microI, 6, '0', \STR_PAD_LEFT);
335
                    $micro = \substr($micro, 0, 3).' '.\substr($micro, 3);
336
                    
337
                    $value = \sprintf('%dd %02d:%02d:%02d'.($microI > 0 ? '.%s' : ''), $days, $hour, $min, $sec, $micro);
338
                } else {
339
                    $value = '0d 00:00:00';
340
                }
1 ignored issue
show
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
341
            break;
1 ignored issue
show
Case breaking statement indented incorrectly; expected 16 spaces, found 12
Loading history...
342
            default:
343
                throw new \InvalidArgumentException('Unknown column type (flags: '.$flags.', type: '.$column->getType().')');
344
            break;
345
        }
346
        
347 2
        return $value;
348
    }
349
    
350
    /**
351
     * @param \Plasma\ColumnDefinitionInterface  $column
352
     * @param string|int                         $value
353
     * @return string|int
354
     */
355
    protected function zeroFillInts(\Plasma\ColumnDefinitionInterface $column, $value) {
356
        $flags = $column->getFlags();
357
        
358
        if(($flags & \Plasma\Drivers\MySQL\FieldFlags::ZEROFILL_FLAG) !== 0) {
359
            $value = \str_pad(((string) $value), $column->getLength(), '0', \STR_PAD_LEFT);
360
        }
361
        
362
        return $value;
363
    }
364
}
365