Passed
Branch master (fb2e38)
by Sebastian
02:23
created

ListAccessTrait   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 40
ccs 14
cts 14
cp 1
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 3 1
A last() 0 5 2
A set() 0 3 1
A first() 0 7 2
1
<?php
2
/*
3
 * Copyright (C) 2022 Sebastian Böttger <[email protected]>
4
 * You may use, distribute and modify this code under the
5
 * terms of the MIT license.
6
 *
7
 * You should have received a copy of the MIT license with
8
 * this file. If not, please visit: https://opensource.org/licenses/mit-license.php
9
 */
10
11
namespace Seboettg\Collection\Lists\ListFeatures;
12
13
/**
14
 * @property array $array
15
 */
16
trait ListAccessTrait
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21 6
    public function get(int $index)
22
    {
23 6
        return $this->array[$index] ?? null;
24
    }
25
26
    /**
27
     * @param int $index
28
     * @param mixed $element
29
     * @return void
30
     */
31 1
    public function set(int $index, $element): void
32
    {
33 1
        $this->array[$index] = $element;
34 1
    }
35
36
    /**
37
     * @inheritDoc
38
     */
39 2
    public function first()
40
    {
41 2
        if (empty($this->array)) {
42 1
            return null;
43
        }
44 1
        reset($this->array);
45 1
        return $this->array[key($this->array)];
46
    }
47
48
    /**
49
     * @inheritDoc
50
     */
51 3
    public function last()
52
    {
53 3
        $item = end($this->array);
54 3
        reset($this->array);
55 3
        return $item === false ? null: $item;
56
    }
57
}
58