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 ( b4dd81...80eb77 )
by Charlotte
03:13
created

StatementExecuteCommand::onNext()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 8.1426

Importance

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