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 ( 9eee7f...86e0d3 )
by Jasper
11:21
created

ImmutableEquatableVector::containsIndex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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