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
declare(strict_types = 1);
4
5
/*
6
* This file is part of the StateMachine package
7
*
8
* (c) Michal Wachowski <[email protected]>
9
*
10
* For the full copyright and license information, please view the LICENSE
11
* file that was distributed with this source code.
12
*/
13
14
namespace StateMachine\Collection;
15
16
/**
17
 * Abstract generic Collection
18
 * Used for internal representations
19
 *
20
 * @package StateMachine
21
 */
22
abstract class Collection implements \Countable
23
{
24
    /**
25
     * Collection elements
26
     *
27
     * @var array
28
     */
29
    protected $collection = array();
30
31
    /**
32
     * Return value for given offset
33
     *
34
     * @param string $offset
35
     *
36
     * @return mixed
37
     * @throws OutOfRangeException
38
     */
39 15
    public function get($offset)
40
    {
41 15
        if (!$this->has($offset)) {
42 3
            throw OutOfRangeException::offsetNotFound($offset);
43
        }
44
45 12
        return $this->collection[$offset];
46
    }
47
48
    /**
49
     * Check if there is element for offset
50
     *
51
     * @param string $offset
52
     *
53
     * @return bool
54
     */
55 21
    public function has($offset): bool
56
    {
57 21
        return array_key_exists($offset, $this->collection);
58
    }
59
60
    /**
61
     * Return all elements in collection
62
     *
63
     * @return array
64
     */
65 4
    public function all(): array
66
    {
67 4
        return $this->collection;
68
    }
69
70
    /**
71
     * Count elements of an object
72
     *
73
     * @return int
74
     */
75 2
    public function count(): int
76
    {
77 2
        return count($this->collection);
78
    }
79
}
80