Completed
Push — master ( 940547...961f28 )
by Dmitry
03:23
created

Converter::toObject()   B

Complexity

Conditions 11
Paths 19

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 11.0207

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 17
cts 18
cp 0.9444
rs 7.3166
c 0
b 0
f 0
cc 11
nc 19
nop 1
crap 11.0207

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 Basis;
4
5
use Carbon\Carbon;
6
use Exception;
7
use stdClass;
8
use Tarantool\Mapper\Entity;
9
10
class Converter
11
{
12 45
    public function isTuple($array)
13
    {
14 45
        if (is_object($array)) {
15
            $array = get_object_vars($array);
16
        }
17 45
        if (!is_array($array)) {
18 10
            return false;
19
        }
20 45
        if (version_compare(PHP_VERSION, '7.3.0') >= 0) {
21
            return array_key_last($array) === count($array) - 1;
22
        }
23 45
        return !count($array) || array_values($array) == $array;
24
    }
25
26 45
    public function toObject($data)
27
    {
28 45
        if (is_array($data)) {
29 45
            if ($this->isTuple($data)) {
30 45
                return $this->convertArrayToObject($data);
31
            }
32
        }
33
34 45
        if (is_object($data)) {
35 5
            if ($data instanceof Entity) {
36
                // keep instance
37
                return $data;
38
            }
39
40 5
            $tmp = $data;
41 5
            $data = [];
42 5
            foreach ($tmp as $k => $v) {
43 2
                $data[$k] = $v;
44
            }
45
        }
46
47 45
        $data = (object) $data;
48
49 45
        foreach ($data as $k => $v) {
50 45
            if (is_array($v) && $this->isTuple($v)) {
51 45
                $data->$k = $this->convertArrayToObject($v);
52 45
            } elseif (is_array($v) || is_object($v)) {
53 45
                $data->$k = $this->toObject($v);
54
            }
55
        }
56
57 45
        return $data;
58
    }
59
60 45
    public function convertArrayToObject($data)
61
    {
62 45
        $result = [];
63 45
        foreach ($data as $k => $v) {
64 10
            if ($this->isTuple($v)) {
65
                $result[$k] = $this->convertArrayToObject($v);
66 10
            } elseif (is_object($v) || is_array($v)) {
67
                $result[$k] = $this->toObject($v);
68
            } else {
69 10
                $result[$k] = $v;
70
            }
71
        }
72 45
        return $result;
73
    }
74
75 11
    public function toArray($data) : array
76
    {
77 11
        if (!$data) {
78 5
            return [];
79
        }
80
81 11
        if (is_object($data)) {
82 7
            $data = get_object_vars($data);
83
        }
84
85 11
        foreach ($data as $k => $v) {
86 11
            if (is_array($v) || is_object($v)) {
87 11
                $data[$k] = $this->toArray($v);
88
            }
89
        }
90
91 11
        return $data;
92
    }
93
94
    private $underscores = [];
95
96 1
    public function toUnderscore(string $input) : string
97
    {
98 1
        if (!array_key_exists($input, $this->underscores)) {
99 1
            preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches);
100 1
            $ret = $matches[0];
101 1
            foreach ($ret as &$match) {
102 1
                $match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match);
103
            }
104 1
            $this->underscores[$input] = implode('_', $ret);
105
        }
106 1
        return $this->underscores[$input];
107
    }
108
109
    private $camelcased = [];
110
111 1
    public function toCamelCase(string $string, bool $capitalize = false) : string
112
    {
113 1
        $capitalize = $capitalize ? 1 : 0;
114
115 1
        if (!array_key_exists($capitalize, $this->camelcased)) {
116 1
            $this->camelcased[$capitalize] = [];
117
        }
118
119 1
        if (!array_key_exists($string, $this->camelcased[$capitalize])) {
120 1
            $chain = explode('_', $string);
121 1
            foreach ($chain as $index => $word) {
122 1
                $chain[$index] = $index || $capitalize ? ucfirst($word) : $word;
123
            }
124
125 1
            $this->camelcased[$capitalize][$string] = implode('', $chain);
126
        }
127
128 1
        return $this->camelcased[$capitalize][$string];
129
    }
130
131
132
    private $dates = [];
133
134 1
    public function getDate($string)
135
    {
136 1
        $key = $string;
137 1
        if (func_num_args() == 3) {
138 1
            $key = implode('.', func_get_args());
139
        }
140
141 1
        if (Carbon::hasTestNow() || !array_key_exists($key, $this->dates)) {
142 1
            if (strlen($string) == 8 && is_numeric($string)) {
143 1
                $value = Carbon::createFromFormat('Ymd', $string)->setTime(0, 0, 0);
144 1
            } elseif (func_num_args() == 3) {
145 1
                [$year, $month, $day] = func_get_args();
0 ignored issues
show
Bug introduced by
The variable $year does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $month does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $day does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
146 1
                $value = Carbon::createFromDate($year, $month, $day)->setTime(0, 0, 0);
147
            } else {
148 1
                $value = Carbon::parse($string);
149
            }
150 1
            if (Carbon::hasTestNow()) {
151 1
                return $value;
152
            }
153 1
            $this->dates[$key] = $value;
154
        }
155 1
        return $this->dates[$key]->copy();
156
    }
157
158 1
    public function getTimestamp($string)
159
    {
160 1
        return $this->getDate($string)->timestamp;
161
    }
162
}
163