Completed
Push — master ( d01fce...3c3d94 )
by Nate
02:38
created

AbstractList::subList()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 2
nop 2
crap 3
1
<?php
2
/*
3
 * Copyright (c) Nate Brunette.
4
 * Distributed under the MIT License (http://opensource.org/licenses/MIT)
5
 */
6
7
namespace Tebru\Collection;
8
9
use OutOfBoundsException;
10
11
/**
12
 * Class AbstractList
13
 *
14
 * A skeletal implementation of the [@see ListInterface]
15
 *
16
 * @author Nate Brunette <[email protected]>
17
 */
18
abstract class AbstractList extends AbstractCollection implements ListInterface
19
{
20
    /**
21
     * Returns the index of the first instance of the element, -1 if the element
22
     * doesn't exist
23
     *
24
     * @param mixed $element
25
     * @return int
26
     */
27 2
    public function indexOf($element): int
28
    {
29 2
        $index = array_search($element, $this->toArray(), true);
30
31 2
        return false === $index ? -1 : $index;
32
    }
33
34
    /**
35
     * Returns the index of the last instance of the element, -1 if the element
36
     * doesn't exist
37
     *
38
     * @param mixed $element
39
     * @return int
40
     */
41 2
    public function lastIndexOf($element): int
42
    {
43 2
        $elements = array_reverse($this->toArray());
44
45 2
        $index = array_search($element, $elements, true);
46
47
        // return -1 if not found or (size - index found - 1) to return the index of the element after reverse
48 2
        return false === $index ? -1 : $this->count() - $index - 1;
49
    }
50
}
51