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 ( beb630...0f844e )
by Jasper
11:36
created

EquatableVector   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 158
Duplicated Lines 47.47 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 31
lcom 1
cbo 1
dl 75
loc 158
rs 9.8
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 10 10 3
A __clone() 0 10 2
A add() 0 8 1
A remove() 9 9 1
A replace() 0 12 2
A get() 0 8 2
A search() 10 10 3
A searchAll() 16 16 4
A contains() 0 9 2
A containsIndex() 0 4 1
B equals() 18 18 5
A count() 0 4 1
A countItem() 12 12 3
A getIterator() 0 4 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
3
/**
4
 * @license https://github.com/f500/equatable/blob/master/LICENSE MIT
5
 */
6
7
declare(strict_types=1);
8
9
namespace F500\Equatable;
10
11
use ArrayIterator;
12
use Countable;
13
use IteratorAggregate;
14
use Traversable;
15
16
/**
17
 * @copyright Copyright (c) 2015 Future500 B.V.
18
 * @author    Jasper N. Brouwer <[email protected]>
19
 */
20
final class EquatableVector implements Equatable, Countable, IteratorAggregate
21
{
22
    /**
23
     * @var Equatable[]
24
     */
25
    private $items = [];
26
27
    /**
28
     * @param Equatable[] $items
29
     */
30 View Code Duplication
    public function __construct(array $items = [])
1 ignored issue
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...
31
    {
32
        foreach ($items as $item) {
33
            if (!$item instanceof Equatable) {
34
                throw InvalidArgumentException::invalidTypeInArray('items', Equatable::class, $item);
35
            }
36
37
            $this->items[] = $item;
38
        }
39
    }
40
41
    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...
42
    {
43
        $items = [];
44
45
        foreach ($this->items as $item) {
46
            $items[] = clone $item;
47
        }
48
49
        $this->items = $items;
50
    }
51
52
    public function add(Equatable $value): EquatableVector
53
    {
54
        $items = $this->items;
55
56
        $items[] = $value;
57
58
        return new static($items);
59
    }
60
61 View Code Duplication
    public function remove(Equatable $value): EquatableVector
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...
62
    {
63
        $index = $this->search($value);
64
        $items = $this->items;
65
66
        unset($items[$index]);
67
68
        return new static($items);
69
    }
70
71
    public function replace(int $index, Equatable $value): EquatableVector
72
    {
73
        if (!$this->containsIndex($index)) {
74
            throw OutOfRangeException::indexOutOfRange($index);
75
        }
76
77
        $items = $this->items;
78
79
        $items[$index] = $value;
80
81
        return new static($items);
82
    }
83
84
    public function get(int $index): Equatable
85
    {
86
        if (!$this->containsIndex($index)) {
87
            throw OutOfRangeException::indexOutOfRange($index);
88
        }
89
90
        return $this->items[$index];
91
    }
92
93 View Code Duplication
    public function search(Equatable $value): int
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...
94
    {
95
        foreach ($this->items as $index => $item) {
96
            if ($item->equals($value)) {
97
                return $index;
98
            }
99
        }
100
101
        throw OutOfRangeException::valueOutOfRange($value);
102
    }
103
104 View Code Duplication
    public function searchAll(Equatable $value): array
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...
105
    {
106
        $foundIndexes = [];
107
108
        foreach ($this->items as $index => $item) {
109
            if ($item->equals($value)) {
110
                $foundIndexes[] = $index;
111
            }
112
        }
113
114
        if (!$foundIndexes) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $foundIndexes 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...
115
            throw OutOfRangeException::valueOutOfRange($value);
116
        }
117
118
        return $foundIndexes;
119
    }
120
121
    public function contains(Equatable $value): bool
122
    {
123
        try {
124
            $this->search($value);
125
            return true;
126
        } catch (OutOfRangeException $e) {
127
            return false;
128
        }
129
    }
130
131
    public function containsIndex(int $index): bool
132
    {
133
        return isset($this->items[$index]);
134
    }
135
136 View Code Duplication
    public function equals($other): bool
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...
137
    {
138
        if (!$other instanceof static) {
139
            return false;
140
        }
141
142
        if ($this->count() !== $other->count()) {
143
            return false;
144
        }
145
146
        foreach ($this->items as $item) {
147
            if ($this->countItem($item) !== $other->countItem($item)) {
148
                return false;
149
            }
150
        }
151
152
        return true;
153
    }
154
155
    public function count(): int
156
    {
157
        return count($this->items);
158
    }
159
160 View Code Duplication
    public function countItem(Equatable $value): int
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...
161
    {
162
        $count = 0;
163
164
        foreach ($this->items as $item) {
165
            if ($item->equals($value)) {
166
                $count++;
167
            }
168
        }
169
170
        return $count;
171
    }
172
173
    public function getIterator(): Traversable
174
    {
175
        return new ArrayIterator($this->items);
176
    }
177
}
178