Completed
Push — master ( 2b2467...86fe2f )
by Marco
12:06
created

Request   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 91
Duplicated Lines 26.37 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
c 1
b 0
f 0
lcom 1
cbo 1
dl 24
loc 91
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setAutoclean() 0 7 1
A getAutoclean() 0 5 1
A clean() 0 7 1
A addRequest() 0 9 1
A getRequest() 13 17 3
A deleteRequest() 11 21 3

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 namespace Comodojo\RpcClient\Components;
2
3
use \Comodojo\RpcClient\RpcRequest;
4
use \Exception;
5
6
/**
7
 * Protocol Trait
8
 *
9
 * @package     Comodojo Spare Parts
10
 * @author      Marco Giovinazzi <[email protected]>
11
 * @license     MIT
12
 *
13
 * LICENSE:
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
 * THE SOFTWARE.
22
 */
23
24
trait Request {
25
26
    /**
27
     * Autoclean requests
28
     *
29
     * @var string
30
     */
31
    private $autoclean = true;
32
33
    private $requests = array();
34
35
    /**
36
     * Set autoclean on/off
37
     *
38
     * @param   bool   $mode  If true, requests will be removed from queue at each send()
39
     *
40
     * @return  \Comodojo\RpcClient\RpcClient
41
     */
42
    public function setAutoclean($mode = true) {
43
44
        $this->autoclean = filter_var($mode, FILTER_VALIDATE_BOOLEAN);
45
46
        return $this;
47
48
    }
49
50
    public function getAutoclean() {
51
52
        return $this->autoclean;
53
54
    }
55
56
    public function clean() {
57
58
        $this->requests = array();
59
60
        return $this;
61
62
    }
63
64
    public function addRequest(RpcRequest $request) {
65
66
        $uid = $request->getUniqueId();
67
68
        $this->requests[$uid] = $request;
69
70
        return $this;
71
72
    }
73
74
    public function getRequest($uid = null) {
75
76 View Code Duplication
        if ( is_null($uid) ) {
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...
77
78
            return $this->requests;
79
80
        } else if ( array_key_exists($uid, $this->requests) ) {
81
82
            return $this->requests[$uid];
83
84
        } else {
85
86
            return null;
87
88
        }
89
90
    }
91
92
    public function deleteRequest($uid = null) {
93
94
        if ( is_null($uid) ) {
95
96
            $this->requests = arrray();
97
98
            return true;
99
100 View Code Duplication
        } else if ( array_key_exists($uid, $this->requests) ) {
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...
101
102
            unset($this->requests[$uid]);
103
104
            return true;
105
106
        } else {
107
108
            return false;
109
110
        }
111
112
    }
113
114
}
115