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 ( d13c9c...463153 )
by Charlotte
03:30
created

PromiseCommand::waitForCompletion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 2
cts 2
cp 1
crap 1
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\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 \React\Promise\Deferred
21
     */
22
    protected $deferred;
23
    
24
    /**
25
     * @var mixed
26
     */
27
    protected $resolveValue;
28
    
29
    /**
30
     * @var bool
31
     */
32
    protected $finished = false;
33
    
34
    /**
35
     * Constructor.
36
     */
37 2
    function __construct() {
38 2
        $this->deferred = new \React\Promise\Deferred();
39
        
40
        $this->once('error', function (\Throwable $error) {
41
            $this->deferred->reject($error);
42 2
        });
43
        
44
        $this->once('end', function () {
45 2
            $this->deferred->resolve($this->resolveValue);
46 2
        });
47 2
    }
48
    
49
    /**
50
     * Get the encoded message for writing to the database connection.
51
     * @return string
52
     */
53
    abstract function getEncodedMessage(): string;
54
    
55
    /**
56
     * Get the promise.
57
     * @var \React\Promise\PromiseInterface
58
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
59 2
    function getPromise(): \React\Promise\PromiseInterface {
60 2
        return $this->deferred->promise();
61
    }
62
    
63
    /**
64
     * Sets the parser state, if necessary. If not, return `-1`.
65
     * @return int
66
     */
67 2
    function setParserState(): int {
68 2
        return -1;
69
    }
70
    
71
    /**
72
     * Sets the command as completed. This state gets reported back to the user.
73
     * @return void
74
     */
75 2
    function onComplete(): void {
76 2
        $this->finished = true;
77 2
        $this->emit('end', array());
78 2
    }
79
    
80
    /**
81
     * Sets the command as errored. This state gets reported back to the user.
82
     * @param \Throwable  $throwable
83
     * @return void
84
     */
85
    function onError(\Throwable $throwable): void {
86
        $this->finished = true;
87
        $this->emit('error', array($throwable));
88
    }
89
    
90
    /**
91
     * Sends the next received value into the command.
92
     * @param mixed  $value
93
     * @return void
94
     */
95
    abstract function onNext($value): void;
96
    
97
    /**
98
     * Whether the command has finished.
99
     * @return bool
100
     */
101 2
    function hasFinished(): bool {
102 2
        return $this->finished;
103
    }
104
    
105
    /**
106
     * Whether this command sets the connection as busy.
107
     * @return bool
108
     */
109 2
    function waitForCompletion(): bool {
110 2
        return true;
111
    }
112
    
113
    /**
114
     * Handles query commands on next caller.
115
     * @param \Plasma\Drivers\MySQL\ProtocolOnNextCaller  $value
116
     * @return void
117
     */
118 1
    function handleQueryOnNextCaller(\Plasma\Drivers\MySQL\ProtocolOnNextCaller $value): void {
119 1
        $parser = $value->getParser();
0 ignored issues
show
Unused Code introduced by
The assignment to $parser is dead and can be removed.
Loading history...
120 1
        $buffer = $value->getBuffer();
121
        
122 1
        if($this->resolveValue !== null) {
123
            $this->emit('data', array($this->parseResultsetRow($buffer)));
124
        } else {
125 1
            $field = $this->handleQueryOnNextCallerColumns($value);
126 1
            if($field) {
1 ignored issue
show
introduced by
$field is of type Plasma\ColumnDefinition, thus it always evaluated to true.
Loading history...
127
                $this->fields[$field->getName()] = $field;
0 ignored issues
show
Bug Best Practice introduced by
The property fields does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
128
                
129
                if(\count($this->fields) >= $this->fieldsCount) {
130
                    $this->createResolve();
0 ignored issues
show
Bug introduced by
The method createResolve() does not exist on Plasma\Drivers\MySQL\Commands\PromiseCommand. It seems like you code against a sub-type of Plasma\Drivers\MySQL\Commands\PromiseCommand such as Plasma\Drivers\MySQL\Commands\QueryCommand. ( Ignorable by Annotation )

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

130
                    $this->/** @scrutinizer ignore-call */ 
131
                           createResolve();
Loading history...
131
                }
132
            }
133
        }
134 1
    }
135
    
136
    /**
137
     * Handles the column definitions of query commands on next caller.
138
     * @param \Plasma\Drivers\MySQL\ProtocolOnNextCaller  $value
139
     * @return \Plasma\ColumnDefinitionInterface|null
140
     */
141 1
    function handleQueryOnNextCallerColumns(\Plasma\Drivers\MySQL\ProtocolOnNextCaller $value): ?\Plasma\ColumnDefinitionInterface {
142 1
        $parser = $value->getParser();
143 1
        $buffer = $value->getBuffer();
144
        
145 1
        if($this->fieldsCount === null) {
146 1
            $fieldCount = \Plasma\Drivers\MySQL\Messages\MessageUtility::readIntLength($buffer);
147
            
148 1
            if($fieldCount === 0xFB) {
149
                // Handle it on future tick, so we can cleanly finish the buffer of this call
150
                $this->driver->getLoop()->futureTick(function () use (&$parser) {
151
                    $localMsg = new \Plasma\Drivers\MySQL\Messages\LocalInFileRequestMessage();
0 ignored issues
show
Bug introduced by
The call to Plasma\Drivers\MySQL\Mes...tMessage::__construct() has too few arguments starting with parser. ( Ignorable by Annotation )

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

151
                    $localMsg = /** @scrutinizer ignore-call */ new \Plasma\Drivers\MySQL\Messages\LocalInFileRequestMessage();

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...
152
                    $parser->handleMessage($localMsg);
153
                });
154
                
155
                return null;
156
            }
157
            
158 1
            $this->fieldsCount = $fieldCount;
0 ignored issues
show
Bug Best Practice introduced by
The property fieldsCount does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
159 1
            return null;
160
        }
161
        
162
        return static::parseColumnDefinition($buffer, $parser);
163
    }
164
    
165
    /**
166
     * Parses the column definition.
167
     * @param string                                $buffer
168
     * @param \Plasma\Drivers\MySQL\ProtocolParser  $parser
169
     * @return \Plasma\ColumnDefinitionInterface
170
     */
171
    static function parseColumnDefinition(string &$buffer, \Plasma\Drivers\MySQL\ProtocolParser $parser): \Plasma\ColumnDefinitionInterface {
0 ignored issues
show
Unused Code introduced by
The parameter $parser 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

171
    static function parseColumnDefinition(string &$buffer, /** @scrutinizer ignore-unused */ \Plasma\Drivers\MySQL\ProtocolParser $parser): \Plasma\ColumnDefinitionInterface {

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...
172
        $catalog = \Plasma\Drivers\MySQL\Messages\MessageUtility::readStringLength($buffer); // catalog
173
        $database = \Plasma\Drivers\MySQL\Messages\MessageUtility::readStringLength($buffer);
174
        $table = \Plasma\Drivers\MySQL\Messages\MessageUtility::readStringLength($buffer);
175
        $orgTable = \Plasma\Drivers\MySQL\Messages\MessageUtility::readStringLength($buffer);
176
        $name = \Plasma\Drivers\MySQL\Messages\MessageUtility::readStringLength($buffer);
177
        $orgName = \Plasma\Drivers\MySQL\Messages\MessageUtility::readStringLength($buffer);
178
        $buffer = \substr($buffer, 1); // 0x0C
179
        
180
        $charset = \Plasma\Drivers\MySQL\Messages\MessageUtility::readInt2($buffer);
181
        $length = \Plasma\Drivers\MySQL\Messages\MessageUtility::readInt4($buffer);
182
        $type = \Plasma\Drivers\MySQL\Messages\MessageUtility::readInt1($buffer);
183
        $flags = \Plasma\Drivers\MySQL\Messages\MessageUtility::readInt2($buffer);
184
        $decimals = \Plasma\Drivers\MySQL\Messages\MessageUtility::readInt1($buffer);
185
        
186
        $buffer = \substr($buffer, 2); // fillers
187
        
188
        /*if($this instanceof COM_FIELD_LIST) {
189
            \Plasma\Drivers\MySQL\Messages\MessageUtility::readStringLength($buffer);
190
        }*/
191
        
192
        $database = ($database ?: $catalog);
193
        $table = ($table ?: $orgTable);
194
        $name = ($name ?: $orgName);
195
        
196
        $charset = \Plasma\Drivers\MySQL\CharacterSetFlags::CHARSET_MAP[$charset] ?? 'Unknown charset "'.$charset.'"';
197
        $type = $type;
198
        $nullable = ($flags & \Plasma\Drivers\MySQL\FieldFlags::NOT_NULL_FLAG) === 0;
199
        
200
        return (new \Plasma\ColumnDefinition($database, $table, $name, $type, $charset, $length, $nullable, $flags, $decimals));
0 ignored issues
show
Bug introduced by
It seems like $table can also be of type null; however, parameter $table of Plasma\ColumnDefinition::__construct() does only seem to accept string, 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

200
        return (new \Plasma\ColumnDefinition($database, /** @scrutinizer ignore-type */ $table, $name, $type, $charset, $length, $nullable, $flags, $decimals));
Loading history...
Bug introduced by
It seems like $name can also be of type null; however, parameter $name of Plasma\ColumnDefinition::__construct() does only seem to accept string, 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

200
        return (new \Plasma\ColumnDefinition($database, $table, /** @scrutinizer ignore-type */ $name, $type, $charset, $length, $nullable, $flags, $decimals));
Loading history...
Bug introduced by
It seems like $database can also be of type null; however, parameter $database of Plasma\ColumnDefinition::__construct() does only seem to accept string, 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

200
        return (new \Plasma\ColumnDefinition(/** @scrutinizer ignore-type */ $database, $table, $name, $type, $charset, $length, $nullable, $flags, $decimals));
Loading history...
201
    }
202
    
203
    /**
204
     * Parses the text resultset row and returns the row.
205
     * @param \Plasma\ColumnDefinitionInterface  $column
0 ignored issues
show
Coding Style introduced by
Doc comment for parameter $column does not match actual variable name $buffer
Loading history...
206
     * @param string                             $buffer
0 ignored issues
show
Coding Style introduced by
Superfluous parameter comment
Loading history...
207
     * @return array
208
     */
209
    protected function parseResultsetRow(string &$buffer): array {
210
        $row = array();
211
        
212
        /** @var \Plasma\ColumnDefinitionInterface  $column */
213
        foreach($this->fields as $column) {
214
            $rawValue = \Plasma\Drivers\MySQL\Messages\MessageUtility::readStringLength($buffer);
215
            
216
            try {
217
                $value = \Plasma\Types\TypeExtensionsManager::getManager('driver-mysql')
218
                    ->decodeType($column->getType(), $rawValue)
219
                    ->getValue();
220
            } catch (\Plasma\Exception $e) {
221
                $value = $this->stdDecodeValue($rawValue);
0 ignored issues
show
Bug introduced by
The call to Plasma\Drivers\MySQL\Com...mmand::stdDecodeValue() has too few arguments starting with param. ( Ignorable by Annotation )

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

221
                /** @scrutinizer ignore-call */ 
222
                $value = $this->stdDecodeValue($rawValue);

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...
Bug introduced by
$rawValue of type null|string is incompatible with the type Plasma\ColumnDefinitionInterface expected by parameter $column of Plasma\Drivers\MySQL\Com...mmand::stdDecodeValue(). ( Ignorable by Annotation )

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

221
                $value = $this->stdDecodeValue(/** @scrutinizer ignore-type */ $rawValue);
Loading history...
222
            }
223
            
224
            $row[$column->getName()] = $value;
225
        }
226
        
227
        return $row;
228
    }
229
    
230
    /**
231
     * Standard decode value, if type extensions failed.
232
     * @param \Plasma\ColumnDefinitionInterface  $column
233
     * @param string                             $param
234
     * @return mixed
235
     * @throws \Plasma\Exception
236
     */
237
    protected function stdDecodeValue(\Plasma\ColumnDefinitionInterface $column, string $param) {
238
        $flags = $column->getFlags();
239
        
240
        if($param !== null && ($flags & \Plasma\Drivers\MySQL\FieldFlags::ZEROFILL_FLAG) === 0) {
241
            switch($column->getType()) {
242
                case \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_LONG:
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
243
                    if(($flags & \Plasma\Drivers\MySQL\FieldFlags::UNSIGNED_FLAG) === 0 || \PHP_INT_SIZE > 4) {
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
244
                        $param = (int) $param;
245
                    }
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
246
                break;
1 ignored issue
show
Coding Style introduced by
Case breaking statement indented incorrectly; expected 20 spaces, found 16
Loading history...
247
                case \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_LONGLONG:
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
248
                    if(($flags & \Plasma\Drivers\MySQL\FieldFlags::UNSIGNED_FLAG) === 0 && \PHP_INT_SIZE > 4) {
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
249
                        $param = (int) $param;
250
                    }
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
251
                break;
1 ignored issue
show
Coding Style introduced by
Case breaking statement indented incorrectly; expected 20 spaces, found 16
Loading history...
252
                case \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_TINY:
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
253
                case \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_SHORT:
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
254
                case \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_INT24:
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
255
                case \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_TIMESTAMP:
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
256
                    $param = (int) $param;
257
                break;
1 ignored issue
show
Coding Style introduced by
Case breaking statement indented incorrectly; expected 20 spaces, found 16
Loading history...
258
                case \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_FLOAT:
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
259
                case \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_DOUBLE:
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
260
                    $param = (float) $param;
261
                break;
1 ignored issue
show
Coding Style introduced by
Case breaking statement indented incorrectly; expected 20 spaces, found 16
Loading history...
262
                // Other types are taken as string
263
            }
264
        }
265
        
266
        return $param;
267
    }
268
}
269