Completed
Push — EventLoopContainer ( c9a84d...6e77fa )
by Vasily
04:04
created

Connection::onRead()   F

Complexity

Conditions 16
Paths 0

Size

Total Lines 44
Code Lines 31

Duplication

Lines 8
Ratio 18.18 %

Importance

Changes 3
Bugs 2 Features 0
Metric Value
cc 16
eloc 31
c 3
b 2
f 0
nc 0
nop 0
dl 8
loc 44
rs 2.7326

How to fix   Complexity   

Long Method

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:

1
<?php
2
namespace PHPDaemon\Clients\Memcache;
3
4
use PHPDaemon\Network\ClientConnection;
5
6
/**
7
 * @package    Network clients
8
 * @subpackage MemcacheClient
9
 * @author     Vasily Zorin <[email protected]>
10
 */
11
class Connection extends ClientConnection
12
{
13
    /**
14
     * DESCR
15
     */
16
    const STATE_DATA = 1;
17
18
    /**
19
     * @var mixed current result
20
     */
21
    public $result;
22
23
    /**
24
     * @var string flags of incoming value
25
     */
26
    public $valueFlags;
27
28
    /**
29
     * @var integer length of incoming value
30
     */
31
    public $valueLength;
32
33
    /**
34
     * @var string error message
35
     */
36
    public $error;
37
38
    /**
39
     * @var string current incoming key
40
     */
41
    public $key;
42
43
    /**
44
     * @var string
45
     */
46
    protected $EOL = "\r\n";
47
48
    /**
49
     * @var integer
50
     */
51
    protected $maxQueue = 10;
52
53
    /**
54
     * Called when new data received
55
     * @return void
56
     */
57
    protected function onRead()
58
    {
59
        start:
60
        if ($this->state === self::STATE_ROOT) {
61
            while (($l = $this->readLine()) !== null) {
62
                $e = explode(' ', $l);
63
64
                if ($e[0] === 'VALUE') {
65
                    $this->key = $e[1];
66
                    $this->valueFlags = $e[2];
67
                    $this->valueLength = $e[3];
68
                    $this->result = '';
69
                    $this->setWatermark($this->valueLength);
70
                    $this->state = self::STATE_DATA;
71
                    break;
72
                } elseif ($e[0] === 'STAT') {
73
                    if ($this->result === null) {
74
                        $this->result = [];
75
                    }
76
77
                    $this->result[$e[1]] = $e[2];
78
                } elseif (($e[0] === 'STORED') || ($e[0] === 'END') || ($e[0] === 'DELETED') || ($e[0] === 'ERROR')
79
                    || ($e[0] === 'CLIENT_ERROR') || ($e[0] === 'SERVER_ERROR')
80
                ) {
81
                    if ($e[0] !== 'END') {
82
                        $this->result = false;
83
                        $this->error = isset($e[1]) ? $e[1] : null;
84
                    }
85
                    $this->onResponse->executeOne($this);
86
                    $this->checkFree();
87
                    $this->result = null;
88
                }
89
            }
90
        }
91
92 View Code Duplication
        if ($this->state === self::STATE_DATA) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
93
            if (false === ($this->result = $this->readExact($this->valueLength))) {
94
                return; //we do not have a whole packet
95
            }
96
            $this->state = self::STATE_ROOT;
97
            $this->setWatermark(1);
98
            goto start;
99
        }
100
    }
101
}
102