Passed
Push — master ( 9cfe94...4dbffe )
by Sebastian
02:19
created

ListAccessTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 11
c 2
b 0
f 0
dl 0
loc 48
ccs 16
cts 16
cp 1
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A has() 0 3 1
A get() 0 3 1
A first() 0 7 2
A set() 0 3 1
A last() 0 5 2
1
<?php
2
declare(strict_types=1);
3
/*
4
 * Copyright (C) 2022 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\Lists\ListFeatures;
13
14
/**
15
 * @property array $array
16
 */
17
trait ListAccessTrait
18
{
19
    /**
20
     * {@inheritdoc}
21
     */
22 1
    public function has(int $index): bool
23
    {
24 1
        return array_key_exists($index, $this->array);
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 6
    public function get(int $index)
31
    {
32 6
        return $this->array[$index] ?? null;
33
    }
34
35
    /**
36
     * @param int $index
37
     * @param mixed $element
38
     * @return void
39
     */
40 1
    public function set(int $index, $element): void
41
    {
42 1
        $this->array[$index] = $element;
43 1
    }
44
45
    /**
46
     * @inheritDoc
47
     */
48 2
    public function first()
49
    {
50 2
        if (empty($this->array)) {
51 1
            return null;
52
        }
53 1
        reset($this->array);
54 1
        return $this->array[key($this->array)];
55
    }
56
57
    /**
58
     * @inheritDoc
59
     */
60 3
    public function last()
61
    {
62 3
        $item = end($this->array);
63 3
        reset($this->array);
64 3
        return $item === false ? null: $item;
65
    }
66
}
67