Passed
Push — main ( 54d11e...e3fc5a )
by Sergey
11:08 queued 06:53
created

Utils   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 18
c 1
b 0
f 0
dl 0
loc 31
ccs 19
cts 19
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A extractField() 0 8 4
B EnsureArray() 0 18 8
1
<?php
2
3
namespace Gurucomkz\EagerLoading;
4
5
class Utils
6
{
7
8 6
    public static function EnsureArray($arr, $kfield = null)
9
    {
10 6
        if (is_array($arr)) {
11 1
            return $arr;
12
        }
13 5
        $result = [];
14 5
        foreach ($arr as $k => $v) {
15 5
            $key = $k;
16 5
            if ($kfield !== null) {
17 5
                if (is_array($v) && isset($v[$kfield])) {
18 4
                    $key = $v[$kfield];
19 1
                } elseif (is_object($v) && property_exists($v, $kfield)) {
20 1
                    $key = $v->$kfield;
21
                }
22
            }
23 5
            $result[$key] = $v;
24
        }
25 5
        return $result;
26
    }
27
28 4
    public static function extractField($arr, $field)
29
    {
30 4
        $result = [];
31 4
        foreach ($arr as $record) {
32 4
            $key = is_object($record) ? $record->ID : $record['ID'];
33 4
            $result[$key] = is_object($record) ? $record->$field : $record[$field];
34
        }
35 4
        return $result;
36
    }
37
}
38