AbstractCollection::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
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 3
    public function contains($objectToFind) : bool
19
    {
20 3
        foreach ($this as $elem) {
21 3
            if ($elem === $objectToFind) {
22 3
                return true;
23
            }
24
        }
25 3
        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 4
    public function count()
39
    {
40 4
        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 13
    public function getIterator()
53
    {
54 13
        return new \ArrayIterator($this->data);
55
    }
56
}
57