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

StatementExecuteCommand::resetSequence()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
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
 * 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
    function __construct(\Plasma\DriverInterface $driver, $id, string $query, array $params) {
42
        parent::__construct($driver, $query);
43
        
44
        $this->id = $id;
45
        $this->params = $params;
46
    }
47
    
48
    /**
49
     * Get the encoded message for writing to the database connection.
50
     * @return string
51
     */
52
    function getEncodedMessage(): string {
53
        $packet = \chr(static::COMMAND_ID);
54
        $packet .= \Plasma\Drivers\MySQL\Messages\MessageUtility::writeInt4($this->id);
55
        
56
        $packet .= "\0"; // Cursor type flag
57
        $packet .= \Plasma\Drivers\MySQL\Messages\MessageUtility::writeInt4(1); // Iteration count is always 1
58
        
59
        $paramCount = \count($this->params);
60
        
61
        $bitmapOffset = \strlen($payload);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $payload seems to be never defined.
Loading history...
62
        $packet .= \str_repeat("\0", (($paramCount + 7) >> 3));
63
        
64
        $bound = 0;
65
        
66
        $types = '';
67
        $values = '';
68
        
69
        foreach($this->params as $id => $param) {
70
            if($param === null) {
71
                $offset = $bitmapOffset + ($id >> 3);
72
                $packet[$offset] = $packet[$offset] | \chr((1 << ($id % 8)));
73
            } else {
74
                $bound = 1;
75
            }
76
            
77
            $unsigned = false;
78
            
79
            try {
80
                $manager = \Plasma\Types\TypeExtensionsManager::getManager('driver-mysql');
81
                $encode = $manager->encodeType($param);
82
                
83
                $unsigned = $encode->isUnsigned();
84
                $type = $encode->getSQLType();
85
                $value = $encode->getValue();
86
            } catch (\Plasma\Exception $e) {
87
                [ $unsigned, $type, $value ] = $this->stdEncodeValue($param);
88
            }
89
            
90
            $types .= \chr($type);
91
            $types .= ($unsigned ? "\x80" : "\0");
92
            
93
            $values .= $value;
94
        }
95
        
96
        $packet .= \chr($bound);
97
        
98
        if($bound) {
99
            $packet .= $types;
100
            $packet .= $values;
101
        }
102
        
103
        return $packet;
104
    }
105
    
106
    /**
107
     * Parses the binary resultset row and returns the row.
108
     * @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...
109
     * @param string                             $buffer
0 ignored issues
show
Coding Style introduced by
Superfluous parameter comment
Loading history...
110
     * @return array
111
     */
112
    protected function parseResultsetRow(string &$buffer): array {
113
        $buffer = \substr($buffer, 1); // Skip first byte (header)
114
        
115
        $nullRow = array();
116
        $i = 0;
117
        
118
        /** @var \Plasma\ColumnDefinitionInterface  $column */
119
        foreach($this->fields as $column) { // Handle NULL-bitmap
120
            if((\ord($buffer[(($i + 2) >> 3)]) & (1 << (($i + 2) % 8))) !== 0) {
121
                $nullRow[$column->getName()] = null;
122
            }
123
            
124
            $i++;
125
        }
126
        
127
        $buffer = \substr($buffer, ((\count($column) + 9) >> 3)); // Remove NULL-bitmap
0 ignored issues
show
Bug introduced by
$column of type Plasma\ColumnDefinitionInterface is incompatible with the type Countable|array expected by parameter $var of count(). ( Ignorable by Annotation )

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

127
        $buffer = \substr($buffer, ((\count(/** @scrutinizer ignore-type */ $column) + 9) >> 3)); // Remove NULL-bitmap
Loading history...
Comprehensibility Best Practice introduced by
The variable $column seems to be defined by a foreach iteration on line 119. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
128
        $row = array();
129
        
130
        /** @var \Plasma\ColumnDefinitionInterface  $column */
131
        foreach($this->fields as $column) {
132
            if(\array_key_exists($columm->getName(), $nullRow)) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $columm seems to be never defined.
Loading history...
133
                $row[$columm->getName()] = $nullRow[$columm->getName()];
134
                continue;
135
            }
136
            
137
            $rawValue = \Plasma\Drivers\MySQL\Messages\MessageUtility::readStringLength($buffer);
138
            
139
            try {
140
                $value = $column->parseValue($rawValue);
141
            } catch (\Plasma\Exception $e) {
142
                $value = $this->stdDecodeValue($rawValue);
0 ignored issues
show
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

142
                $value = $this->stdDecodeValue(/** @scrutinizer ignore-type */ $rawValue);
Loading history...
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

142
                /** @scrutinizer ignore-call */ 
143
                $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...
143
            }
144
            
145
            $row[$column->getName()] = $value;
146
        }
147
        
148
        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
    protected function stdEncodeValue($param): array {
158
        $unsigned = false;
159
        
160
        switch(\gettype($param)) {
161
            case 'boolean':
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
162
                $type = \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_TINY;
163
                $value = ($param ? "\x01" : "\0");
164
            break;
1 ignored issue
show
Coding Style introduced by
Case breaking statement indented incorrectly; expected 16 spaces, found 12
Loading history...
165
            case 'integer':
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
166
                if($param >= 0) {
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
167
                    $unsigned = true;
168
                }
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
169
                
170
                // TODO: Check if short, long or long long
171
                if($param >= 0 && $param < (1 << 15)) {
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
172
                    $type = \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_SHORT;
173
                    $value = \Plasma\Drivers\MySQL\Messages\MessageUtility::writeInt2($param);
174
                } elseif(\PHP_INT_SIZE === 4) {
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
175
                    $type = \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_LONG;
176
                    $value = \Plasma\Drivers\MySQL\Messages\MessageUtility::writeInt4($param);
177
                } else {
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
178
                    $type = \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_LONGLONG;
179
                    $value = \Plasma\Drivers\MySQL\Messages\MessageUtility::writeInt8($param);
180
                }
0 ignored issues
show
Coding Style introduced by
Line indented incorrectly; expected 12 spaces, found 16
Loading history...
181
            break;
0 ignored issues
show
Coding Style introduced by
Case breaking statement indented incorrectly; expected 16 spaces, found 12
Loading history...
182
            case 'double':
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
183
                $type = \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_DOUBLE;
184
                $value = \Plasma\Drivers\MySQL\Messages\MessageUtility::writeFloat($param);
185
            break;
0 ignored issues
show
Coding Style introduced by
Case breaking statement indented incorrectly; expected 16 spaces, found 12
Loading history...
186
            case 'string':
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
187
                $type = \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_LONG_BLOB;
188
                
189
                $value = \Plasma\Drivers\MySQL\Messages\MessageUtility::writeInt4(\strlen($param));
190
                $value .= $param;
191
            break;
0 ignored issues
show
Coding Style introduced by
Case breaking statement indented incorrectly; expected 16 spaces, found 12
Loading history...
192
            case 'NULL':
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
193
                $type = \Plasma\Drivers\MySQL\FieldFlags::FIELD_TYPE_NULL;
194
                $value = '';
195
            break;
1 ignored issue
show
Coding Style introduced by
Case breaking statement indented incorrectly; expected 16 spaces, found 12
Loading history...
196
            default:
1 ignored issue
show
Coding Style introduced by
Line indented incorrectly; expected 8 spaces, found 12
Loading history...
197
                throw new \Plasma\Exception('Unexpected type for binding parameter: '.\gettype($param));
198
            break;
199
        }
200
        
201
        return array($unsigned, $type, $value);
202
    }
203
    
204
    /**
205
     * Whether the sequence ID should be resetted.
206
     * @return bool
207
     */
208
    function resetSequence(): bool {
209
        return false;
210
    }
211
}
212