Completed
Push — master ( 1e032b...d01fce )
by Nate
02:22
created

AbstractList   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 53
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A indexOf() 0 6 2
A lastIndexOf() 0 9 2
A subList() 0 10 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
    /**
52
     * Returns a new ListInterface ranging from $fromIndex inclusive to
53
     * $toIndex exclusive
54
     *
55
     * @param int $fromIndex
56
     * @param int $toIndex
57
     * @return ListInterface
58
     * @throws \OutOfBoundsException If to or from index does not exist
59
     */
60 4
    public function subList(int $fromIndex, int $toIndex): ListInterface
61
    {
62 4
        if (!$this->has($fromIndex) || !$this->has($toIndex - 1)) {
63 2
            throw new OutOfBoundsException('Unable to create sub list as $toIndex or $fromIndex do not exist in list');
64
        }
65
66 2
        $newList = array_slice($this->toArray(), $fromIndex, $toIndex - $fromIndex);
67
68 2
        return new static($newList);
0 ignored issues
show
Unused Code introduced by
The call to AbstractList::__construct() has too many arguments starting with $newList.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
69
    }
70
}
71