index_of()   A
last analyzed

Complexity

Conditions 6
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 4
nc 3
nop 3
dl 0
loc 9
rs 9.2222
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace BrenoRoosevelt;
5
6
/**
7
 * Searches the iterable for a given element and returns the first corresponding key (index) if successful
8
 *
9
 * @param  iterable $haystack The iterable collection
10
 * @param  mixed    $element  The searched element
11
 * @param  bool     $strict   If the third parameter strict is set to true then will also check the types of the needle
12
 * @return false|int|string the key for needle if it is found in the array, false otherwise.
13
 */
14
function index_of(iterable $haystack, $element, bool $strict = true)
15
{
16
    foreach ($haystack as $index => $value) {
17
        if (($strict && $element === $value) || (!$strict && $element == $value)) {
18
            return $index;
19
        }
20
    }
21
22
    return false;
23
}
24