Passed
Push — master ( 18efe9...9cdba3 )
by George
04:43
created

ObjectConvertor::toArray()   C

Complexity

Conditions 13
Paths 14

Size

Total Lines 49
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 13.6148

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 27
c 1
b 0
f 0
nc 14
nop 2
dl 0
loc 49
ccs 22
cts 26
cp 0.8462
crap 13.6148
rs 6.6166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace ag84ark\ObjectConvertor;
4
5
use Illuminate\Support\Str;
6
7
class ObjectConvertor
8
{
9 24
    public static function toObject(array $array, $object)
10
    {
11 24
        $class = get_class($object);
12
13 24
        $methods = get_class_methods($class);
14
15 24
        foreach ($methods as $method) {
16 24
            preg_match('/^(set)(.*?)$/i', $method, $results);
17
18 24
            $pre = $results[1] ?? '';
19
20 24
            $k = $results[2] ?? '';
21
22 24
            $k = strtolower(substr($k, 0, 1)).substr($k, 1);
23 24
            $k2 = Str::snake($k);
24
25 24
            if ('set' === $pre && ! empty($array[$k])) {
26 18
                $object->$method($array[$k]);
27 24
            } elseif ('set' === $pre && ! empty($array[$k2])) {
28 4
                $object->$method($array[$k2]);
29 24
            } elseif ('set' === $pre && ! empty($array[Str::ucfirst($k)])) {
30 13
                $object->$method($array[Str::ucfirst($k)]);
31
            }
32
        }
33
34 24
        return $object;
35
    }
36
37
    /**
38
     * @param $object
39
     */
40 12
    public static function toObjectBaseModelApi(array $array, $object): BaseModelApi
41
    {
42 12
        $class = get_class($object);
43
44 12
        $methods = get_class_methods($class);
45
46 12
        foreach ($methods as $method) {
47 12
            preg_match('/^(set)(.*?)$/i', $method, $results);
48
49 12
            $pre = $results[1] ?? '';
50
51 12
            $k = $results[2] ?? '';
52
53 12
            $k = strtolower(substr($k, 0, 1)).substr($k, 1);
54
55 12
            $k2 = Str::snake($k);
56
57 12
            if ('set' === $pre && ! empty($array[$k])) {
58 12
                $object->$method($array[$k]);
59 12
            } elseif ('set' === $pre && ! empty($array[$k2])) {
60
                $object->$method($array[$k2]);
61 12
            } elseif ('set' === $pre && ! empty($array[Str::ucfirst($k)])) {
62 6
                $object->$method($array[Str::ucfirst($k)]);
63
            }
64
        }
65
66 12
        return $object;
67
    }
68
69 14
    public static function toArray($object, bool $withNulls = true): array
70
    {
71 14
        $array = [];
72
73 14
        $class = get_class($object);
74
75 14
        $methods = get_class_methods($class);
76
77 14
        foreach ($methods as $method) {
78 14
            preg_match(' /^(get)(.*?)$/i', $method, $results);
79
80 14
            $pre = $results[1] ?? '';
81
82 14
            $k = $results[2] ?? '';
83
84 14
            $k = strtolower(substr($k, 0, 1)).substr($k, 1);
85
86 14
            if ('get' == $pre) {
87 14
                $array[$k] = $object->$method();
88 14
                if (is_object($array[$k])) {
89 4
                    if (method_exists($array[$k], 'toArray')) {
90 4
                        $array[$k] = $array[$k]->toArray($withNulls, $withNulls);
91
                    } else {
92 4
                        $array[$k] = self::toArray($array[$k], $withNulls);
93
                    }
94
                }
95 14
                if (is_array($array[$k])) {
96 4
                    foreach ($array[$k] as $key => $value) {
97 4
                        if (is_object($array[$k][$key])) {
98
                            if (method_exists($array[$k][$key], 'toArray')) {
99
                                $array[$k][$key] = $array[$k][$key]->toArray($withNulls);
100
                            } else {
101
                                $array[$k][$key] = self::toArray($array[$k][$key], $withNulls);
102
                            }
103
104
                            if (false === $withNulls && null === $array[$k][$key]) {
105 2
                                unset($array[$k][$key]);
106
                            }
107
                        }
108
                    }
109
                }
110
                // Remove null values if option is set
111 14
                if (false === $withNulls && null === $array[$k]) {
112 8
                    unset($array[$k]);
113
                }
114
            }
115
        }
116
117 14
        return $array;
118
    }
119
}
120