Passed
Push — version-4 ( d98195...afc63d )
by Sebastian
02:40
created

ListAccessTrait::last()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Seboettg\Collection\Lists\ListFeatures;
4
5
/**
6
 * @property array $array
7
 */
8
trait ListAccessTrait
9
{
10
    /**
11
     * {@inheritdoc}
12
     */
13 5
    public function get(int $index)
14
    {
15 5
        return $this->array[$index] ?? null;
16
    }
17
18
    /**
19
     * @param $key
20
     * @param $element
21
     * @return void
22
     */
23
    public function set($key, $element): void
24
    {
25
        $this->array[$key] = $element;
26
    }
27
28
    /**
29
     * Returns the first element
30
     * @return mixed
31
     */
32 1
    public function first()
33
    {
34 1
        if (empty($this->array)) {
35
            return null;
36
        }
37 1
        reset($this->array);
38 1
        return $this->array[key($this->array)];
39
    }
40
41
    /**
42
     * Returns the last element
43
     * @return mixed
44
     */
45 1
    public function last()
46
    {
47 1
        $item = end($this->array);
48 1
        reset($this->array);
49 1
        return $item;
50
    }
51
}
52