Passed
Push — master ( f41c92...a01d47 )
by Bao
06:46
created

H::value()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace BaoPham\DynamoDb;
4
5
/**
6
 * Class H
7
 *
8
 * Short for "Helper".
9
 * We often get breaking changes from Laravel Helpers, so to ensure this won't happen again, we port the helpers here.
10
 *
11
 * @package BaoPham\DynamoDb
12
 */
13
// phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps
14
class H
15
{
16 80
    public static function array_first($array, callable $callback = null, $default = null)
17
    {
18 80
        if (is_null($callback)) {
19 2
            if (empty($array)) {
20
                return static::value($default);
21
            }
22 2
            foreach ($array as $item) {
23 2
                return $item;
24
            }
25
        }
26 78
        foreach ($array as $key => $value) {
27 78
            if (call_user_func($callback, $value, $key)) {
28 37
                return $value;
29
            }
30
        }
31 60
        return static::value($default);
32
    }
33
34 60
    public static function value($value)
35
    {
36 60
        return $value instanceof \Closure ? $value() : $value;
37
    }
38
}
39
// phpcs:enable Squiz.Classes.ValidClassName.NotCamelCaps
40