Conditions | 51 |
Paths | 0 |
Total Lines | 218 |
Code Lines | 133 |
Lines | 20 |
Ratio | 9.17 % |
Changes | 6 | ||
Bugs | 2 | Features | 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 |
||
401 | public function stdin($buf) |
||
402 | { |
||
403 | $this->buf .= $buf; |
||
404 | |||
405 | if ($this->pool->config->protologging->value) { |
||
406 | Daemon::log('Server --> Client: ' . Debug::exportBytes($buf) . "\n\n"); |
||
407 | } |
||
408 | |||
409 | start: |
||
410 | |||
411 | $this->buflen = mb_orig_strlen($this->buf); |
||
412 | |||
413 | if ($this->buflen < 5) { |
||
414 | // Not enough data buffered yet |
||
415 | return; |
||
416 | } |
||
417 | |||
418 | $type = mb_orig_substr($this->buf, 0, 1); |
||
419 | |||
420 | list(, $length) = unpack('N', mb_orig_substr($this->buf, 1, 4)); |
||
421 | $length -= 4; |
||
422 | |||
423 | if ($this->buflen < 5 + $length) { |
||
424 | // Not enough data buffered yet |
||
425 | return; |
||
426 | } |
||
427 | |||
428 | $packet = mb_orig_substr($this->buf, 5, $length); |
||
429 | $this->buf = mb_orig_substr($this->buf, 5 + $length); |
||
430 | |||
431 | if ($type === 'R') { |
||
432 | // Authentication request |
||
433 | list(, $authType) = unpack('N', $packet); |
||
434 | |||
435 | if ($authType === 0) { |
||
436 | // Successful |
||
437 | if ($this->pool->config->protologging->value) { |
||
438 | Daemon::log(__CLASS__ . ': auth. ok.'); |
||
439 | } |
||
440 | |||
441 | $this->state = self::STATE_AUTH_OK; |
||
442 | |||
443 | foreach ($this->onConnected as $cb) { |
||
444 | $cb($this, true); |
||
445 | } |
||
446 | } // @todo move to constant values |
||
447 | View Code Duplication | elseif ($authType === 2) { |
|
448 | // KerberosV5 |
||
449 | Daemon::log(__CLASS__ . ': Unsupported authentication method: KerberosV5.'); |
||
450 | $this->state = self::STATE_AUTH_ERROR; // Auth. error |
||
451 | $this->finish(); // Unsupported, finish |
||
452 | } elseif ($authType === 3) { |
||
453 | // Cleartext |
||
454 | $this->sendPacket('p', $this->password); // Password Message |
||
455 | $this->state = self::STATE_AUTH_PACKET_SENT; |
||
456 | } elseif ($authType === 4) { |
||
457 | // Crypt |
||
458 | $salt = mb_orig_substr($packet, 4, 2); |
||
459 | $this->sendPacket('p', crypt($this->password, $salt)); // Password Message |
||
460 | $this->state = self::STATE_AUTH_PACKET_SENT; |
||
461 | } elseif ($authType === 5) { |
||
462 | // MD5 |
||
463 | $salt = mb_orig_substr($packet, 4, 4); |
||
464 | $this->sendPacket('p', 'md5' . md5(md5($this->password . $this->user) . $salt)); // Password Message |
||
465 | $this->state = self::STATE_AUTH_PACKET_SENT; |
||
466 | View Code Duplication | } elseif ($authType === 6) { |
|
467 | // SCM |
||
468 | Daemon::log(__CLASS__ . ': Unsupported authentication method: SCM.'); |
||
469 | $this->state = self::STATE_AUTH_ERROR; // Auth. error |
||
470 | $this->finish(); // Unsupported, finish |
||
471 | } elseif ($authType === 9) { |
||
472 | // GSS |
||
473 | Daemon::log(__CLASS__ . ': Unsupported authentication method: GSS.'); |
||
474 | $this->state = self::STATE_AUTH_ERROR; // Auth. error |
||
475 | $this->finish(); // Unsupported, finish |
||
476 | } |
||
477 | } elseif ($type === 'T') { |
||
478 | // Row Description |
||
479 | list(, $numfields) = unpack('n', mb_orig_substr($packet, 0, 2)); |
||
480 | $p = 2; |
||
481 | |||
482 | for ($i = 0; $i < $numfields; ++$i) { |
||
483 | list($name) = $this->decodeNULstrings($packet, 1, $p); |
||
484 | $field = unpack('NtableOID/nattrNo/NdataType/ndataTypeSize/NtypeMod/nformat', |
||
485 | mb_orig_substr($packet, $p, 18)); |
||
486 | $p += 18; |
||
487 | $field['name'] = $name; |
||
488 | $this->resultFields[] = $field; |
||
489 | } |
||
490 | } elseif ($type === 'D') { |
||
491 | // Data Row |
||
492 | list(, $numfields) = unpack('n', mb_orig_substr($packet, 0, 2)); |
||
493 | $p = 2; |
||
494 | $row = []; |
||
495 | |||
496 | for ($i = 0; $i < $numfields; ++$i) { |
||
497 | list(, $length) = unpack('N', mb_orig_substr($packet, $p, 4)); |
||
498 | $p += 4; |
||
499 | |||
500 | if ($length === 0xffffffff) { |
||
501 | // hack |
||
502 | $length = -1; |
||
503 | } |
||
504 | |||
505 | if ($length === -1) { |
||
506 | $value = null; |
||
507 | } else { |
||
508 | $value = mb_orig_substr($packet, $p, $length); |
||
509 | $p += $length; |
||
510 | } |
||
511 | |||
512 | $row[$this->resultFields[$i]['name']] = $value; |
||
513 | } |
||
514 | |||
515 | $this->resultRows[] = $row; |
||
516 | } elseif ($type === 'G' || $type === 'H') { |
||
517 | // Copy in response |
||
518 | // The backend is ready to copy data from the frontend to a table; see Section 45.2.5. |
||
519 | if ($this->pool->config->protologging->value) { |
||
520 | Daemon::log(__CLASS__ . ': Caught CopyInResponse'); |
||
521 | } |
||
522 | } elseif ($type === 'C') { |
||
523 | // Close command |
||
524 | $type = mb_orig_substr($packet, 0, 1); |
||
525 | |||
526 | if ($type === 'S' || $type === 'P') { |
||
527 | list($name) = $this->decodeNULstrings(mb_orig_substr($packet, 1)); |
||
528 | } else { |
||
529 | $tag = $this->decodeNULstrings($packet); |
||
530 | $tag = explode(' ', $tag[0]); |
||
531 | |||
532 | if ($tag[0] === 'INSERT') { |
||
533 | $this->insertId = $tag[1]; |
||
534 | $this->insertNum = $tag[2]; |
||
535 | } elseif ($tag[0] === 'DELETE' || $tag[0] === 'UPDATE' || $tag[0] === 'MOVE' |
||
536 | || $tag[0] === 'FETCH' || $tag[0] === 'COPY' |
||
537 | ) { |
||
538 | $this->affectedRows = $tag[1]; |
||
539 | } |
||
540 | } |
||
541 | |||
542 | $this->onResultDone(); |
||
543 | } elseif ($type === 'n') { |
||
544 | // No Data |
||
545 | $this->onResultDone(); |
||
546 | } elseif ($type === 'E') { |
||
547 | // Error Response |
||
548 | $code = ord($packet); |
||
549 | $message = ''; |
||
550 | |||
551 | foreach ($this->decodeNULstrings(mb_orig_substr($packet, 1), 0xFF) as $p) { |
||
552 | if ($message !== '') { |
||
553 | $message .= ' '; |
||
554 | $p = mb_orig_substr($p, 1); |
||
555 | } |
||
556 | |||
557 | $message .= $p; |
||
558 | } |
||
559 | |||
560 | $this->errno = -1; |
||
561 | $this->errmsg = $message; |
||
562 | |||
563 | View Code Duplication | if ($this->state === self::STATE_AUTH_PACKET_SENT) { |
|
564 | // Auth. error |
||
565 | foreach ($this->onConnected as $cb) { |
||
566 | $cb($this, false); |
||
567 | } |
||
568 | |||
569 | $this->state = self::STATE_AUTH_ERROR; |
||
570 | } |
||
571 | |||
572 | $this->onError(); |
||
573 | |||
574 | if ($this->pool->config->protologging->value) { |
||
575 | Daemon::log(__CLASS__ . ': Error response caught (0x' . dechex($code) . '): ' . $message); |
||
576 | } |
||
577 | } elseif ($type === 'I') { |
||
578 | // Empty Query Response |
||
579 | $this->errno = -1; |
||
580 | $this->errmsg = 'Query was empty'; |
||
581 | $this->onError(); |
||
582 | } elseif ($type === 'S') { |
||
583 | // Portal Suspended |
||
584 | if ($this->pool->config->protologging->value) { |
||
585 | Daemon::log(__CLASS__ . ': Caught PortalSuspended'); |
||
586 | } |
||
587 | } elseif ($type === 'S') { |
||
588 | // Parameter Status |
||
589 | $u = $this->decodeNULstrings($packet, 2); |
||
590 | |||
591 | if (isset($u[0])) { |
||
592 | $this->parameters[$u[0]] = isset($u[1]) ? $u[1] : null; |
||
593 | |||
594 | if ($this->pool->config->protologging->value) { |
||
595 | Daemon::log(__CLASS__ . ': Parameter ' . $u[0] . ' = \'' . $this->parameters[$u[0]] . '\''); |
||
596 | } |
||
597 | } |
||
598 | } elseif ($type === 'K') { |
||
599 | // Backend Key Data |
||
600 | list(, $this->backendKey) = unpack('N', $packet); |
||
601 | $this->backendKey = isset($u[1]) ? $u[1] : null; |
||
602 | |||
603 | if ($this->pool->config->protologging->value) { |
||
604 | Daemon::log(__CLASS__ . ': BackendKey is ' . $this->backendKey); |
||
605 | } |
||
606 | } elseif ($type === 'Z') { |
||
607 | // Ready For Query |
||
608 | $this->status = $packet; |
||
609 | |||
610 | if ($this->pool->config->protologging->value) { |
||
611 | Daemon::log(__CLASS__ . ': Ready For Query. Status: ' . $this->status); |
||
612 | } |
||
613 | } else { |
||
614 | Daemon::log(__CLASS__ . ': Caught message with unsupported type - ' . $type); |
||
615 | } |
||
616 | |||
617 | goto start; |
||
618 | } |
||
619 | |||
682 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.