ArrayAccessTrait   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 0
loc 52
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A offsetExists() 0 4 1
A offsetGet() 0 10 2
A offsetSet() 0 4 1
A offsetUnset() 0 4 1
1
<?php
2
/**
3
 * File ArrayAccessTrait.php
4
 *
5
 * @author Edward Pfremmer <[email protected]>
6
 */
7
namespace Epfremme\Collection\Traits;
8
9
/**
10
 * Trait ArrayAccessTrait
11
 *
12
 * @property array|mixed[] $elements
13
 * @package Epfremme\Collection\Traits
14
 */
15
trait ArrayAccessTrait
16
{
17
    /**
18
     * Test if offset exists in collection
19
     *
20
     * @param $offset
21
     * @return bool
22
     */
23 11
    public function offsetExists($offset)
24
    {
25 11
        return array_key_exists($offset, $this->elements);
26
    }
27
28
    /**
29
     * Return collection element at $offset
30
     *
31
     * @return mixed
32
     */
33 9
    public function offsetGet($offset)
34
    {
35 9
        if (!$this->offsetExists($offset)) {
36 1
            throw new \InvalidArgumentException(
37 1
                sprintf('Offset %s does not exist in collection', $offset)
38 1
            );
39
        }
40
41 8
        return $this->elements[$offset];
42
    }
43
44
    /**
45
     * Collection offset to set
46
     *
47
     * @param mixed $offset
48
     * @param mixed $value
49
     * @return void
50
     */
51 3
    public function offsetSet($offset, $value)
52
    {
53 3
        $this->elements[$offset] = $value;
54 3
    }
55
56
    /**
57
     * Collection offset to unset
58
     *
59
     * @param mixed $offset
60
     * @return void
61
     */
62 2
    public function offsetUnset($offset)
63
    {
64 2
        unset($this->elements[$offset]);
65 2
    }
66
}
67