Passed
Branch version-4 (8b03a3)
by Sebastian
02:18
created

ArrayAccessTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 33.33%

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 46
ccs 3
cts 9
cp 0.3333
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A offsetUnset() 0 3 1
A offsetExists() 0 3 1
A offsetSet() 0 3 1
A offsetGet() 0 3 1
1
<?php
2
declare(strict_types=1);
3
/*
4
 * Copyright (C) 2018 Sebastian Böttger <[email protected]>
5
 * You may use, distribute and modify this code under the
6
 * terms of the MIT license.
7
 *
8
 * You should have received a copy of the MIT license with
9
 * this file. If not, please visit: https://opensource.org/licenses/mit-license.php
10
 */
11
12
namespace Seboettg\Collection\NativePhp;
13
14
/**
15
 * Trait ArrayAccessTrait
16
 * @package Seboettg\Collection\ArrayList
17
 */
18
trait ArrayAccessTrait
19
{
20
21
    /**
22
     * Offset to retrieve
23
     * @link http://php.net/manual/en/arrayaccess.offsetget.php
24
     * @param mixed $offset The offset to retrieve.
25
     *
26
     * @return mixed Can return all value types.
27
     */
28
    public function offsetGet($offset)
29
    {
30
        return $this->array[$offset] ?? null;
31
    }
32
33
    /**
34
     * Offset to set
35
     * @link http://php.net/manual/en/arrayaccess.offsetset.php
36
     * @param mixed $offset The offset to assign the value to.
37
     * @param mixed $value The value to set.
38
     */
39 3
    public function offsetSet($offset, $value)
40
    {
41 3
        $this->array[$offset] = $value;
0 ignored issues
show
Bug Best Practice introduced by
The property array does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
42 3
    }
43
44
    /**
45
     * Whether a offset exists
46
     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
47
     *
48
     * @param mixed $offset
49
     * @return bool
50
     */
51
    public function offsetExists($offset): bool
52
    {
53
        return isset($this->array[$offset]);
54
    }
55
56
    /**
57
     * Offset to unset
58
     * @link http://php.net/manual/en/arrayaccess.offsetunset.php
59
     * @param mixed $offset The offset to unset.
60
     */
61
    public function offsetUnset($offset)
62
    {
63
        unset($this->array[$offset]);
64
    }
65
}
66