Completed
Pull Request — master (#212)
by Alexandru
45:00 queued 03:00
created

H   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Test Coverage

Coverage 66.67%

Importance

Changes 0
Metric Value
wmc 8
eloc 11
dl 0
loc 23
ccs 8
cts 12
cp 0.6667
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A value() 0 3 2
A array_first() 0 16 6
1
<?php
2
3
namespace Rennokki\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 Rennokki\DynamoDb
12
 */
13
class H
14
{
15 5
    public static function array_first($array, callable $callback = null, $default = null)
16
    {
17 5
        if (is_null($callback)) {
18
            if (empty($array)) {
19
                return static::value($default);
20
            }
21
            foreach ($array as $item) {
22
                return $item;
23
            }
24
        }
25 5
        foreach ($array as $key => $value) {
26 5
            if (call_user_func($callback, $value, $key)) {
27 5
                return $value;
28
            }
29
        }
30 2
        return static::value($default);
31
    }
32
33 2
    public static function value($value)
34
    {
35 2
        return $value instanceof \Closure ? $value() : $value;
36
    }
37
}
38