Completed
Push — master ( 1f3396...4932db )
by Cristiano
02:20
created

IndexFindersPart::findLastIndex()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 2
rs 10
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
11
namespace phootwork\lang\parts;
12
13
trait IndexFindersPart {
14
15
	/**
16
	 * Returns the index of the given element or false if the element can't be found
17
	 *
18
	 * @param mixed $element
19
	 * @return int the index for the given element
20
	 */
21 4
	public function indexOf($element): ?int {
22 4
		$out = array_search($element, $this->array, true);
23
24 4
		return false === $out ? null : $out;
0 ignored issues
show
Bug Best Practice introduced by
The expression return false === $out ? null : $out could return the type string which is incompatible with the type-hinted return integer|null. Consider adding an additional type-check to rule them out.
Loading history...
25
	}
26
27
	/**
28
	 * Searches the array with a given callback and returns the index for the last element if found.
29
	 *
30
	 * The callback function takes one or two parameters:
31
	 *
32
	 *     function ($element [, $query]) {}
33
	 *
34
	 * The callback must return a boolean
35
	 * When it's passed, $query must be the first argument:
36
	 *
37
	 *     - find($query, callback)
38
	 *     - find(callback)
39
	 *
40
	 * @param array $arguments
41
	 * @return int|null the index or null if it hasn't been found
42
	 */
43 2
	public function findLastIndex(...$arguments): ?int {
44 2
		$index = count($arguments) === 1 ?
45 2
			$this->findLast($arguments[0]) : $this->findLast($arguments[0], $arguments[1]);
0 ignored issues
show
Bug introduced by
The method findLast() does not exist on phootwork\lang\parts\IndexFindersPart. Did you maybe mean findLastIndex()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

45
			$this->/** @scrutinizer ignore-call */ 
46
          findLast($arguments[0]) : $this->findLast($arguments[0], $arguments[1]);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
46
47 2
		return $this->indexOf($index);
48
	}
49
50
	/**
51
	 * Searches the array with a given callback and returns the index for the first element if found.
52
	 *
53
	 * The callback function takes one or two parameters:
54
	 *
55
	 *     function ($element [, $query]) {}
56
	 *
57
	 * The callback must return a boolean
58
	 * When it's passed, $query must be the first argument:
59
	 *
60
	 *     - find($query, callback)
61
	 *     - find(callback)
62
	 *
63
	 * @param array $arguments
64
	 * @return int|null the index or null if it hasn't been found
65
	 */
66 2
	public function findIndex(...$arguments): ?int {
67 2
		$index = count($arguments) === 1 ? $this->find($arguments[0]) : $this->find($arguments[0], $arguments[1]);
0 ignored issues
show
Bug introduced by
It seems like find() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

67
		$index = count($arguments) === 1 ? $this->/** @scrutinizer ignore-call */ find($arguments[0]) : $this->find($arguments[0], $arguments[1]);
Loading history...
68
69 2
		return $this->indexOf($index);
70
	}
71
}
72