RequestManager   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 74
Duplicated Lines 32.43 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 32.14%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 24
loc 74
ccs 9
cts 28
cp 0.3214
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A clean() 0 7 1
A add() 0 7 1
A get() 13 17 3
A delete() 11 21 3
A searchByUid() 0 12 2

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
 * @package     Comodojo Spare Parts
8
 * @author      Marco Giovinazzi <[email protected]>
9
 * @license     MIT
10
 *
11
 * LICENSE:
12
 *
13
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
 * THE SOFTWARE.
20
 */
21
22
class RequestManager {
23
24
    private $requests = [];
25
26 63
    public function clean() {
27
28 63
        $this->requests = [];
29
30 63
        return $this;
31
32
    }
33
34 63
    public function add(RpcRequest $request) {
35
36 63
        $this->requests[] = $request;
37
38 63
        return $this;
39
40
    }
41
42 63
    public function get($uid = null) {
43
44 63 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...
45
46 63
            return $this->requests;
47
48
        } else if ( $key = $this->searchByUid($uid) != null ) {
49
50
            return $this->requests[$key];
51
52
        } else {
53
54
            return null;
55
56
        }
57
58
    }
59
60
    public function delete($uid = null) {
61
62
        if ( is_null($uid) ) {
63
64
            $this->requests = arrray();
65
66
            return true;
67
68 View Code Duplication
        } else if ( $key = $this->searchByUid($uid) != null ) {
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...
69
70
            unset($this->requests[$key]);
71
72
            return true;
73
74
        } else {
75
76
            return false;
77
78
        }
79
80
    }
81
82
    private function searchByUid($uid) {
83
84
        $element = array_filter(
85
            $this->requests,
86
            function ($e) use ($uid) {
87
                return $e->getUid() == $uid;
88
            }
89
        );
90
91
        return sizeof($element) == 1 ? array_keys($element)[0] : null;
92
93
    }
94
95
}
96