AbstractList   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 33
ccs 0
cts 11
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A indexOf() 0 6 2
A lastIndexOf() 0 9 2
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
/**
10
 * Class AbstractList
11
 *
12
 * A skeletal implementation of the [@see ListInterface]
13
 *
14
 * @author Nate Brunette <[email protected]>
15
 */
16
abstract class AbstractList extends AbstractCollection implements ListInterface
17
{
18
    /**
19
     * Returns the index of the first instance of the element, -1 if the element
20
     * doesn't exist
21
     *
22
     * @param mixed $element
23
     * @return int
24
     */
25
    public function indexOf($element): int
26
    {
27
        $index = array_search($element, $this->toArray(), true);
28
29
        return false === $index ? -1 : $index;
30
    }
31
32
    /**
33
     * Returns the index of the last instance of the element, -1 if the element
34
     * doesn't exist
35
     *
36
     * @param mixed $element
37
     * @return int
38
     */
39
    public function lastIndexOf($element): int
40
    {
41
        $elements = array_reverse($this->toArray());
42
43
        $index = array_search($element, $elements, true);
44
45
        // return -1 if not found or (size - index found - 1) to return the index of the element after reverse
46
        return false === $index ? -1 : $this->count() - $index - 1;
47
    }
48
}
49