Passed
Push — main ( 954f0f...3cf1f8 )
by Sergey
04:03
created

Utils   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 84.21%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 18
c 1
b 0
f 0
dl 0
loc 31
ccs 16
cts 19
cp 0.8421
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 4
    public static function EnsureArray($arr, $kfield = null)
9
    {
10 4
        if (is_array($arr)) {
11
            return $arr;
12
        }
13 4
        $result = [];
14 4
        foreach ($arr as $k => $v) {
15 4
            $key = $k;
16 4
            if ($kfield !== null) {
17 4
                if (is_array($v) && isset($v[$kfield])) {
18 4
                    $key = $v[$kfield];
19
                } elseif (is_object($v) && property_exists($v, $kfield)) {
20
                    $key = $v->$kfield;
21
                }
22
            }
23 4
            $result[$key] = $v;
24
        }
25 4
        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