| Total Complexity | 65 |
| Total Lines | 493 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ProtocolParser often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ProtocolParser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class ProtocolParser implements \Evenement\EventEmitterInterface { |
||
| 18 | use \Evenement\EventEmitterTrait; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var int |
||
| 22 | */ |
||
| 23 | const STATE_INIT = 0; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var int |
||
| 27 | */ |
||
| 28 | const STATE_HANDSHAKE = 1; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var int |
||
| 32 | */ |
||
| 33 | const STATE_HANDSHAKE_ERROR = 2; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var int |
||
| 37 | */ |
||
| 38 | const STATE_AUTH = 5; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var int |
||
| 42 | */ |
||
| 43 | const STATE_AUTH_SENT = 6; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var int |
||
| 47 | */ |
||
| 48 | const STATE_AUTH_ERROR = 7; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var int |
||
| 52 | */ |
||
| 53 | const STATE_OK = 9; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var int |
||
| 57 | */ |
||
| 58 | const CLIENT_CAPABILITIES = ( |
||
| 59 | \Plasma\Drivers\MySQL\CapabilityFlags::CLIENT_LONG_PASSWORD | |
||
| 60 | \Plasma\Drivers\MySQL\CapabilityFlags::CLIENT_LONG_FLAG | |
||
| 61 | \Plasma\Drivers\MySQL\CapabilityFlags::CLIENT_LOCAL_FILES | |
||
| 62 | \Plasma\Drivers\MySQL\CapabilityFlags::CLIENT_INTERACTIVE | |
||
| 63 | \Plasma\Drivers\MySQL\CapabilityFlags::CLIENT_TRANSACTIONS | |
||
| 64 | \Plasma\Drivers\MySQL\CapabilityFlags::CLIENT_SECURE_CONNECTION | |
||
| 65 | \Plasma\Drivers\MySQL\CapabilityFlags::CLIENT_PROTOCOL_41 | |
||
| 66 | \Plasma\Drivers\MySQL\CapabilityFlags::CLIENT_DEPRECATE_EOF |
||
| 67 | ); |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var int |
||
| 71 | */ |
||
| 72 | const CLIENT_MAX_PACKET_SIZE = 0x1000000; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var int |
||
| 76 | */ |
||
| 77 | const CLIENT_CHARSET_NUMBER = 0x21; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var \Plasma\Drivers\MySQL\Driver |
||
| 81 | */ |
||
| 82 | protected $driver; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var \React\Socket\ConnectionInterface |
||
| 86 | */ |
||
| 87 | protected $connection; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var int |
||
| 91 | */ |
||
| 92 | protected $state = ProtocolParser::STATE_INIT; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var \Plasma\BinaryBuffer |
||
| 96 | */ |
||
| 97 | protected $buffer; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var \Plasma\BinaryBuffer |
||
| 101 | */ |
||
| 102 | protected $messageBuffer; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * The sequence ID is incremented with each packet and may wrap around. |
||
| 106 | * It starts at 0 and is reset to 0 when a new command begins in the Command Phase. |
||
| 107 | * @var int |
||
| 108 | * @see https://dev.mysql.com/doc/internals/en/sequence-id.html |
||
| 109 | */ |
||
| 110 | protected $sequenceID = 0; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var \Plasma\Drivers\MySQL\Messages\HandshakeMessage|null |
||
| 114 | */ |
||
| 115 | protected $handshakeMessage; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @var \Plasma\Drivers\MySQL\Messages\OkResponseMessage|null |
||
| 119 | */ |
||
| 120 | protected $lastOkMessage; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @var \Plasma\CommandInterface|null |
||
| 124 | */ |
||
| 125 | protected $currentCommand; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @var callable|null |
||
| 129 | */ |
||
| 130 | protected $parseCallback; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Constructor. |
||
| 134 | * @param \Plasma\Drivers\MySQL\Driver $driver |
||
| 135 | * @param \React\Socket\ConnectionInterface $connection |
||
| 136 | */ |
||
| 137 | function __construct(\Plasma\Drivers\MySQL\Driver $driver, \React\Socket\ConnectionInterface $connection) { |
||
| 138 | $this->driver = $driver; |
||
| 139 | $this->connection = $connection; |
||
| 140 | |||
| 141 | $this->buffer = new \Plasma\BinaryBuffer(); |
||
| 142 | $this->messageBuffer = new \Plasma\BinaryBuffer(); |
||
| 143 | |||
| 144 | $this->addEvents(); |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Invoke a command to execute. |
||
| 149 | * @param \Plasma\CommandInterface|null $command |
||
| 150 | * @return void |
||
| 151 | */ |
||
| 152 | function invokeCommand(?\Plasma\CommandInterface $command): void { |
||
| 153 | if($command === null) { |
||
| 154 | return; |
||
| 155 | } |
||
| 156 | |||
| 157 | $this->currentCommand = $command; |
||
| 158 | $this->processCommand(); |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Executes a command, without handling any aftermath. |
||
| 163 | * The `onComplete` callback will be immediately invoked, regardless of the `waitForCompletion` value. |
||
| 164 | * @param \Plasma\CommandInterface $command |
||
| 165 | * @return void |
||
| 166 | */ |
||
| 167 | function executeCommand(\Plasma\CommandInterface $command): void { |
||
| 168 | $this->processCommand($command); |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Marks the command itself as finished, if currently running. |
||
| 173 | * @param \Plasma\Drivers\MySQL\Commands\CommandInterface $command |
||
| 174 | * @return void |
||
| 175 | */ |
||
| 176 | function markCommandAsFinished(\Plasma\CommandInterface $command): void { |
||
| 177 | if($command === $this->currentCommand) { |
||
| 178 | $this->currentCommand = null; |
||
| 179 | } |
||
| 180 | |||
| 181 | $command->onComplete(); |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Get the parser state. |
||
| 186 | * @return int |
||
| 187 | */ |
||
| 188 | function getState(): int { |
||
| 189 | return $this->state; |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Get the handshake message, or null. |
||
| 194 | * @return \Plasma\Drivers\MySQL\Messages\HandshakeMessage|null |
||
| 195 | */ |
||
| 196 | function getHandshakeMessage(): ?\Plasma\Drivers\MySQL\Messages\HandshakeMessage { |
||
| 197 | return $this->handshakeMessage; |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Get the last ok response message, or null. |
||
| 202 | * @return \Plasma\Drivers\MySQL\Messages\OkResponseMessage|null |
||
| 203 | */ |
||
| 204 | function getLastOkMessage(): ?\Plasma\Drivers\MySQL\Messages\OkResponseMessage { |
||
| 205 | return $this->lastOkMessage; |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Sends a packet to the server. |
||
| 210 | * @param string $packet |
||
| 211 | * @return void |
||
| 212 | */ |
||
| 213 | function sendPacket(string $packet): void { |
||
| 214 | $maxSize = static::CLIENT_MAX_PACKET_SIZE - 4; |
||
| 215 | |||
| 216 | do { |
||
| 217 | $partial = \substr($packet, 0, $maxSize); |
||
| 218 | $packet = \substr($packet, $maxSize); |
||
| 219 | |||
| 220 | $length = \Plasma\BinaryBuffer::writeInt3(\strlen($partial)); |
||
| 221 | $sequence = \Plasma\BinaryBuffer::writeInt1((++$this->sequenceID)); |
||
| 222 | |||
| 223 | $this->connection->write($length.$sequence.$partial); |
||
| 224 | } while(\strlen($packet) > $maxSize); |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Sets the parse callback. |
||
| 229 | * @param callable $callback |
||
| 230 | * @return void |
||
| 231 | */ |
||
| 232 | function setParseCallback(callable $callback): void { |
||
| 233 | $this->parseCallback = $callback; |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Processes a command. |
||
| 238 | * @param \Plasma\CommandInterface|null $command |
||
| 239 | * @return void |
||
| 240 | */ |
||
| 241 | protected function processCommand(?\Plasma\CommandInterface $command = null) { |
||
| 242 | if($command === null && $this->currentCommand instanceof \Plasma\CommandInterface) { |
||
| 243 | $command = $this->currentCommand; |
||
| 244 | |||
| 245 | if($this->currentCommand instanceof \Plasma\Drivers\MySQL\Commands\CommandInterface) { |
||
| 246 | $state = $command->setParserState(); |
||
| 247 | if($state !== -1) { |
||
| 248 | $this->state = $state; |
||
| 249 | } |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | if($command === null) { |
||
| 254 | return; |
||
| 255 | } |
||
| 256 | |||
| 257 | if(!($command instanceof \Plasma\Drivers\MySQL\Commands\CommandInterface) || $command->resetSequence()) { |
||
| 258 | $this->sequenceID = -1; |
||
| 259 | } |
||
| 260 | |||
| 261 | //\assert((\Plasma\Drivers\MySQL\Messages\MessageUtility::debug('Processing command '.get_class($command)) || true)); |
||
| 262 | |||
| 263 | $this->sendPacket($command->getEncodedMessage()); |
||
| 264 | |||
| 265 | if($command !== $this->currentCommand || !$command->waitForCompletion()) { |
||
| 266 | //\assert((\Plasma\Drivers\MySQL\Messages\MessageUtility::debug('Mark command as completed') || true)); |
||
| 267 | $command->onComplete(); |
||
| 268 | |||
| 269 | if($command === $this->currentCommand) { |
||
| 270 | $this->currentCommand = null; |
||
| 271 | } |
||
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Processes the buffer. |
||
| 277 | * @return void |
||
| 278 | */ |
||
| 279 | protected function processBuffer() { |
||
| 407 | } |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Handles an incoming message. |
||
| 411 | * @param \Plasma\BinaryBuffer $buffer |
||
| 412 | * @param \Plasma\Drivers\MySQL\Messages\MessageInterface $message |
||
| 413 | * @return void |
||
| 414 | */ |
||
| 415 | function handleMessage(\Plasma\BinaryBuffer $buffer, \Plasma\Drivers\MySQL\Messages\MessageInterface $message) { |
||
| 484 | }); |
||
| 485 | } |
||
| 486 | } |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Adds the events to the connection. |
||
| 490 | * @return void |
||
| 491 | */ |
||
| 492 | protected function addEvents() { |
||
| 493 | $this->connection->on('data', function ($chunk) { |
||
| 494 | $this->buffer->append($chunk); |
||
| 495 | $this->processBuffer(); |
||
| 496 | }); |
||
| 497 | |||
| 498 | $this->connection->on('close', function () { |
||
| 499 | $this->handleClose(); |
||
| 500 | }); |
||
| 501 | } |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Connection close handler. |
||
| 505 | * @return void |
||
| 506 | */ |
||
| 507 | protected function handleClose() { |
||
| 510 | } |
||
| 511 | } |
||
| 512 | } |
||
| 513 |