GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 8c0dd9...9491ea )
by Jasper
02:19
created

ImmutableEquatableMap   A

Complexity

Total Complexity 34

Size/Duplication

Total Lines 202
Duplicated Lines 37.13 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 75
loc 202
wmc 34
lcom 1
cbo 4
ccs 82
cts 82
cp 1
rs 9.2

14 Methods

Rating   Name   Duplication   Size   Complexity  
A remove() 9 9 1
A get() 0 8 2
A search() 10 10 3
A __construct() 10 10 3
A __clone() 0 10 2
A add() 0 12 2
A replace() 0 12 2
A searchAll() 16 16 4
A contains() 0 9 2
A containsKey() 0 8 3
B equals() 18 18 5
A getIterator() 0 4 1
A count() 0 4 1
A countItem() 12 12 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
2
3
/**
4
 * @license https://github.com/f500/equatable/blob/master/LICENSE MIT
5
 */
6
7
namespace F500\Equatable;
8
9
use ArrayIterator;
10
11
/**
12
 * @copyright Copyright (c) 2015 Future500 B.V.
13
 * @author    Jasper N. Brouwer <[email protected]>
14
 */
15
class ImmutableEquatableMap implements EquatableMap
16
{
17
    /**
18
     * @var Equatable[]
19
     */
20
    private $items = [];
21
22
    /**
23
     * @param Equatable[] $items
24
     */
25 126 View Code Duplication
    public function __construct(array $items = [])
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...
26
    {
27 126
        foreach ($items as $key => $value) {
28 90
            if (!$value instanceof Equatable) {
29 3
                throw InvalidArgumentException::invalidTypeInArray('items', Equatable::class, $value);
30
            }
31
32 87
            $this->items[$key] = $value;
33 82
        }
34 123
    }
35
36 3
    public function __clone()
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...
37
    {
38 3
        $items = [];
39
40 3
        foreach ($this->items as $key => $item) {
41 3
            $items[$key] = clone $item;
42 2
        }
43
44 3
        $this->items = $items;
45 3
    }
46
47
    /**
48
     * @inheritdoc
49
     */
50 15
    public function add($key, Equatable $value)
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...
51
    {
52 15
        if ($this->containsKey($key)) {
53 3
            throw InRangeException::keyInRange($key);
54
        }
55
56 9
        $items = $this->items;
57
58 9
        $items[$key] = $value;
59
60 9
        return new static($items);
61
    }
62
63
    /**
64
     * @inheritdoc
65
     */
66 9 View Code Duplication
    public function remove(Equatable $value)
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...
67
    {
68 9
        $key   = $this->search($value);
69 9
        $items = $this->items;
70
71 9
        unset($items[$key]);
72
73 9
        return new static($items);
74
    }
75
76
    /**
77
     * @inheritdoc
78
     */
79 12
    public function replace($key, Equatable $value)
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...
80
    {
81 12
        if (!$this->containsKey($key)) {
82 3
            throw OutOfRangeException::keyOutOfRange($key);
83
        }
84
85 6
        $items = $this->items;
86
87 6
        $items[$key] = $value;
88
89 6
        return new static($items);
90
    }
91
92
    /**
93
     * @inheritdoc
94
     */
95 36
    public function get($key)
96
    {
97 36
        if (!$this->containsKey($key)) {
98 3
            throw OutOfRangeException::keyOutOfRange($key);
99
        }
100
101 30
        return $this->items[$key];
102
    }
103
104
    /**
105
     * @inheritdoc
106
     */
107 21 View Code Duplication
    public function search(Equatable $value)
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...
108
    {
109 21
        foreach ($this->items as $key => $item) {
110 18
            if ($item->equals($value)) {
111 18
                return $key;
112
            }
113 14
        }
114
115 6
        throw OutOfRangeException::valueOutOfRange($value);
116
    }
117
118
    /**
119
     * @inheritdoc
120
     */
121 9 View Code Duplication
    public function searchAll(Equatable $value)
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...
122
    {
123 9
        $foundKeys = [];
124
125 9
        foreach ($this->items as $index => $item) {
126 6
            if ($item->equals($value)) {
127 6
                $foundKeys[] = $index;
128 4
            }
129 6
        }
130
131 9
        if (!$foundKeys) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $foundKeys of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
132 3
            throw OutOfRangeException::valueOutOfRange($value);
133
        }
134
135 6
        return $foundKeys;
136
    }
137
138
    /**
139
     * @inheritdoc
140
     */
141 3
    public function contains(Equatable $value)
142
    {
143
        try {
144 3
            $this->search($value);
145 3
            return true;
146 3
        } catch (OutOfRangeException $e) {
147 3
            return false;
148
        }
149
    }
150
151
    /**
152
     * @inheritdoc
153
     */
154 57
    public function containsKey($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...
155
    {
156 57
        if (!is_string($key) && !is_int($key)) {
157 12
            throw InvalidArgumentException::invalidType('key', 'integer or string', $key);
158
        }
159
160 45
        return isset($this->items[$key]);
161
    }
162
163
    /**
164
     * @inheritdoc
165
     */
166 18 View Code Duplication
    public function equals($other)
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...
167
    {
168 18
        if (!$other instanceof static) {
169 3
            return false;
170
        }
171
172 15
        if ($this->count() !== $other->count()) {
173 3
            return false;
174
        }
175
176 12
        foreach ($this->items as $item) {
177 9
            if ($this->countItem($item) !== $other->countItem($item)) {
178 5
                return false;
179
            }
180 8
        }
181
182 9
        return true;
183
    }
184
185
    /**
186
     * @inheritdoc
187
     */
188 12
    public function getIterator()
189
    {
190 12
        return new ArrayIterator($this->items);
191
    }
192
193
    /**
194
     * @inheritdoc
195
     */
196 42
    public function count()
197
    {
198 42
        return count($this->items);
199
    }
200
201
    /**
202
     * @inheritdoc
203
     */
204 12 View Code Duplication
    public function countItem(Equatable $value)
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...
205
    {
206 12
        $count = 0;
207
208 12
        foreach ($this->items as $item) {
209 12
            if ($item->equals($value)) {
210 12
                $count++;
211 8
            }
212 8
        }
213
214 12
        return $count;
215
    }
216
}
217