Completed
Push — master ( 6bd4a8...c4104c )
by Ducatel
03:37
created

AbstractCollection::contains()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 5
cp 0
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 1
crap 12
1
<?php
2
3
namespace Ducatel\PHPCollection\Base;
4
5
abstract class AbstractCollection implements \IteratorAggregate, \Countable
6
{
7
    /**
8
     * @var The array where data are stored internally
9
     */
10
    protected $data = [];
11
12
    /**
13
     * Check if an object is present in the collection.
14
     * The check will be done by `===`
15
     * @param $objectToFind The object you want to check if it's already present.
16
     * @return bool True if present else false
17
     */
18
    public function contains($objectToFind) : bool
19
    {
20
        foreach ($this as $elem) {
21
            if ($elem === $objectToFind) {
22
                return true;
23
            }
24
        }
25
        return false;
26
    }
27
28
    /**
29
     * Count elements of an object
30
     *
31
     * @link  http://php.net/manual/en/countable.count.php
32
     * @return int The custom count as an integer.
33
     *        </p>
34
     *        <p>
35
     *        The return value is cast to an integer.
36
     * @since 5.1.0
37
     */
38 1
    public function count()
39
    {
40 1
        return count($this->data);
41
    }
42
43
44
    /**
45
     * Retrieve an external iterator
46
     *
47
     * @link  http://php.net/manual/en/iteratoraggregate.getiterator.php
48
     * @return \Traversable An instance of an object implementing <b>Iterator</b> or
49
     *        <b>Traversable</b>
50
     * @since 5.0.0
51
     */
52 8
    public function getIterator()
53
    {
54 8
        return new \ArrayIterator($this->data);
55
    }
56
}
57