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
Pull Request — master (#25)
by Charlotte
02:22
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 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Plasma Driver MySQL component
4
 * Copyright 2018-2019 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
     * @var \Plasma\ColumnDefinitionInterface[]
36
     */
37
    protected $paramsDef;
38
    
39
    /**
40
     * @var int
41
     */
42
    protected $cursor;
43
    
44
    /**
45
     * Constructor.
46
     * @param \Plasma\DriverInterface              $driver
47 40
     * @param mixed                                $id
48 40
     * @param string                               $query
49
     * @param array                                $params
50 40
     * @param \Plasma\ColumnDefinitionInterface[]  $paramsDef
51 40
     * @param int                                  $cursor
52 40
     */
53 40
    function __construct(\Plasma\DriverInterface $driver, $id, string $query, array $params, array $paramsDef, int $cursor = 0) {
54
        parent::__construct($driver, $query);
55
        
56
        $this->id = $id;
57
        $this->params = $params;
58
        $this->paramsDef = $paramsDef;
59 39
        $this->cursor = $cursor;
60 39
    }
61 39
    
62
    /**
63 39
     * Get the encoded message for writing to the database connection.
64 39
     * @return string
65
     */
66 39
    function getEncodedMessage(): string {
67 39
        $packet = \chr(static::COMMAND_ID);
68
        $packet .= \Plasma\BinaryBuffer::writeInt4($this->id);
69 39
        
70
        $packet .= \chr($this->cursor);
71 39
        $packet .= \Plasma\BinaryBuffer::writeInt4(1); // Iteration count is always 1
72 39
        
73
        $bitmapOffset = \strlen($packet);
74 39
        $packet .= \str_repeat("\x00", ((\count($this->params) + 7) >> 3));
75 38
        
76 1
        $bound = 0;
77 1
        
78
        $types = '';
79 38
        $values = '';
80
        
81
        foreach($this->params as $id => $param) {
82
            if($param === null) {
83 38
                $offset = $bitmapOffset + ($id >> 3);
84 36
                $packet[$offset] = $packet[$offset] | \chr((1 << ($id % 8)));
85
            } else {
86
                $bound = 1;
87
            }
88
            
89 38
            try {
90 38
                $encode = \Plasma\Types\TypeExtensionsManager::getManager('driver-mysql')
91
                    ->encodeType($param, $this->paramsDef[$id]);
92
                
93 35
                $unsigned = $encode->isUnsigned();
94 35
                $type = $encode->getDatabaseType();
95
                $value = $encode->getValue();
96
            } catch (\Plasma\Exception $e) {
97 36
                [ $unsigned, $type, $value ] = \Plasma\Drivers\MySQL\BinaryProtocolValues::encode($param);
98
            }
99 36
            
100 35
            $types .= \chr($type).($unsigned ? "\x80" : "\x00");
101 35
            $values .= $value;
102
        }
103
        
104 36
        $packet .= \chr($bound);
105
        
106
        if($bound) {
107
            $packet .= $types;
108
            $packet .= $values;
109
        }
110
        
111 38
        return $packet;
112 38
    }
113
    
114
    /**
115
     * Whether the sequence ID should be resetted.
116
     * @return bool
117
     */
118
    function resetSequence(): bool {
119
        return true;
120 35
    }
121 35
    
122
    /**
123 35
     * Sends the next received value into the command.
124 35
     * @param mixed  $value
125
     * @return void
126
     * @throws \Plasma\Exception
127 35
     */
128 35
    function onNext($value): void {
129 2
        if($this->cursor > 0 && $this->fieldsCount > 0 && $this->fieldsCount <= \count($this->fields)) {
130
            if(!($value instanceof \Plasma\Drivers\MySQL\Messages\OkResponseMessage || $value instanceof \Plasma\Drivers\MySQL\Messages\EOFMessage)) {
131
                throw new \Plasma\Exception('Requested a cursor, but received row instead');
132 35
            } elseif(($value->statusFlags & \Plasma\Drivers\MySQL\StatusFlags::SERVER_STATUS_CURSOR_EXISTS) === 0) {
133
                throw new \Plasma\Exception('Requested a cursor, but did not receive one');
134
            }
135 35
        }
136 35
        
137
        parent::onNext($value);
138
    }
139 35
    
140 35
    /**
141 2
     * Parses the binary resultset row and returns the row.
142 2
     * @param \Plasma\BinaryBuffer  $buffer
143
     * @return array
144
     */
145
    protected function parseResultsetRow(\Plasma\BinaryBuffer $buffer): array {
146 35
        $buffer->read(1); // remove packet header
147 35
        
148
        $nullRow = array();
149 35
        $i = 0;
150 35
        
151
        /** @var \Plasma\ColumnDefinitionInterface  $column */
152
        foreach($this->fields as $column) { // Handle NULL-bitmap
153 35
            if((\ord($buffer[(($i + 2) >> 3)]) & (1 << (($i + 2) % 8))) !== 0) {
154
                $nullRow[$column->getName()] = null;
155
            }
156 35
            
157
            $i++;
158
        }
159
        
160
        $buffer->read(((\count($this->fields) + 9) >> 3)); // Remove NULL-bitmap
161
        $row = array();
162
        
163
        /** @var \Plasma\ColumnDefinitionInterface  $column */
164
        foreach($this->fields as $column) {
165
            if(\array_key_exists($column->getName(), $nullRow)) {
166
                $row[$column->getName()] = null;
167
                continue;
168
            }
169
            
170
            try {
171
                $value = \Plasma\Types\TypeExtensionsManager::getManager('driver-mysql')
172
                    ->decodeType($column->getType(), $buffer)
173
                    ->getValue();
174
            } catch (\Plasma\Exception $e) {
175
                $value = \Plasma\Drivers\MySQL\BinaryProtocolValues::decode($column, $buffer);
176
            }
177
            
178
            $row[$column->getName()] = $value;
179
        }
180
        
181
        return $row;
182
    }
183
}
184