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 ( 9c8b5e...1077ea )
by Charlotte
03:13
created

PromiseCommand   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 244
Duplicated Lines 0 %

Test Coverage

Coverage 53.61%

Importance

Changes 0
Metric Value
eloc 88
dl 0
loc 244
ccs 52
cts 97
cp 0.5361
rs 9.84
c 0
b 0
f 0
wmc 32

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getPromise() 0 2 1
A setParserState() 0 2 1
A __construct() 0 9 1
A onError() 0 3 1
A hasFinished() 0 2 1
A onComplete() 0 3 1
A waitForCompletion() 0 2 1
A handleQueryOnNextCaller() 0 16 3
C stdDecodeValue() 0 30 15
A parseColumnDefinition() 0 26 1
A parseResultsetRow() 0 19 3
A handleQueryOnNextCallerColumns() 0 19 3
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 3
    function __construct() {
38 3
        $this->deferred = new \React\Promise\Deferred();
39
        
40
        $this->once('error', function (\Throwable $error) {
41
            $this->deferred->reject($error);
42 3
        });
43
        
44
        $this->once('end', function () {
45 2
            $this->deferred->resolve($this->resolveValue);
46 3
        });
47 3
    }
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 3
    function getPromise(): \React\Promise\PromiseInterface {
60 3
        return $this->deferred->promise();
61
    }
62
    
63
    /**
64
     * Sets the parser state, if necessary. If not, return `-1`.
65
     * @return int
66
     */
67 3
    function setParserState(): int {
68 3
        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');
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 3
    function hasFinished(): bool {
102 3
        return $this->finished;
103
    }
104
    
105
    /**
106
     * Whether this command sets the connection as busy.
107
     * @return bool
108
     */
109 3
    function waitForCompletion(): bool {
110 3
        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
        $buffer = $value->getBuffer();
120 1
        $parser = $value->getParser();
121
        
122 1
        if($this->resolveValue !== null) {
123
            $row = $this->parseResultsetRow($buffer);
124
            $this->emit('data', array($row));
125
        } else {
126 1
            $field = $this->handleQueryOnNextCallerColumns($buffer, $parser);
127 1
            if($field) {
1 ignored issue
show
introduced by
$field is of type Plasma\ColumnDefinition, thus it always evaluated to true.
Loading history...
128 1
                $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...
129
            }
130
        }
131
        
132
        // By ref doesn't seem to work, this is a workaround for now
133 1
        $value->setBuffer($buffer);
134 1
    }
135
    
136
    /**
137
     * Handles the column definitions of query commands on next caller.
138
     * @param string                                $buffer
139
     * @param \Plasma\Drivers\MySQL\ProtocolParser  $parser
140
     * @return \Plasma\ColumnDefinitionInterface|null
141
     */
142 2
    function handleQueryOnNextCallerColumns(string &$buffer, \Plasma\Drivers\MySQL\ProtocolParser $parser): ?\Plasma\ColumnDefinitionInterface {
143 2
        if($this->fieldsCount === null) {
144 1
            $fieldCount = \Plasma\Drivers\MySQL\Messages\MessageUtility::readIntLength($buffer);
145
            
146 1
            if($fieldCount === 0xFB) {
147
                // Handle it on future tick, so we can cleanly finish the buffer of this call
148
                $this->driver->getLoop()->futureTick(function () use (&$parser) {
149
                    $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

149
                    $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...
150
                    $parser->handleMessage($localMsg);
151
                });
152
                
153
                return null;
154
            }
155
            
156 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...
157 1
            return null;
158
        }
159
        
160 2
        return static::parseColumnDefinition($buffer);
161
    }
162
    
163
    /**
164
     * Parses the column definition.
165
     * @param string  $buffer
166
     * @return \Plasma\ColumnDefinitionInterface
167
     */
168 2
    static function parseColumnDefinition(string &$buffer): \Plasma\ColumnDefinitionInterface {
169 2
        \Plasma\Drivers\MySQL\Messages\MessageUtility::readStringLength($buffer); // catalog - always "def"
170 2
        $database = \Plasma\Drivers\MySQL\Messages\MessageUtility::readStringLength($buffer);
171 2
        $table = \Plasma\Drivers\MySQL\Messages\MessageUtility::readStringLength($buffer);
172 2
        \Plasma\Drivers\MySQL\Messages\MessageUtility::readStringLength($buffer); // orgTable
173 2
        $name = \Plasma\Drivers\MySQL\Messages\MessageUtility::readStringLength($buffer);
174 2
        \Plasma\Drivers\MySQL\Messages\MessageUtility::readStringLength($buffer); // orgName
175 2
        $buffer = \substr($buffer, 1); // 0x0C
176
        
177 2
        $charset = \Plasma\Drivers\MySQL\Messages\MessageUtility::readInt2($buffer);
178 2
        $length = \Plasma\Drivers\MySQL\Messages\MessageUtility::readInt4($buffer);
179 2
        $type = \Plasma\Drivers\MySQL\Messages\MessageUtility::readInt1($buffer);
180 2
        $flags = \Plasma\Drivers\MySQL\Messages\MessageUtility::readInt2($buffer);
181 2
        $decimals = \Plasma\Drivers\MySQL\Messages\MessageUtility::readInt1($buffer);
182
        
183 2
        $buffer = \substr($buffer, 2); // fillers
184
        
185
        /*if($this instanceof COM_FIELD_LIST) {
186
            \Plasma\Drivers\MySQL\Messages\MessageUtility::readStringLength($buffer);
187
        }*/
188
        
189 2
        $charset = \Plasma\Drivers\MySQL\CharacterSetFlags::CHARSET_MAP[$charset] ?? 'Unknown charset "'.$charset.'"';
190 2
        $type = \Plasma\Drivers\MySQL\FieldFlags::TYPE_MAP[$type] ?? 'Unknown type "'.$type.'"';
191 2
        $nullable = (($flags & \Plasma\Drivers\MySQL\FieldFlags::NOT_NULL_FLAG) === 0);
192
        
193 2
        return (new \Plasma\ColumnDefinition($database, $table, $name, $type, $charset, $length, $nullable, $flags, $decimals));
0 ignored issues
show
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

193
        return (new \Plasma\ColumnDefinition(/** @scrutinizer ignore-type */ $database, $table, $name, $type, $charset, $length, $nullable, $flags, $decimals));
Loading history...
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

193
        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

193
        return (new \Plasma\ColumnDefinition($database, $table, /** @scrutinizer ignore-type */ $name, $type, $charset, $length, $nullable, $flags, $decimals));
Loading history...
194
    }
195
    
196
    /**
197
     * Parses the text resultset row and returns the row.
198
     * @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...
199
     * @param string                             $buffer
0 ignored issues
show
Coding Style introduced by
Superfluous parameter comment
Loading history...
200
     * @return array
201
     */
202
    protected function parseResultsetRow(string &$buffer): array {
203
        $row = array();
204
        
205
        /** @var \Plasma\ColumnDefinitionInterface  $column */
206
        foreach($this->fields as $column) {
207
            $rawValue = \Plasma\Drivers\MySQL\Messages\MessageUtility::readStringLength($buffer);
208
            
209
            try {
210
                $value = \Plasma\Types\TypeExtensionsManager::getManager('driver-mysql')
211
                    ->decodeType($column->getType(), $rawValue)
212
                    ->getValue();
213
            } catch (\Plasma\Exception $e) {
214
                $value = $this->stdDecodeValue($column, $rawValue);
0 ignored issues
show
Bug introduced by
It seems like $rawValue can also be of type null; however, parameter $param of Plasma\Drivers\MySQL\Com...mmand::stdDecodeValue() 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

214
                $value = $this->stdDecodeValue($column, /** @scrutinizer ignore-type */ $rawValue);
Loading history...
215
            }
216
            
217
            $row[$column->getName()] = $value;
218
        }
219
        
220
        return $row;
221
    }
222
    
223
    /**
224
     * Standard decode value, if type extensions failed.
225
     * @param \Plasma\ColumnDefinitionInterface  $column
226
     * @param string                             $param
227
     * @return mixed
228
     * @throws \Plasma\Exception
229
     */
230
    protected function stdDecodeValue(\Plasma\ColumnDefinitionInterface $column, string $param) {
231
        $flags = $column->getFlags();
232
        
233
        if($param !== null && ($flags & \Plasma\Drivers\MySQL\FieldFlags::ZEROFILL_FLAG) === 0) {
234
            switch($column->getType()) {
235
                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...
236
                    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...
237
                        $param = (int) $param;
238
                    }
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
239
                break;
1 ignored issue
show
Coding Style introduced by
Case breaking statement indented incorrectly; expected 20 spaces, found 16
Loading history...
240
                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...
241
                    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...
242
                        $param = (int) $param;
243
                    }
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 16 spaces, found 20
Loading history...
244
                break;
1 ignored issue
show
Coding Style introduced by
Case breaking statement indented incorrectly; expected 20 spaces, found 16
Loading history...
245
                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...
246
                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...
247
                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...
248
                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...
249
                    $param = (int) $param;
250
                break;
1 ignored issue
show
Coding Style introduced by
Case breaking statement indented incorrectly; expected 20 spaces, found 16
Loading history...
251
                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...
252
                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...
253
                    $param = (float) $param;
254
                break;
1 ignored issue
show
Coding Style introduced by
Case breaking statement indented incorrectly; expected 20 spaces, found 16
Loading history...
255
                // Other types are taken as string
256
            }
257
        }
258
        
259
        return $param;
260
    }
261
}
262