Conditions | 33 |
Paths | 48 |
Total Lines | 136 |
Code Lines | 93 |
Lines | 0 |
Ratio | 0 % |
Tests | 56 |
CRAP Score | 79.2122 |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
334 | 62 | protected function processBuffer() { |
|
335 | 62 | if($this->buffer->getSize() < 4) { |
|
336 | return; |
||
337 | } |
||
338 | |||
339 | 62 | $buffer = clone $this->buffer; |
|
340 | |||
341 | 62 | $length = $buffer->readInt3(); |
|
342 | 62 | $this->sequenceID = $buffer->readInt1(); |
|
343 | |||
344 | 62 | if($length === static::CLIENT_MAX_PACKET_SIZE) { |
|
345 | $this->buffer->read(($length + 4)); |
||
346 | $this->messageBuffer->append($buffer->read($length)); |
||
347 | return; |
||
348 | 62 | } elseif($this->messageBuffer->getSize() > 0) { |
|
349 | $this->messageBuffer->append($buffer->read($length)); |
||
350 | $buffer = $this->messageBuffer; |
||
351 | $this->messageBuffer = new \Plasma\BinaryBuffer(); |
||
352 | } |
||
353 | |||
354 | 62 | if($buffer->getSize() < $length) { |
|
355 | return; |
||
356 | } |
||
357 | |||
358 | 62 | if($length > 0) { |
|
359 | 62 | $this->buffer->read(($length + 4)); |
|
360 | 62 | $buffer->slice(0, $length); |
|
361 | } else { |
||
362 | $this->buffer->slice($buffer->getSize()); |
||
363 | } |
||
364 | |||
365 | 62 | if($buffer->getSize() === 0) { |
|
366 | return; |
||
367 | } |
||
368 | |||
369 | /** @var \Plasma\Drivers\MySQL\Messages\MessageInterface $message */ |
||
370 | 62 | $message = null; |
|
371 | |||
372 | 62 | if($this->state === static::STATE_INIT) { |
|
373 | 62 | $message = new \Plasma\Drivers\MySQL\Messages\HandshakeMessage($this); |
|
374 | } else { |
||
375 | 62 | $firstChar = $buffer->read(1); |
|
376 | |||
377 | 62 | $okRespID = \Plasma\Drivers\MySQL\Messages\OkResponseMessage::getID(); |
|
378 | $isOkMessage = ( |
||
379 | ( |
||
380 | 62 | $firstChar === $okRespID && |
|
381 | 61 | (!($this->currentCommand instanceof \Plasma\Drivers\MySQL\Commands\QueryCommand) |
|
382 | 61 | || \strtoupper(\substr($this->currentCommand->getQuery(), 0, 6)) !== 'SELECT') // Fix for MySQL 5.7 |
|
383 | ) || |
||
384 | ( |
||
385 | 48 | $firstChar === \Plasma\Drivers\MySQL\Messages\EOFMessage::getID() && |
|
386 | 62 | ($this->handshakeMessage->capability & \Plasma\Drivers\MySQL\CapabilityFlags::CLIENT_DEPRECATE_EOF) !== 0 |
|
387 | ) |
||
388 | ); |
||
389 | |||
390 | switch(true) { |
||
391 | 62 | case ($firstChar === \Plasma\Drivers\MySQL\Messages\ErrResponseMessage::getID()): |
|
392 | 1 | $message = new \Plasma\Drivers\MySQL\Messages\ErrResponseMessage($this); |
|
393 | 1 | break; |
|
394 | 61 | case ($this->currentCommand instanceof \Plasma\Drivers\MySQL\Commands\StatementPrepareCommand && $firstChar === $okRespID): |
|
395 | 37 | $message = new \Plasma\Drivers\MySQL\Messages\PrepareStatementOkMessage($this); |
|
396 | 37 | break; |
|
397 | 61 | case $isOkMessage: |
|
398 | 61 | $message = new \Plasma\Drivers\MySQL\Messages\OkResponseMessage($this); |
|
399 | 61 | $this->lastOkMessage = $message; |
|
400 | 61 | break; |
|
1 ignored issue
–
show
|
|||
401 | 47 | case ($this->state < static::STATE_OK && $firstChar === \Plasma\Drivers\MySQL\Messages\AuthMoreDataMessage::getID()): |
|
1 ignored issue
–
show
|
|||
402 | $message = new \Plasma\Drivers\MySQL\Messages\AuthMoreDataMessage($this); |
||
403 | break; |
||
1 ignored issue
–
show
|
|||
404 | 47 | case ($this->state < static::STATE_OK && $firstChar === \Plasma\Drivers\MySQL\Messages\AuthSwitchRequestMessage::getID()): |
|
1 ignored issue
–
show
|
|||
405 | $message = new \Plasma\Drivers\MySQL\Messages\AuthSwitchRequestMessage($this); |
||
406 | break; |
||
1 ignored issue
–
show
|
|||
407 | 47 | case ($this->state < static::STATE_OK && $firstChar === \Plasma\Drivers\MySQL\Messages\AuthMoreDataMessage::getID()): |
|
1 ignored issue
–
show
|
|||
408 | $message = new \Plasma\Drivers\MySQL\Messages\AuthMoreDataMessage($this); |
||
409 | break; |
||
410 | 47 | case ($firstChar === \Plasma\Drivers\MySQL\Messages\EOFMessage::getID() && $length < 6): |
|
411 | $message = new \Plasma\Drivers\MySQL\Messages\EOFMessage($this); |
||
412 | break; |
||
1 ignored issue
–
show
|
|||
413 | 47 | case ($firstChar === \Plasma\Drivers\MySQL\Messages\LocalInFileRequestMessage::getID()): |
|
1 ignored issue
–
show
|
|||
414 | if($this->driver->getOptions()['localInFile.enable']) { |
||
1 ignored issue
–
show
|
|||
415 | $message = new \Plasma\Drivers\MySQL\Messages\LocalInFileRequestMessage($this); |
||
416 | } else { |
||
1 ignored issue
–
show
|
|||
417 | $this->emit('error', array((new \Plasma\Exception('MySQL server requested a local file, but the driver options is disabled')))); |
||
418 | |||
419 | if($this->buffer->getSize() > 0) { |
||
1 ignored issue
–
show
|
|||
420 | $this->driver->getLoop()->futureTick(function () { |
||
421 | $this->processBuffer(); |
||
422 | }); |
||
1 ignored issue
–
show
|
|||
423 | } |
||
1 ignored issue
–
show
|
|||
424 | |||
425 | return; |
||
426 | } |
||
1 ignored issue
–
show
|
|||
427 | break; |
||
1 ignored issue
–
show
|
|||
428 | default: |
||
429 | 47 | $buffer->prepend($firstChar); |
|
430 | |||
431 | 47 | if($this->parseCallback !== null) { |
|
432 | $parse = $this->parseCallback; |
||
433 | $this->parseCallback = null; |
||
434 | |||
435 | $caller = new \Plasma\Drivers\MySQL\ProtocolOnNextCaller($this, $buffer); |
||
436 | $parse($caller); |
||
437 | 47 | } elseif($this->currentCommand !== null) { |
|
438 | 47 | $command = $this->currentCommand; |
|
439 | |||
440 | 47 | $caller = new \Plasma\Drivers\MySQL\ProtocolOnNextCaller($this, $buffer); |
|
441 | 47 | $command->onNext($caller); |
|
442 | |||
443 | 47 | if($command->hasFinished()) { |
|
444 | 37 | $this->currentCommand = null; |
|
445 | 37 | $command->onComplete(); |
|
446 | } |
||
447 | } |
||
448 | |||
449 | 47 | if($this->buffer->getSize() > 0) { |
|
450 | $this->driver->getLoop()->futureTick(function () { |
||
451 | 47 | $this->processBuffer(); |
|
452 | 47 | }); |
|
453 | } |
||
454 | |||
455 | 47 | return; |
|
456 | break; |
||
457 | } |
||
458 | } |
||
459 | |||
460 | 62 | $state = $message->setParserState(); |
|
461 | 62 | if($state !== -1) { |
|
462 | 62 | $this->state = $state; |
|
463 | } |
||
464 | |||
465 | 62 | if($message instanceof \Plasma\Drivers\MySQL\Messages\HandshakeMessage) { |
|
466 | 62 | $this->handshakeMessage = $message; |
|
467 | } |
||
468 | |||
469 | 62 | $this->handleMessage($buffer, $message); |
|
470 | 62 | } |
|
649 |