Searching::findThe()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stratadox\ImmutableCollection;
6
7
use function array_search;
8
use function in_array;
9
use function is_object;
10
use Stratadox\Collection\Collection;
11
use Stratadox\Collection\NotFound;
12
13
/**
14
 * Behaviour to allow "searching" the immutable collection.
15
 *
16
 * Provides access to searching behaviour in the form of methods that
17
 * return information on the existence or position of the items in the
18
 * original collection.
19
 *
20
 * @package Stratadox\Collection
21
 * @author  Stratadox
22
 */
23
trait Searching
24
{
25
    /**
26
     * @see Searchable::has()
27
     * @param mixed $item
28
     * @return bool
29
     */
30
    public function has($item): bool
31
    {
32
        return $this->positionOf($item) !== false;
33
    }
34
35
    /**
36
     * @see Searchable::hasThe()
37
     * @param object $object
38
     * @return bool
39
     */
40
    public function hasThe($object): bool
41
    {
42
        return in_array($object, $this->items(), true);
43
    }
44
45
    /**
46
     * @see Searchable::find()
47
     * @param mixed $item
48
     * @return int
49
     * @throws NotFound
50
     */
51
    public function find($item): int
52
    {
53
        $position = $this->positionOf($item);
54
        $this->mustBeValid($position, $item);
55
        return $position;
56
    }
57
58
    /**
59
     * @see Searchable::findThe()
60
     * @param object $object
61
     * @return int
62
     * @throws NotFound
63
     */
64
    public function findThe($object): int
65
    {
66
        $position = array_search($object, $this->items(), true);
67
        $this->mustBeValid($position, $object);
0 ignored issues
show
Bug introduced by
It seems like $position can also be of type string; however, parameter $position of Stratadox\ImmutableColle...earching::mustBeValid() does only seem to accept integer|boolean, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

67
        $this->mustBeValid(/** @scrutinizer ignore-type */ $position, $object);
Loading history...
68
        return $position;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $position could return the type string which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
69
    }
70
71
    /**
72
     * @param mixed $item The item we're looking for.
73
     * @return int|bool   The position or false.
74
     */
75
    private function positionOf($item)
76
    {
77
        if (is_object($item)) {
78
            return array_search($item, $this->items(), false);
0 ignored issues
show
Bug Best Practice introduced by
The expression return array_search($item, $this->items(), false) also could return the type string which is incompatible with the documented return type integer|boolean.
Loading history...
79
        }
80
        return array_search($item, $this->items(), true);
0 ignored issues
show
Bug Best Practice introduced by
The expression return array_search($item, $this->items(), true) also could return the type string which is incompatible with the documented return type integer|boolean.
Loading history...
81
    }
82
83
    /**
84
     * @param int|bool $position            The position or false.
85
     * @param mixed    $whatWeAreLookingFor The item we're looking for.
86
     * @throws NotFound                     When the position is false.
87
     */
88
    private function mustBeValid($position, $whatWeAreLookingFor): void
89
    {
90
        if ($position === false) {
91
            /** @var Collection $this */
92
            throw NoSuchValue::couldNotFind($this, $whatWeAreLookingFor);
93
        }
94
    }
95
96
    /** @see Collection::items() */
97
    abstract public function items(): array;
98
}
99