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.

Collection::count()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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 Countable;
13
use F500\Equatable\Exceptions\InvalidArgumentException;
14
use F500\Equatable\Exceptions\OutOfRangeException;
15
use IteratorAggregate;
16
use Traversable;
17
18
/**
19
 * @copyright Copyright (c) 2015 Future500 B.V.
20
 * @author    Jasper N. Brouwer <[email protected]>
21
 */
22
abstract class Collection implements Equatable, Countable, IteratorAggregate
23
{
24
    protected $items = [];
25
26 63
    public function getIterator(): Traversable
27
    {
28 63
        return new ArrayIterator($this->items);
29
    }
30
31 78
    public function isEmpty(): bool
32
    {
33 78
        return !$this->items;
34
    }
35
36 282
    public function count(): int
37
    {
38 282
        return count($this->items);
39
    }
40
41 57
    public function countItem($value): int
42
    {
43 57
        $count = 0;
44
45 57
        foreach ($this->items as $item) {
46 57
            if ($this->theseAreEqual($item, $value)) {
47 57
                $count++;
48
            }
49
        }
50
51 57
        return $count;
52
    }
53
54 51
    public function contains($value): bool
55
    {
56
        try {
57 51
            $this->search($value);
58 42
            return true;
59 45
        } catch (OutOfRangeException $e) {
60 45
            return false;
61
        }
62
    }
63
64
    abstract public function search($value);
0 ignored issues
show
Documentation introduced by
For interfaces and abstract methods it is generally a good practice to add a @return annotation even if it is just @return void or @return null, so that implementors know what to do in the overridden method.

For interface and abstract methods, it is impossible to infer the return type from the immediate code. In these cases, it is generally advisible to explicitly annotate these methods with a @return doc comment to communicate to implementors of these methods what they are expected to return.

Loading history...
65
66 66
    public function searchAll($value): Vector
67
    {
68 66
        $foundPointers = [];
69
70 66
        foreach ($this->items as $pointer => $item) {
71 66
            if ($this->theseAreEqual($item, $value)) {
72 66
                $foundPointers[] = $pointer;
73
            }
74
        }
75
76 66
        return new Vector($foundPointers);
77
    }
78
79 6
    public function toArray(): array
80
    {
81 6
        return $this->items;
82
    }
83
84 12
    public function first()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
85
    {
86 12
        if ($this->isEmpty()) {
87 6
            throw OutOfRangeException::doesNotContainAnything();
88
        }
89
90 6
        return reset($this->items);
91
    }
92
93 12
    public function last()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
94
    {
95 12
        if ($this->isEmpty()) {
96 6
            throw OutOfRangeException::doesNotContainAnything();
97
        }
98
99 6
        return end($this->items);
100
    }
101
102
    /**
103
     * The reducer callable is given the carry value and an item,
104
     * and should return the value it is reduced to.
105
     *
106
     * function ($carry, $item) {
107
     *     return $carry . $item;
108
     * }
109
     */
110 12
    public function reduce(callable $reducer, $initial = null)
111
    {
112 12
        return array_reduce($this->items, $reducer, $initial);
113
    }
114
115 267
    protected function theseAreEqual($left, $right): bool
116
    {
117 267
        if ($left === $right) {
118 126
            return true;
119
        }
120
121 240
        if ($left instanceof Equatable && $left->equals($right)) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return $left instanceof ... $left->equals($right);.
Loading history...
122 111
            return true;
123
        }
124
125 207
        return false;
126
    }
127
128 462
    protected function guardAgainstInvalidValue($value)
129
    {
130 462
        if (!is_scalar($value) && !is_object($value)) {
131 6
            throw InvalidArgumentException::invalidValueInArray('values', 'scalar or object', $value);
132
        }
133 456
    }
134
}
135