iterable_min()   B
last analyzed

Complexity

Conditions 8
Paths 10

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 8

Importance

Changes 0
Metric Value
cc 8
eloc 10
nc 10
nop 2
dl 0
loc 17
ccs 9
cts 9
cp 1
crap 8
rs 8.4444
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Improved;
6
7
/**
8
 * Get the minimal element according to a given comparator.
9
 *
10
 * @param iterable      $iterable
11
 * @param callable|null $compare
12
 * @return mixed
13
 */
14
function iterable_min(iterable $iterable, callable $compare = null)
0 ignored issues
show
introduced by
Function Improved\iterable_min() has parameter $iterable with no value type specified in iterable type iterable.
Loading history...
15
{
16 32
    $first = true;
17 32
    $min = null;
18
19 32
    if (!isset($compare)) {
20 25
        foreach ($iterable as $value) {
21 24
            $min = (!isset($min) || $value < $min) ? $value : $min;
22
        }
23
    } else {
24 7
        foreach ($iterable as $value) {
25 7
            $min = ($first || call_user_func($compare, $min, $value) > 0) ? $value : $min;
26 7
            $first = false;
27
        }
28
    }
29
30 32
    return $min;
31
}
32