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 ( dbd29a...6908d2 )
by Charlotte
03:46
created

StatementPrepareCommand::onNext()   C

Complexity

Conditions 14
Paths 13

Size

Total Lines 41
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 22.1327

Importance

Changes 0
Metric Value
cc 14
eloc 28
nc 13
nop 1
dl 0
loc 41
ccs 17
cts 26
cp 0.6538
crap 22.1327
rs 6.2666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 Prepare command.
14
 * @internal
15
 */
16
class StatementPrepareCommand extends PromiseCommand {
17
    /**
18
     * The identifier for this command.
19
     * @var int
20
     * @source
21
     */
22
    const COMMAND_ID = 0x16;
23
    
24
    /**
25
     * @var \Plasma\ClientInterface
26
     */
27
    protected $client;
28
    
29
    /**
30
     * @var string
31
     */
32
    protected $query;
33
    
34
    /**
35
     * @var string
36
     */
37
    protected $rewrittenQuery;
38
    
39
    /**
40
     * @var array
41
     */
42
    protected $rewrittenParams;
43
    
44
    /**
45
     * @var \Plasma\Drivers\MySQL\Messages\PrepareStatementOkMessage
46
     */
47
    protected $okResponse;
48
    
49
    /**
50
     * @var \Plasma\ColumnDefinitionInterface[]
51
     */
52
    protected $params = array();
53
    
54
    /**
55
     * @var \Plasma\ColumnDefinitionInterface[]
56
     */
57
    protected $fields = array();
58
    
59
    /**
60
     * @var bool
61
     */
62
    protected $paramsDone = false;
63
    
64
    /**
65
     * @var \Plasma\StatementInterface|null
66
     */
67
    protected $resolveValue;
68
    
69
    /**
70
     * @var bool
71
     */
72
    protected $deprecatedEOF;
73
    
74
    /**
75
     * Constructor.
76
     * @param \Plasma\ClientInterface  $client
77
     * @param \Plasma\DriverInterface  $driver
78
     * @param string                   $query
79
     */
80 34
    function __construct(\Plasma\ClientInterface $client, \Plasma\DriverInterface $driver, string $query) {
81 34
        parent::__construct($driver);
82
        
83 34
        $this->client = $client;
84 34
        $this->driver = $driver;
85 34
        $this->query = $query;
86 34
        $this->deprecatedEOF = (($driver->getHandshake()->capability & \Plasma\Drivers\MySQL\CapabilityFlags::CLIENT_DEPRECATE_EOF) !== 0);
87
        
88 34
        [ 'query' => $this->rewrittenQuery, 'parameters' => $this->rewrittenParams ] = \Plasma\Utility::parseParameters($this->query, '?');
89 34
    }
90
    
91
    /**
92
     * Get the encoded message for writing to the database connection.
93
     * @return string
94
     */
95 34
    function getEncodedMessage(): string {
96 34
        return \chr(static::COMMAND_ID).$this->rewrittenQuery;
97
    }
98
    
99
    /**
100
     * Sends the next received value into the command.
101
     * @param mixed  $value
102
     * @return void
103
     * @throws \Plasma\Drivers\MySQL\Messages\ParseException
104
     */
105 2
    function onNext($value): void {
106 2
        if($value instanceof \Plasma\Drivers\MySQL\Messages\PrepareStatementOkMessage) {
107 2
            $this->okResponse = $value;
108 2
            $this->fieldsCount = $this->okResponse->numColumns;
109 2
            $this->paramsDone = ($this->okResponse->numParams === 0);
110 2
        } elseif($value instanceof \Plasma\Drivers\MySQL\ProtocolOnNextCaller) {
111 2
            $buffer = $value->getBuffer();
112 2
            $parser = $value->getParser();
113
            
114 2
            $parsed = $this->handleQueryOnNextCallerColumns($buffer, $parser);
115
            
116 2
            if($this->paramsDone) {
117 2
                $this->fields[$parsed->getName()] = $parsed;
118
            } else {
119 2
                $this->params[] = $parsed;
120
            }
121
            
122 2
            if($this->deprecatedEOF) {
123 2
                if($this->paramsDone && $this->fieldsCount <= \count($this->fields)) {
124 2
                    $this->createResolve();
125 2
                } elseif($this->okResponse->numParams <= \count($this->params) && $this->resolveValue === null) {
126 2
                    $this->paramsDone = true;
127
                }
128
            }
129
        } elseif(
130
            $value instanceof \Plasma\Drivers\MySQL\Messages\EOFMessage || $value instanceof \Plasma\Drivers\MySQL\Messages\OkResponseMessage
131
        ) {
132
            if(!$this->paramsDone) {
133
                $this->paramsDone = true;
134
                
135
                if($this->okResponse->numColumns === 0) {
136
                    $this->createResolve();
137
                }
138
                
139
                return;
140
            }
141
            
142
            $this->createResolve();
143
        } else {
144
            throw new \Plasma\Drivers\MySQL\Messages\ParseException('Command received value of type '
145
                .(\is_object($value) ? \get_class($value) : \gettype($value)).' it can not handle');
146
        }
147 2
    }
148
    
149
    /**
150
     * Whether the sequence ID should be resetted.
151
     * @return bool
152
     */
153 34
    function resetSequence(): bool {
154 34
        return true;
155
    }
156
    
157
    /**
158
     * Creates the resolve value.
159
     * @return void
160
     */
161 2
    protected function createResolve(): void {
162 2
        $this->finished = true;
163
        
164 2
        $id = $this->okResponse->statementID;
165 2
        $queryr = $this->rewrittenQuery;
166 2
        $paramsr = $this->rewrittenParams;
167
        
168 2
        $this->resolveValue = new \Plasma\Drivers\MySQL\Statement($this->client, $this->driver, $id, $this->query, $queryr, $paramsr, $this->params, $this->fields);
169 2
        $this->deferred->resolve($this->resolveValue);
170 2
    }
171
}
172