Conditions | 51 |
Paths | 0 |
Total Lines | 210 |
Lines | 12 |
Ratio | 5.71 % |
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 |
||
423 | public function onRead() |
||
424 | { |
||
425 | start: |
||
426 | if ($this->finished) { |
||
427 | return; |
||
428 | } |
||
429 | $l = $this->getInputLength(); |
||
430 | if ($l < 5) { |
||
431 | return; // Not enough data buffered yet |
||
432 | } |
||
433 | $type = $this->look(1); |
||
434 | |||
435 | list(, $length) = unpack('N', $this->look(4, 1)); |
||
436 | |||
437 | $length -= 4; |
||
438 | |||
439 | if ($l < $length + 5) { |
||
440 | // Not enough data buffered yet |
||
441 | return; |
||
442 | } |
||
443 | |||
444 | $this->drain(5); |
||
445 | |||
446 | $packet = $this->read($length); |
||
447 | |||
448 | if ($type === 'R') { |
||
449 | // Authentication request |
||
450 | list(, $authType) = unpack('N', $packet); |
||
451 | if ($this->pool->config->protologging->value) { |
||
452 | Daemon::log('auth type = ' . $authType); |
||
453 | } |
||
454 | if ($authType === 0) { |
||
455 | // Successful |
||
456 | if ($this->pool->config->protologging->value) { |
||
457 | Daemon::log(self::class . ': auth. ok.'); |
||
458 | } |
||
459 | |||
460 | $this->state = self::STATE_AUTH_OK; |
||
461 | |||
462 | $this->connected = true; |
||
463 | $this->onConnected->executeAll($this, true); |
||
464 | if ($this->connected && !$this->busy) { |
||
465 | $this->pool->markConnFree($this, $this->url); |
||
466 | } |
||
467 | } // @todo move to constant values |
||
468 | View Code Duplication | elseif ($authType === 2) { |
|
469 | // KerberosV5 |
||
470 | Daemon::log(self::class . ': Unsupported authentication method: KerberosV5.'); |
||
471 | $this->state = self::STATE_AUTH_ERROR; // Auth. error |
||
472 | $this->finish(); // Unsupported, finish |
||
473 | } elseif ($authType === 3) { |
||
474 | // Cleartext |
||
475 | $this->sendPacket('p', $this->password); // Password Message |
||
476 | $this->state = self::STATE_AUTH_PACKET_SENT; |
||
477 | } elseif ($authType === 4) { |
||
478 | // Crypt |
||
479 | $salt = mb_orig_substr($packet, 4, 2); |
||
480 | $this->sendPacket('p', crypt($this->password, $salt) . "\x00"); // Password Message |
||
481 | $this->state = self::STATE_AUTH_PACKET_SENT; |
||
482 | } elseif ($authType === 5) { |
||
483 | // MD5 |
||
484 | $salt = mb_orig_substr($packet, 4, 4); |
||
485 | $this->sendPacket('p', 'md5' . md5(md5($this->password . $this->user) . $salt) . "\x00"); // Password Message |
||
486 | $this->state = self::STATE_AUTH_PACKET_SENT; |
||
487 | View Code Duplication | } elseif ($authType === 6) { |
|
488 | // SCM |
||
489 | Daemon::log(self::class . ': Unsupported authentication method: SCM.'); |
||
490 | $this->state = self::STATE_AUTH_ERROR; // Auth. error |
||
491 | $this->finish(); // Unsupported, finish |
||
492 | } elseif ($authType === 9) { |
||
493 | // GSS |
||
494 | Daemon::log(self::class . ': Unsupported authentication method: GSS.'); |
||
495 | $this->state = self::STATE_AUTH_ERROR; // Auth. error |
||
496 | $this->finish(); // Unsupported, finish |
||
497 | } |
||
498 | } elseif ($type === 'T') { |
||
499 | // Row Description |
||
500 | list(, $numfields) = unpack('n', mb_orig_substr($packet, 0, 2)); |
||
501 | $p = 2; |
||
502 | |||
503 | for ($i = 0; $i < $numfields; ++$i) { |
||
504 | list($name) = $this->decodeNULstrings($packet, 1, $p); |
||
505 | $field = unpack('NtableOID/nattrNo/NdataType/ndataTypeSize/NtypeMod/nformat', |
||
506 | mb_orig_substr($packet, $p, 18)); |
||
507 | $p += 18; |
||
508 | $field['name'] = $name; |
||
509 | $this->resultFields[] = $field; |
||
510 | } |
||
511 | } elseif ($type === 'D') { |
||
512 | // Data Row |
||
513 | list(, $numfields) = unpack('n', mb_orig_substr($packet, 0, 2)); |
||
514 | $p = 2; |
||
515 | $row = []; |
||
516 | |||
517 | for ($i = 0; $i < $numfields; ++$i) { |
||
518 | list(, $length) = unpack('N', mb_orig_substr($packet, $p, 4)); |
||
519 | $p += 4; |
||
520 | |||
521 | if ($length === 0xffffffff) { |
||
522 | // hack |
||
523 | $length = -1; |
||
524 | } |
||
525 | |||
526 | if ($length === -1) { |
||
527 | $value = null; |
||
528 | } else { |
||
529 | $value = mb_orig_substr($packet, $p, $length); |
||
530 | $p += $length; |
||
531 | } |
||
532 | |||
533 | $row[$this->resultFields[$i]['name']] = $value; |
||
534 | } |
||
535 | |||
536 | $this->resultRows[] = $row; |
||
537 | } elseif ($type === 'G' || $type === 'H') { |
||
538 | // Copy in response |
||
539 | // The backend is ready to copy data from the frontend to a table; see Section 45.2.5. |
||
540 | if ($this->pool->config->protologging->value) { |
||
541 | Daemon::log(self::class . ': Caught CopyInResponse'); |
||
542 | } |
||
543 | } elseif ($type === 'C') { |
||
544 | // Close command |
||
545 | $type = mb_orig_substr($packet, 0, 1); |
||
546 | |||
547 | if ($type === 'S' || $type === 'P') { |
||
548 | list($name) = $this->decodeNULstrings(mb_orig_substr($packet, 1)); |
||
549 | } else { |
||
550 | $tag = $this->decodeNULstrings($packet); |
||
551 | $tag = explode(' ', $tag[0]); |
||
552 | |||
553 | if ($tag[0] === 'INSERT') { |
||
554 | $this->insertId = $tag[1]; |
||
555 | $this->insertNum = (int) $tag[2]; |
||
556 | $this->affectedRows = (int) $tag[2]; |
||
557 | } elseif ($tag[0] === 'DELETE' || $tag[0] === 'UPDATE' || $tag[0] === 'MOVE' |
||
558 | || $tag[0] === 'FETCH' || $tag[0] === 'COPY' |
||
559 | ) { |
||
560 | $this->affectedRows = (int) $tag[1]; |
||
561 | } |
||
562 | } |
||
563 | |||
564 | $this->onResultDone(); |
||
565 | } elseif ($type === 'n') { |
||
566 | // No Data |
||
567 | $this->onResultDone(); |
||
568 | } elseif ($type === 'E') { |
||
569 | // Error Response |
||
570 | $code = ord($packet); |
||
571 | $message = ''; |
||
572 | |||
573 | foreach ($this->decodeNULstrings(mb_orig_substr($packet, 1), 0xFF) as $p) { |
||
574 | if ($message !== '') { |
||
575 | $message .= ' '; |
||
576 | $p = mb_orig_substr($p, 1); |
||
577 | } |
||
578 | |||
579 | $message .= $p; |
||
580 | } |
||
581 | |||
582 | $this->errno = -1; |
||
583 | $this->errmsg = $message; |
||
584 | |||
585 | $this->onError(); |
||
586 | |||
587 | if ($this->pool->config->protologging->value) { |
||
588 | Daemon::log(self::class . ': Error response caught (0x' . dechex($code) . '): ' . $message); |
||
589 | } |
||
590 | } elseif ($type === 'I') { |
||
591 | // Empty Query Response |
||
592 | $this->errno = -1; |
||
593 | $this->errmsg = 'Query was empty'; |
||
594 | $this->onError(); |
||
595 | } elseif ($type === 'S') { |
||
596 | // Portal Suspended |
||
597 | if ($this->pool->config->protologging->value) { |
||
598 | Daemon::log(self::class . ': Caught PortalSuspended'); |
||
599 | } |
||
600 | } elseif ($type === 'S') { |
||
601 | // Parameter Status |
||
602 | $u = $this->decodeNULstrings($packet, 2); |
||
603 | |||
604 | if (isset($u[0])) { |
||
605 | $this->parameters[$u[0]] = isset($u[1]) ? $u[1] : null; |
||
606 | |||
607 | if ($this->pool->config->protologging->value) { |
||
608 | Daemon::log(self::class . ': Parameter ' . $u[0] . ' = \'' . $this->parameters[$u[0]] . '\''); |
||
609 | } |
||
610 | } |
||
611 | } elseif ($type === 'K') { |
||
612 | // Backend Key Data |
||
613 | list(, $this->backendKey) = unpack('N', $packet); |
||
614 | $this->backendKey = isset($u[1]) ? $u[1] : null; |
||
615 | |||
616 | if ($this->pool->config->protologging->value) { |
||
617 | Daemon::log(self::class . ': BackendKey is ' . $this->backendKey); |
||
618 | } |
||
619 | } elseif ($type === 'Z') { |
||
620 | // Ready For Query |
||
621 | $this->status = $packet; |
||
622 | |||
623 | if ($this->pool->config->protologging->value) { |
||
624 | Daemon::log(self::class . ': Ready For Query. Status: ' . $this->status); |
||
625 | } |
||
626 | } else { |
||
627 | Daemon::log(self::class . ': Caught message with unsupported type - ' . $type); |
||
628 | } |
||
629 | |||
630 | goto start; |
||
631 | |||
632 | } |
||
633 | |||
699 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.