Pool   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 192
Duplicated Lines 34.38 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 66
loc 192
rs 10
c 0
b 0
f 0
wmc 26
lcom 1
cbo 1

9 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 4 1
A set() 14 17 4
A add() 13 14 4
A delete() 0 13 3
A replace() 13 14 4
A append() 13 14 4
A prepend() 13 14 4
A stats() 0 4 1
A getConfigDefaults() 0 15 1

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
/**
5
 * @package    Network clients
6
 * @subpackage MemcacheClient
7
 * @author     Vasily Zorin <[email protected]>
8
 */
9
class Pool extends \PHPDaemon\Network\Client
10
{
11
12
    /**
13
     * Gets the key
14
     * @param  string $key Key
15
     * @param  callable $onResponse Callback called when response received
16
     * @callback $onResponse ( )
17
     * @return void
18
     */
19
    public function get($key, $onResponse)
20
    {
21
        $this->requestByKey($key, 'get ' . $this->config->prefix->value . $key . "\r\n", $onResponse);
0 ignored issues
show
Bug introduced by
The property prefix does not seem to exist in PHPDaemon\Config\Section.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
22
    }
23
24
    /**
25
     * Sets the key
26
     * @param  string $key Key
27
     * @param  string $value Value
28
     * @param  integer $exp Lifetime in seconds (0 - immortal)
29
     * @param  callable $onResponse Callback called when the request complete
0 ignored issues
show
Documentation introduced by
Should the type for parameter $onResponse not be callable|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
30
     * @callback $onResponse ( )
31
     * @return void
32
     */
33 View Code Duplication
    public function set($key, $value, $exp = 0, $onResponse = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
34
    {
35
        $this->getConnectionByKey($key, function ($conn) use ($key, $value, $exp, $onResponse) {
36
            if (!$conn->connected) {
37
                return;
38
            }
39
            if ($onResponse !== null) {
40
                $conn->onResponse->push($onResponse);
41
                $conn->checkFree();
42
            }
43
44
            $conn->writeln(
45
                'set ' . $this->config->prefix->value . $key . ' 0 ' . $exp . ' '
0 ignored issues
show
Bug introduced by
The property prefix does not seem to exist in PHPDaemon\Config\Section.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
46
                . mb_orig_strlen($value) . ($onResponse === null ? ' noreply' : '') . "\r\n" . $value
47
            );
48
        });
49
    }
50
51
    /**
52
     * Adds the key
53
     * @param  string $key Key
54
     * @param  string $value Value
55
     * @param  integer $exp Lifetime in seconds (0 - immortal)
56
     * @param  callable $onResponse Callback called when the request complete
0 ignored issues
show
Documentation introduced by
Should the type for parameter $onResponse not be callable|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
57
     * @callback $onResponse ( )
58
     * @return void
59
     */
60 View Code Duplication
    public function add($key, $value, $exp = 0, $onResponse = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
61
    {
62
        $this->getConnectionByKey($key, function ($conn) use ($key, $value, $exp, $onResponse) {
63
            if (!$conn->connected) {
64
                return;
65
            }
66
            if ($onResponse !== null) {
67
                $conn->onResponse->push($onResponse);
68
                $conn->checkFree();
69
            }
70
            $conn->writeln('add ' . $this->config->prefix->value . $key . ' 0 ' . $exp . ' ' . mb_orig_strlen($value)
0 ignored issues
show
Bug introduced by
The property prefix does not seem to exist in PHPDaemon\Config\Section.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
71
                . ($onResponse === null ? ' noreply' : '') . "\r\n" . $value);
72
        });
73
    }
74
75
    /**
76
     * Deletes the key
77
     * @param  string $key Key
78
     * @param  callable $onResponse Callback called when the request complete
0 ignored issues
show
Documentation introduced by
Should the type for parameter $onResponse not be callable|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
79
     * @param  integer $time Time to block this key
80
     * @callback $onResponse ( )
81
     * @return void
82
     */
83
    public function delete($key, $onResponse = null, $time = 0)
84
    {
85
        $this->getConnectionByKey($key, function ($conn) use ($key, $time, $onResponse) {
86
            if (!$conn->connected) {
87
                return;
88
            }
89
            if ($onResponse !== null) {
90
                $conn->onResponse->push($onResponse);
91
                $conn->checkFree();
92
            }
93
            $conn->writeln('delete ' . $this->config->prefix->value . $key . ' ' . $time);
0 ignored issues
show
Bug introduced by
The property prefix does not seem to exist in PHPDaemon\Config\Section.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
94
        });
95
    }
96
97
    /**
98
     * Replaces the key
99
     * @param  string $key Key
100
     * @param  string $value Value
101
     * @param  integer $exp Lifetime in seconds (0 - immortal)
102
     * @param  callable $onResponse Callback called when the request complete
0 ignored issues
show
Documentation introduced by
Should the type for parameter $onResponse not be callable|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
103
     * @callback $onResponse ( )
104
     * @return void
105
     */
106 View Code Duplication
    public function replace($key, $value, $exp = 0, $onResponse = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
107
    {
108
        $this->getConnectionByKey($key, function ($conn) use ($key, $value, $exp, $onResponse) {
109
            if (!$conn->connected) {
110
                return;
111
            }
112
            if ($onResponse !== null) {
113
                $conn->onResponse->push($onResponse);
114
                $conn->checkFree();
115
            }
116
            $conn->writeln('replace ' . $this->config->prefix->value . $key . ' 0 ' . $exp . ' ' . mb_orig_strlen($value)
0 ignored issues
show
Bug introduced by
The property prefix does not seem to exist in PHPDaemon\Config\Section.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
117
                . ($onResponse === null ? ' noreply' : '') . "\r\n" . $value);
118
        });
119
    }
120
121
    /**
122
     * Appends a string to the key's value
123
     * @param  string $key Key
124
     * @param  string $value Value
125
     * @param  integer $exp Lifetime in seconds (0 - immortal)
126
     * @param  callable $onResponse Callback called when the request complete
0 ignored issues
show
Documentation introduced by
Should the type for parameter $onResponse not be callable|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
127
     * @callback $onResponse ( )
128
     * @return void
129
     */
130 View Code Duplication
    public function append($key, $value, $exp = 0, $onResponse = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
131
    {
132
        $this->getConnectionByKey($key, function ($conn) use ($key, $value, $exp, $onResponse) {
133
            if (!$conn->connected) {
134
                return;
135
            }
136
            if ($onResponse !== null) {
137
                $conn->onResponse->push($onResponse);
138
                $conn->checkFree();
139
            }
140
            $conn->writeln('replace ' . $this->config->prefix->value . $key . ' 0 ' . $exp . ' ' . mb_orig_strlen($value)
0 ignored issues
show
Bug introduced by
The property prefix does not seem to exist in PHPDaemon\Config\Section.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
141
                . ($onResponse === null ? ' noreply' : '') . "\r\n" . $value);
142
        });
143
    }
144
145
    /**
146
     * Prepends a string to the key's value
147
     * @param  string $key Key
148
     * @param  string $value Value
149
     * @param  integer $exp Lifetime in seconds (0 - immortal)
150
     * @param  callable $onResponse Callback called when the request complete
0 ignored issues
show
Documentation introduced by
Should the type for parameter $onResponse not be callable|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
151
     * @callback $onResponse ( )
152
     * @return void
153
     */
154 View Code Duplication
    public function prepend($key, $value, $exp = 0, $onResponse = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
155
    {
156
        $this->getConnectionByKey($key, function ($conn) use ($key, $value, $exp, $onResponse) {
157
            if (!$conn->connected) {
158
                return;
159
            }
160
            if ($onResponse !== null) {
161
                $conn->onResponse->push($onResponse);
162
                $conn->setFree(false);
163
            }
164
            $conn->writeln('prepend ' . $this->config->prefix->value . $key . ' 0 ' . $exp . ' ' . mb_orig_strlen($value)
0 ignored issues
show
Bug introduced by
The property prefix does not seem to exist in PHPDaemon\Config\Section.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
165
                . ($onResponse === null ? ' noreply' : '') . "\r\n" . $value);
166
        });
167
    }
168
169
    /**
170
     * Gets a statistics
171
     * @param  callable $onResponse Callback called when the request complete
172
     * @param  string $server Server
0 ignored issues
show
Documentation introduced by
Should the type for parameter $server not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
173
     * @return void
174
     */
175
    public function stats($onResponse, $server = null)
176
    {
177
        $this->requestByServer($server, 'stats' . "\r\n", $onResponse);
178
    }
179
180
    /**
181
     * Setting default config options
182
     * Overriden from NetworkClient::getConfigDefaults
183
     * @return array|bool
184
     */
185
    protected function getConfigDefaults()
186
    {
187
        return [
188
            /* [string|array] Default servers */
189
            'servers' => 'tcp://127.0.0.1',
190
191
            /* [integer] Default port */
192
            'port' => 11211,
193
194
            /* [integer] Maximum connections per server */
195
            'maxconnperserv' => 32,
196
197
            'prefix' => '',
198
        ];
199
    }
200
}
201