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

Connection   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 91
Duplicated Lines 8.79 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 3
Bugs 2 Features 0
Metric Value
c 3
b 2
f 0
dl 8
loc 91
rs 10
wmc 16
lcom 1
cbo 2

1 Method

Rating   Name   Duplication   Size   Complexity  
F onRead() 8 44 16

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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