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 ( c84b74...6c770b )
by Charlotte
03:58
created

src/Commands/PromiseCommand.php (1 issue)

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
 * Abstract command which has a promise.
14
 * @internal
15
 */
16
abstract class PromiseCommand implements CommandInterface {
17
    use \Evenement\EventEmitterTrait;
18
    
19
    /**
20
     * @var \Plasma\DriverInterface
21
     */
22
    protected $driver;
23
    
24
    /**
25
     * @var \React\Promise\Deferred
26
     */
27
    protected $deferred;
28
    
29
    /**
30
     * @var mixed
31
     */
32
    protected $resolveValue;
33
    
34
    /**
35
     * @var bool
36
     */
37
    protected $finished = false;
38
    
39
    /**
40
     * Constructor.
41
     * @param \Plasma\DriverInterface  $driver
42
     */
43 6
    function __construct(\Plasma\DriverInterface $driver) {
44 6
        $this->driver = $driver;
45 6
        $this->deferred = new \React\Promise\Deferred();
46
        
47
        $this->once('error', function (\Throwable $error) {
48
            $this->deferred->reject($error);
49 6
        });
50
        
51
        $this->once('end', function () {
52
            // Let the event loop read the stream buffer before resolving
53
            $this->driver->getLoop()->futureTick(function () {
54 3
                $this->deferred->resolve($this->resolveValue);
55 3
                $this->resolveValue = null;
56 3
            });
57 6
        });
58 6
    }
59
    
60
    /**
61
     * Get the encoded message for writing to the database connection.
62
     * @return string
63
     */
64
    abstract function getEncodedMessage(): string;
65
    
66
    /**
67
     * Get the promise.
68
     * @var \React\Promise\PromiseInterface
69
     */
70 6
    function getPromise(): \React\Promise\PromiseInterface {
71 6
        return $this->deferred->promise();
72
    }
73
    
74
    /**
75
     * Sets the parser state, if necessary. If not, return `-1`.
76
     * @return int
77
     */
78 6
    function setParserState(): int {
79 6
        return -1;
80
    }
81
    
82
    /**
83
     * Sets the command as completed. This state gets reported back to the user.
84
     * @return void
85
     */
86 3
    function onComplete(): void {
87 3
        $this->finished = true;
88 3
        $this->emit('end');
89 3
    }
90
    
91
    /**
92
     * Sets the command as errored. This state gets reported back to the user.
93
     * @param \Throwable  $throwable
94
     * @return void
95
     */
96
    function onError(\Throwable $throwable): void {
97
        $this->finished = true;
98
        
99
        if($this->resolveValue !== null) {
100
            // Let the event loop read the stream buffer and resolve the promise before emitting
101
            // Works around a race condition, where the promise resolves
102
            // after receiving an error response packet
103
            // and therefore the user misses the error event
104
            $this->driver->getLoop()->futureTick(function () use (&$throwable) {
105
                $this->emit('error', array($throwable));
106
            });
107
            
108
            return;
109
        }
110
        
111
        $this->emit('error', array($throwable));
112
    }
113
    
114
    /**
115
     * Sends the next received value into the command.
116
     * @param mixed  $value
117
     * @return void
118
     */
119
    abstract function onNext($value): void;
120
    
121
    /**
122
     * Whether the command has finished.
123
     * @return bool
124
     */
125 5
    function hasFinished(): bool {
126 5
        return $this->finished;
127
    }
128
    
129
    /**
130
     * Whether this command sets the connection as busy.
131
     * @return bool
132
     */
133 6
    function waitForCompletion(): bool {
134 6
        return true;
135
    }
136
    
137
    /**
138
     * Whether the sequence ID should be resetted.
139
     * @return bool
140
     */
141
    abstract function resetSequence(): bool;
142
    
143
    /**
144
     * Handles the column definitions of query commands on next caller.
145
     * @param \Plasma\BinaryBuffer                  $buffer
146
     * @param \Plasma\Drivers\MySQL\ProtocolParser  $parser
147
     * @return \Plasma\ColumnDefinitionInterface|null
148
     */
149 5
    function handleQueryOnNextCallerColumns(\Plasma\BinaryBuffer $buffer, \Plasma\Drivers\MySQL\ProtocolParser $parser): ?\Plasma\ColumnDefinitionInterface {
150 5
        if($this->fieldsCount === null) {
151 3
            $fieldCount = $buffer->readIntLength();
152
            
153 3
            if($fieldCount === 0x00) {
154
                $this->driver->getLoop()->futureTick(function () use (&$buffer, &$parser) {
155
                    $msg = new \Plasma\Drivers\MySQL\Messages\OkResponseMessage($parser);
156
                    $parser->handleMessage($buffer, $msg);
157
                });
158
                
159
                return null;
160 3
            } elseif($fieldCount === 0xFB) {
161
                // Handle it on future tick, so we can cleanly finish the buffer of this call
162
                $this->driver->getLoop()->futureTick(function () use (&$buffer, &$parser) {
163
                    $msg = new \Plasma\Drivers\MySQL\Messages\LocalInFileRequestMessage($parser);
164
                    $parser->handleMessage($buffer, $msg);
165
                });
166
                
167
                return null;
168 3
            } elseif($fieldCount === 0xFF) {
169
                $this->driver->getLoop()->futureTick(function () use (&$buffer, &$parser) {
170
                    $msg = new \Plasma\Drivers\MySQL\Messages\ErrResponseMessage($parser);
171
                    $parser->handleMessage($buffer, $msg);
172
                });
173
                
174
                return null;
175
            }
176
            
177 3
            $this->fieldsCount = $fieldCount;
178 3
            return null;
179
        }
180
        
181 5
        return static::parseColumnDefinition($buffer);
182
    }
183
    
184
    /**
185
     * Parses the column definition.
186
     * @param \Plasma\BinaryBuffer  $buffer
187
     * @return \Plasma\ColumnDefinitionInterface
188
     */
189 5
    static function parseColumnDefinition(\Plasma\BinaryBuffer $buffer): \Plasma\ColumnDefinitionInterface {
190 5
        $buffer->readStringLength(); // catalog - always "def"
191 5
        $database = $buffer->readStringLength();
192
        
193 5
        $table = $buffer->readStringLength();
194 5
        $buffer->readStringLength(); // orgTable
195
        
196 5
        $name = $buffer->readStringLength();
197 5
        $buffer->readStringLength(); // orgName
198
        
199 5
        $buffer->read(1); // 0x0C
200
        
201 5
        $charset = $buffer->readInt2();
202 5
        $length = $buffer->readInt4();
203 5
        $type = $buffer->readInt1();
204 5
        $flags = $buffer->readInt2();
205 5
        $decimals = $buffer->readInt1();
206
        
207 5
        $buffer->read(2); // fillers
208
        
209
        /*if($this instanceof COM_FIELD_LIST) {
210
            $buffer->readStringLength();
211
        }*/
212
        
213 5
        $charset = \Plasma\Drivers\MySQL\CharacterSetFlags::CHARSET_MAP[$charset] ?? 'Unknown charset "'.$charset.'"';
214 5
        $type = \Plasma\Drivers\MySQL\FieldFlags::TYPE_MAP[$type] ?? 'Unknown type "'.$type.'"';
215 5
        $nullable = (($flags & \Plasma\Drivers\MySQL\FieldFlags::NOT_NULL_FLAG) === 0);
216
        
217 5
        return (new \Plasma\ColumnDefinition($database, $table, $name, $type, $charset, $length, $nullable, $flags, $decimals));
218
    }
219
    
220
    /**
221
     * Parses the text resultset row and returns the row.
222
     * @param \Plasma\ColumnDefinitionInterface  $column
223
     * @param \Plasma\BinaryBuffer               $buffer
224
     * @return array
225
     */
226
    protected function parseResultsetRow(\Plasma\BinaryBuffer $buffer): array {
227
        $row = array();
228
        
229
        /** @var \Plasma\ColumnDefinitionInterface  $column */
230
        foreach($this->fields as $column) {
231
            $rawValue = $buffer->readStringLength();
232
            
233
            try {
234
                $value = \Plasma\Types\TypeExtensionsManager::getManager('driver-mysql')
235
                    ->decodeType($column->getType(), $rawValue)
236
                    ->getValue();
237
            } catch (\Plasma\Exception $e) {
238
                $value = $this->stdDecodeValue($column, $rawValue);
239
            }
240
            
241
            $row[$column->getName()] = $value;
242
        }
243
        
244
        return $row;
245
    }
246
    
247
    /**
248
     * Standard decode value, if type extensions failed.
249
     * @param \Plasma\ColumnDefinitionInterface  $column
250
     * @param string                             $param
251
     * @return mixed
252
     * @throws \Plasma\Exception
253
     */
254
    protected function stdDecodeValue(\Plasma\ColumnDefinitionInterface $column, string $param) {
255
        $flags = $column->getFlags();
256
        
257
        if($param !== null && ($flags & \Plasma\Drivers\MySQL\FieldFlags::ZEROFILL_FLAG) === 0) {
258
            switch($column->getType()) {
259
                case \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_LONG:
260
                    if(($flags & \Plasma\Drivers\MySQL\FieldFlags::UNSIGNED_FLAG) === 0 || \PHP_INT_SIZE > 4) {
261
                        $param = (int) $param;
262
                    }
263
                break;
264
                case \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_LONGLONG:
265
                    if(($flags & \Plasma\Drivers\MySQL\FieldFlags::UNSIGNED_FLAG) === 0 && \PHP_INT_SIZE > 4) {
266
                        $param = (int) $param;
267
                    }
268
                break;
269
                case \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_TINY:
270
                case \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_SHORT:
271
                case \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_INT24:
272
                case \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_TIMESTAMP:
1 ignored issue
show
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
273
                    $param = (int) $param;
274
                break;
275
                case \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_FLOAT:
276
                case \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_DOUBLE:
277
                    $param = (float) $param;
278
                break;
279
                // Other types are taken as string
280
            }
281
        }
282
        
283
        return $param;
284
    }
285
}
286