Completed
Push — master ( cb8ad2...cd16c8 )
by Cristiano
02:41
created

AbstractList::findIndex()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the Phootwork package.
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 *
7
 * @license MIT License
8
 * @copyright Thomas Gossmann
9
 */
10
namespace phootwork\collection;
11
12
use phootwork\lang\Comparator;
13
use phootwork\lang\parts\EachPart;
14
use phootwork\lang\parts\IndexFindersPart;
15
use phootwork\lang\parts\ReducePart;
16
use phootwork\lang\parts\RemovePart;
17
use phootwork\lang\parts\ReversePart;
18
19
/**
20
 * Abstract class for all list-like collections
21
 *
22
 * @author Thomas Gossmann
23
 * @author Cristiano Cinotti
24
 */
25
abstract class AbstractList extends AbstractCollection {
26
	use EachPart;
27
	use IndexFindersPart {
28
		indexOf as traitIndexOf;
29
		findLastIndex as traitFindLastIndex;
30
		findIndex as traitFindIndex;
31
	}
32
	use ReducePart;
33
	use RemovePart;
34
	use ReversePart;
35
36
	/**
37
	 * Sorts the collection in reverse order
38
	 * 
39
	 * @see #sort
40
	 * @see #reverse
41
	 *
42
	 * @param Comparator|callable $cmp
43
	 *
44
	 * @return $this
45
	 */
46 1
	public function reverseSort($cmp = null): self {
47 1
		return $this->sort($cmp)->reverse();
48
	}
49
50
	/**
51
	 * Returns the index of the given element or null if the element can't be found
52
	 *
53
	 * @param mixed $element
54
	 *
55
	 * @return int the index for the given element
56
	 */
57 2
	public function indexOf($element): ?int {
58 2
		$index = $this->traitIndexOf($element);
59
60 2
		return $index === null ? $index : (int) $index;
61
	}
62
63
	/**
64
	 * Searches the array with a given callback and returns the index for the last element if found.
65
	 *
66
	 * The callback function takes one or two parameters:
67
	 *
68
	 *     function ($element [, $query]) {}
69
	 *
70
	 * The callback must return a boolean
71
	 * When it's passed, $query must be the first argument:
72
	 *
73
	 *     - find($query, callback)
74
	 *     - find(callback)
75
	 *
76
	 * @param array $arguments
77
	 *
78
	 * @return int|null the index or null if it hasn't been found
79
	 */
80 1
	public function findLastIndex(...$arguments): ?int {
81 1
		$lastIndex = $this->traitFindLastIndex(...$arguments);
82
83 1
		return $lastIndex === null ? $lastIndex : (int) $lastIndex;
84
	}
85
86
	/**
87
	 * Searches the array with a given callback and returns the index for the first element if found.
88
	 *
89
	 * The callback function takes one or two parameters:
90
	 *
91
	 *     function ($element [, $query]) {}
92
	 *
93
	 * The callback must return a boolean
94
	 * When it's passed, $query must be the first argument:
95
	 *
96
	 *     - find($query, callback)
97
	 *     - find(callback)
98
	 *
99
	 * @param array $arguments
100
	 *
101
	 * @return int|null the index or null if it hasn't been found
102
	 */
103 1
	public function findIndex(...$arguments): ?int {
104 1
		$index = $this->traitFindIndex(...$arguments);
105
106 1
		return $index === null ? $index : (int) $index;
107
	}
108
}
109