Passed
Push — main ( 92895e...7a79c8 )
by Breno
01:59
created

ArrayAccessTrait::offsetExists()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace BrenoRoosevelt\Validation\DataStructure;
5
6
use Exception;
7
8
trait ArrayAccessTrait
9
{
10
    use CollectionTrait;
11
12
    public function offsetExists(mixed $offset): bool
13
    {
14
        return array_key_exists($offset, $this->elements);
15
    }
16
17
    public function offsetGet(mixed $offset): mixed
18
    {
19
        if (!array_key_exists($offset, $this->elements)) {
20
            throw new Exception(sprintf('Undefined offset: %s', $offset));
21
        }
22
23
        return $this->elements[$offset];
24
    }
25
26
    public function offsetSet(mixed $offset, mixed $value): void
27
    {
28
        if ($offset === null) {
29
            $this->elements[] = $value;
30
        } else {
31
            $this->elements[$offset] = $value;
32
        }
33
    }
34
35
    public function offsetUnset(mixed $offset): void
36
    {
37
        unset($this->elements[$offset]);
38
    }
39
}
40