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 ( db3255...800171 )
by Jasper
14:47
created

Collection::countItem()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 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
    public function getIterator(): Traversable
27
    {
28
        return new ArrayIterator($this->items);
29
    }
30
31
    public function isEmpty(): bool
32
    {
33
        return !$this->items;
34
    }
35
36
    public function count(): int
37
    {
38
        return count($this->items);
39
    }
40
41
    public function countItem($value): int
42
    {
43
        $count = 0;
44
45
        foreach ($this->items as $item) {
46
            if ($this->theseAreEqual($item, $value)) {
47
                $count++;
48
            }
49
        }
50
51
        return $count;
52
    }
53
54
    public function contains($value): bool
55
    {
56
        try {
57
            $this->search($value);
58
            return true;
59
        } catch (OutOfRangeException $e) {
60
            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
    public function searchAll($value): Vector
67
    {
68
        $foundKeys = [];
69
70
        foreach ($this->items as $pointer => $item) {
71
            if ($this->theseAreEqual($item, $value)) {
72
                $foundKeys[] = $pointer;
73
            }
74
        }
75
76
        return new Vector($foundKeys);
77
    }
78
79
    /**
80
     * The reducer callable is given the carry value and an item,
81
     * and should return the value it is reduced to.
82
     *
83
     * function ($carry, $item) {
84
     *     return $carry . $item;
85
     * }
86
     */
87
    public function reduce(callable $reducer, $initial = null)
88
    {
89
        return array_reduce($this->items, $reducer, $initial);
90
    }
91
92
    protected function theseAreEqual($left, $right): bool
93
    {
94
        if ($left === $right) {
95
            return true;
96
        }
97
98
        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...
99
            return true;
100
        }
101
102
        return false;
103
    }
104
105
    protected function guardAgainstInvalidValue($value): void
106
    {
107
        if (!is_scalar($value) && !is_object($value)) {
108
            throw InvalidArgumentException::invalidValueTypeInArray('values', 'scalar or object', $value);
109
        }
110
    }
111
}
112