| Total Complexity | 81 |
| Total Lines | 601 |
| Duplicated Lines | 0 % |
| Coverage | 79.13% |
| 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 |
||
| 16 | class ProtocolParser implements \Evenement\EventEmitterInterface { |
||
| 17 | use \Evenement\EventEmitterTrait; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var int |
||
| 21 | */ |
||
| 22 | const STATE_INIT = 0; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var int |
||
| 26 | */ |
||
| 27 | const STATE_HANDSHAKE = 1; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var int |
||
| 31 | */ |
||
| 32 | const STATE_HANDSHAKE_ERROR = 2; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var int |
||
| 36 | */ |
||
| 37 | const STATE_AUTH = 5; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var int |
||
| 41 | */ |
||
| 42 | const STATE_AUTH_SENT = 6; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var int |
||
| 46 | */ |
||
| 47 | const STATE_AUTH_ERROR = 7; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var int |
||
| 51 | */ |
||
| 52 | const STATE_OK = 9; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var int |
||
| 56 | */ |
||
| 57 | const CLIENT_CAPABILITIES = ( |
||
| 58 | \Plasma\Drivers\MySQL\CapabilityFlags::CLIENT_FOUND_ROWS | |
||
| 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 = -1; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Whether we use compression. |
||
| 114 | * @var bool |
||
| 115 | */ |
||
| 116 | protected $compressionEnabled = false; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * The compression ID is incremented with each packet and may wrap around. |
||
| 120 | * The compression ID is independent to the sequence ID. |
||
| 121 | * @var int |
||
| 122 | * @see https://dev.mysql.com/doc/internals/en/compressed-packet-header.html |
||
| 123 | */ |
||
| 124 | protected $compressionID = -1; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Small packets should not be compressed. This defines a minimum size for compression. |
||
| 128 | * @var int |
||
| 129 | * @see https://dev.mysql.com/doc/internals/en/uncompressed-payload.html |
||
| 130 | */ |
||
| 131 | protected $compressionSizeThreshold = 50; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @var \Plasma\BinaryBuffer |
||
| 135 | */ |
||
| 136 | protected $compressionBuffer; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @var \Plasma\Drivers\MySQL\Messages\HandshakeMessage|null |
||
| 140 | */ |
||
| 141 | protected $handshakeMessage; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @var \Plasma\Drivers\MySQL\Messages\OkResponseMessage|null |
||
| 145 | */ |
||
| 146 | protected $lastOkMessage; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @var \Plasma\CommandInterface|null |
||
| 150 | */ |
||
| 151 | protected $currentCommand; |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @var callable|null |
||
| 155 | */ |
||
| 156 | protected $parseCallback; |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Constructor. |
||
| 160 | * @param \Plasma\Drivers\MySQL\Driver $driver |
||
| 161 | * @param \React\Socket\ConnectionInterface $connection |
||
| 162 | */ |
||
| 163 | 60 | function __construct(\Plasma\Drivers\MySQL\Driver $driver, \React\Socket\ConnectionInterface $connection) { |
|
| 164 | 60 | $this->driver = $driver; |
|
| 165 | 60 | $this->connection = $connection; |
|
| 166 | |||
| 167 | 60 | $this->buffer = new \Plasma\BinaryBuffer(); |
|
| 168 | 60 | $this->messageBuffer = new \Plasma\BinaryBuffer(); |
|
| 169 | 60 | $this->compressionBuffer = new \Plasma\BinaryBuffer(); |
|
| 170 | |||
| 171 | 60 | $this->addEvents(); |
|
| 172 | 60 | } |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Invoke a command to execute. |
||
| 176 | * @param \Plasma\CommandInterface|null $command |
||
| 177 | * @return void |
||
| 178 | */ |
||
| 179 | 59 | function invokeCommand(?\Plasma\CommandInterface $command): void { |
|
| 180 | 59 | if($command === null) { |
|
| 181 | 56 | return; |
|
| 182 | } |
||
| 183 | |||
| 184 | 59 | $this->currentCommand = $command; |
|
| 185 | 59 | $this->processCommand(); |
|
| 186 | 59 | } |
|
| 187 | |||
| 188 | /** |
||
| 189 | * Executes a command, without handling any aftermath. |
||
| 190 | * The `onComplete` callback will be immediately invoked, regardless of the `waitForCompletion` value. |
||
| 191 | * @param \Plasma\CommandInterface $command |
||
| 192 | * @return void |
||
| 193 | */ |
||
| 194 | function executeCommand(\Plasma\CommandInterface $command): void { |
||
| 195 | $this->processCommand($command); |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Marks the command itself as finished, if currently running. |
||
| 200 | * @param \Plasma\Drivers\MySQL\Commands\CommandInterface $command |
||
| 201 | * @return void |
||
| 202 | */ |
||
| 203 | 56 | function markCommandAsFinished(\Plasma\CommandInterface $command): void { |
|
| 204 | 56 | if($command === $this->currentCommand) { |
|
| 205 | 56 | $this->currentCommand = null; |
|
| 206 | } |
||
| 207 | |||
| 208 | 56 | $command->onComplete(); |
|
| 209 | 56 | } |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Get the parser state. |
||
| 213 | * @return int |
||
| 214 | */ |
||
| 215 | 1 | function getState(): int { |
|
| 216 | 1 | return $this->state; |
|
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Get the handshake message, or null. |
||
| 221 | * @return \Plasma\Drivers\MySQL\Messages\HandshakeMessage|null |
||
| 222 | */ |
||
| 223 | 59 | function getHandshakeMessage(): ?\Plasma\Drivers\MySQL\Messages\HandshakeMessage { |
|
| 224 | 59 | return $this->handshakeMessage; |
|
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Get the last ok response message, or null. |
||
| 229 | * @return \Plasma\Drivers\MySQL\Messages\OkResponseMessage|null |
||
| 230 | */ |
||
| 231 | 4 | function getLastOkMessage(): ?\Plasma\Drivers\MySQL\Messages\OkResponseMessage { |
|
| 232 | 4 | return $this->lastOkMessage; |
|
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Enables compression. |
||
| 237 | * @return void |
||
| 238 | */ |
||
| 239 | 4 | function enableCompression(): void { |
|
| 240 | 4 | $this->compressionEnabled = true; |
|
| 241 | 4 | } |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Sends a packet to the server. |
||
| 245 | * @param string $packet |
||
| 246 | * @return void |
||
| 247 | */ |
||
| 248 | 60 | function sendPacket(string $packet): void { |
|
| 249 | 60 | $initPacklen = \strlen($packet); |
|
| 250 | |||
| 251 | do { |
||
| 252 | 60 | $partial = \substr($packet, 0, static::CLIENT_MAX_PACKET_SIZE); |
|
| 253 | 60 | $partlen = \strlen($partial); |
|
| 254 | |||
| 255 | 60 | $packet = \substr($packet, static::CLIENT_MAX_PACKET_SIZE); |
|
| 256 | 60 | $packlen = \strlen($packet); |
|
| 257 | |||
| 258 | 60 | $length = \Plasma\BinaryBuffer::writeInt3($partlen); |
|
| 259 | 60 | $sequence = \Plasma\BinaryBuffer::writeInt1((++$this->sequenceID)); |
|
| 260 | |||
| 261 | 60 | $packet = $length.$sequence.$partial; |
|
| 262 | |||
| 263 | 60 | if($this->compressionEnabled && $this->state === static::STATE_OK) { |
|
| 264 | 2 | $packet = $this->compressPacket($packet); |
|
| 265 | } |
||
| 266 | |||
| 267 | 60 | $this->connection->write($packet); |
|
| 268 | 60 | } while($packlen > static::CLIENT_MAX_PACKET_SIZE); |
|
| 269 | |||
| 270 | // If the packet is exactly the max size, we have to send two packets |
||
| 271 | 60 | if($initPacklen === static::CLIENT_MAX_PACKET_SIZE) { |
|
| 272 | 1 | $length = \Plasma\BinaryBuffer::writeInt3(0); |
|
| 273 | 1 | $sequence = \Plasma\BinaryBuffer::writeInt1((++$this->sequenceID)); |
|
| 274 | 1 | $this->connection->write($length.$sequence); |
|
| 275 | } |
||
| 276 | 60 | } |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Sets the parse callback. |
||
| 280 | * @param callable $callback |
||
| 281 | * @return void |
||
| 282 | */ |
||
| 283 | function setParseCallback(callable $callback): void { |
||
| 284 | $this->parseCallback = $callback; |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Processes a command. |
||
| 289 | * @param \Plasma\CommandInterface|null $command |
||
| 290 | * @return void |
||
| 291 | */ |
||
| 292 | 59 | protected function processCommand(?\Plasma\CommandInterface $command = null) { |
|
| 293 | 59 | if($command === null && $this->currentCommand instanceof \Plasma\CommandInterface) { |
|
| 294 | 59 | $command = $this->currentCommand; |
|
| 295 | |||
| 296 | 59 | if($this->currentCommand instanceof \Plasma\Drivers\MySQL\Commands\CommandInterface) { |
|
| 297 | 59 | $state = $command->setParserState(); |
|
| 298 | 59 | if($state !== -1) { |
|
| 299 | 59 | $this->state = $state; |
|
| 300 | } |
||
| 301 | } |
||
| 302 | } |
||
| 303 | |||
| 304 | 59 | if($command === null) { |
|
| 305 | return; |
||
| 306 | } |
||
| 307 | |||
| 308 | 59 | if(!($command instanceof \Plasma\Drivers\MySQL\Commands\CommandInterface) || $command->resetSequence()) { |
|
| 309 | 56 | $this->sequenceID = -1; |
|
| 310 | 56 | $this->compressionID = -1; |
|
| 311 | } |
||
| 312 | |||
| 313 | 59 | $this->sendPacket($command->getEncodedMessage()); |
|
| 314 | |||
| 315 | 59 | if($command !== $this->currentCommand || !$command->waitForCompletion()) { |
|
| 316 | 35 | $command->onComplete(); |
|
| 317 | |||
| 318 | 35 | if($command === $this->currentCommand) { |
|
| 319 | 35 | $this->currentCommand = null; |
|
| 320 | } |
||
| 321 | } |
||
| 322 | 59 | } |
|
| 323 | |||
| 324 | /** |
||
| 325 | * Processes the buffer. |
||
| 326 | * @return void |
||
| 327 | */ |
||
| 328 | 59 | protected function processBuffer() { |
|
| 440 | 59 | } |
|
| 441 | |||
| 442 | /** |
||
| 443 | * Handles an incoming message. |
||
| 444 | * @param \Plasma\BinaryBuffer $buffer |
||
| 445 | * @param \Plasma\Drivers\MySQL\Messages\MessageInterface $message |
||
| 446 | * @return void |
||
| 447 | */ |
||
| 448 | 59 | function handleMessage(\Plasma\BinaryBuffer $buffer, \Plasma\Drivers\MySQL\Messages\MessageInterface $message) { |
|
| 512 | 36 | }); |
|
| 513 | } |
||
| 514 | 59 | } |
|
| 515 | |||
| 516 | /** |
||
| 517 | * Compresses a packet. |
||
| 518 | * @param string $packet |
||
| 519 | * @return string |
||
| 520 | */ |
||
| 521 | 2 | protected function compressPacket(string $packet): string { |
|
| 522 | 2 | $length = \strlen($packet); |
|
| 523 | 2 | $packetlen = \Plasma\BinaryBuffer::writeInt3($length); |
|
| 524 | 2 | $id = \Plasma\BinaryBuffer::writeInt1((++$this->compressionID)); |
|
| 525 | |||
| 526 | 2 | if($length < $this->compressionSizeThreshold) { |
|
| 527 | 2 | return $packetlen.$id.\Plasma\BinaryBuffer::writeInt3(0).$packet; |
|
| 528 | } |
||
| 529 | |||
| 530 | $compressed = \zlib_encode($packet, \ZLIB_ENCODING_DEFLATE); |
||
| 531 | $compresslen = \Plasma\BinaryBuffer::writeInt3(\strlen($compressed)); |
||
| 532 | |||
| 533 | return $comrpesslen.$id.$compressed; |
||
| 534 | } |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Decompresses the buffer. |
||
| 538 | * @return void |
||
| 539 | */ |
||
| 540 | 2 | protected function decompressBuffer(): void { |
|
| 541 | 2 | $buffer = new \Plasma\BinaryBuffer(); |
|
| 542 | |||
| 543 | // Copy packet header to new buffer |
||
| 544 | 2 | for($i = 0; $i < 7; $i++) { |
|
| 545 | 2 | $buffer->append($this->compressionBuffer[$i]); |
|
| 546 | } |
||
| 547 | |||
| 548 | 2 | $length = $buffer->readInt3(); |
|
| 549 | 2 | $this->compressionID = $buffer->readInt1(); |
|
| 550 | 2 | $uncompressedLength = $buffer->readInt3(); |
|
| 551 | |||
| 552 | 2 | if(($this->compressionBuffer->getSize() - 7) < $length) { |
|
| 553 | return; |
||
| 554 | } |
||
| 555 | |||
| 556 | 2 | $this->compressionBuffer->read(7); |
|
| 557 | 2 | $buffer = null; |
|
|
1 ignored issue
–
show
|
|||
| 558 | |||
| 559 | 2 | if($uncompressedLength === 0) { |
|
| 560 | 2 | $this->buffer->append($this->compressionBuffer->read($length)); |
|
| 561 | 2 | return; |
|
| 562 | } |
||
| 563 | |||
| 564 | 1 | $rawPacket = $this->compressionBuffer->read($length); |
|
| 565 | 1 | $packet = \zlib_decode($rawPacket, $uncompressedLength); |
|
| 566 | |||
| 567 | 1 | if(\strlen($packet) !== $uncompressedLength) { |
|
| 568 | $packet = "\xFF\x00\x00\x00 Invalid compressed packet"; |
||
| 569 | $this->connection->end($packet); |
||
| 570 | |||
| 571 | return; |
||
| 572 | } |
||
| 573 | |||
| 574 | 1 | $this->buffer->append($packet); |
|
| 575 | |||
| 576 | 1 | if($this->compressionBuffer->getSize() > 7) { |
|
| 577 | $this->decompressBuffer(); |
||
| 578 | } |
||
| 579 | 1 | } |
|
| 580 | |||
| 581 | /** |
||
| 582 | * Adds the events to the connection. |
||
| 583 | * @return void |
||
| 584 | */ |
||
| 585 | 60 | protected function addEvents() { |
|
| 586 | $this->connection->on('data', function ($chunk) { |
||
| 587 | 59 | if($this->compressionEnabled && $this->state === static::STATE_OK) { |
|
| 588 | 2 | $this->compressionBuffer->append($chunk); |
|
| 589 | |||
| 590 | 2 | if($this->compressionBuffer->getSize() > 7) { |
|
| 591 | 2 | $this->decompressBuffer(); |
|
| 592 | } |
||
| 593 | } else { |
||
| 594 | 59 | $this->buffer->append($chunk); |
|
| 595 | } |
||
| 596 | |||
| 597 | 59 | $this->processBuffer(); |
|
| 598 | 60 | }); |
|
| 599 | |||
| 600 | $this->connection->on('close', function () { |
||
| 601 | 3 | $this->handleClose(); |
|
| 602 | 60 | }); |
|
| 603 | 60 | } |
|
| 604 | |||
| 605 | /** |
||
| 606 | * Connection close handler. |
||
| 607 | * @return void |
||
| 608 | */ |
||
| 609 | 3 | protected function handleClose() { |
|
| 617 | 3 | } |
|
| 618 | } |
||
| 619 |