Collection   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 136
Duplicated Lines 11.76 %

Importance

Changes 0
Metric Value
dl 16
loc 136
rs 10
c 0
b 0
f 0
wmc 14

10 Methods

Rating   Name   Duplication   Size   Complexity  
A next() 0 5 1
A get() 6 6 2
A count() 0 3 1
A valid() 0 6 2
A add() 0 7 2
A key() 0 5 1
A keyExists() 0 3 1
A rewind() 0 3 1
A current() 0 5 1
A delete() 6 6 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
2
3
namespace PhpEarth\Stats;
4
5
/**
6
 * Class Collection.
7
 */
8
class Collection implements \Countable, \Iterator
9
{
10
    /**
11
     * @var array
12
     */
13
    protected $data = [];
14
15
    /**
16
     * Adds object to collection with key.
17
     *
18
     * @param $obj
19
     * @param null $key
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $key is correct as it would always require null to be passed?
Loading history...
20
     *
21
     * @throws \Exception
22
     */
23
    public function add($obj, $key = null)
24
    {
25
        if ($key === null) {
26
            $this->data[] = $obj;
27
        } else {
28
            unset($this->data[$key]);
29
            $this->data[$key] = $obj;
30
        }
31
    }
32
33
    /**
34
     * Deletes object from collection by key.
35
     *
36
     * @param $key
37
     *
38
     * @throws \Exception
39
     */
40 View Code Duplication
    public function delete($key)
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...
41
    {
42
        if (isset($this->data[$key])) {
43
            unset($this->data[$key]);
44
        } else {
45
            throw new \Exception("Invalid key $key.");
46
        }
47
    }
48
49
    /**
50
     * Returns object from collection.
51
     *
52
     * @param $key
53
     *
54
     * @return mixed
55
     *
56
     * @throws \Exception
57
     */
58 View Code Duplication
    public function get($key)
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...
59
    {
60
        if (isset($this->data[$key])) {
61
            return $this->data[$key];
62
        } else {
63
            throw new \Exception("Invalid key $key.");
64
        }
65
    }
66
67
    /**
68
     * Checks if object with given key exists in collection.
69
     *
70
     * @param $key
71
     *
72
     * @return bool
73
     */
74
    public function keyExists($key)
75
    {
76
        return isset($this->data[$key]);
77
    }
78
79
    /**
80
     * Returns number of objects in collection.
81
     *
82
     * @return mixed
83
     */
84
    public function count()
85
    {
86
        return count($this->data);
87
    }
88
89
    /**
90
     * Rewinds the internal pointer of an array to its first object of collection.
91
     */
92
    public function rewind()
93
    {
94
        reset($this->data);
95
    }
96
97
    /**
98
     * Returns the current object in a collection.
99
     *
100
     * @return mixed
101
     */
102
    public function current()
103
    {
104
        $data = current($this->data);
105
106
        return $data;
107
    }
108
109
    /**
110
     * Fetch a key from an collection.
111
     *
112
     * @return mixed
113
     */
114
    public function key()
115
    {
116
        $data = key($this->data);
117
118
        return $data;
119
    }
120
121
    /**
122
     * Advance the internal array pointer of a collection.
123
     *
124
     * @return mixed
125
     */
126
    public function next()
127
    {
128
        $data = next($this->data);
129
130
        return $data;
131
    }
132
133
    /**
134
     * Checks if key is set in the collection.
135
     *
136
     * @return bool
137
     */
138
    public function valid()
139
    {
140
        $key = key($this->data);
141
        $data = ($key !== null && $key !== false);
142
143
        return $data;
144
    }
145
}
146