first()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 4
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace BrenoRoosevelt;
5
6
/**
7
 * Returns the first element that matches the specification or a default value
8
 *
9
 * @param iterable $items The collection
10
 * @param callable $callback The specification must return a boolean
11
 * @param mixed $default default value
12
 * @param int $mode [optional] <p>
13
 * Flag determining what arguments are sent to <i>callback</i>:
14
 * </p><ul>
15
 * <li>
16
 * <b>CALLBACK_USE_VALUE</b> - <b>default</b> pass value as the only argument
17
 * </li>
18
 * <li>
19
 * <b>CALLBACK_USE_KEY</b> - pass key as the only argument
20
 * to <i>callback</i> instead of the value</span>
21
 * </li>
22
 * <li>
23
 * <b>CALLBACK_USE_BOTH</b> - pass both value and key as
24
 * arguments to <i>callback</i></span>
25
 * </li>
26
 * </ul>
27
 * @return mixed return the corresponding element or default value
28
 */
29
function first(iterable $items, callable $callback, $default = null, int $mode = CALLBACK_USE_VALUE)
30
{
31
    foreach ($items as $key => $value) {
32
        if (true === call_user_func_array($callback, __args($mode, $key, $value))) {
33
            return $value;
34
        }
35
    }
36
37
    return $default;
38
}
39