Completed
Push — master ( 184377...e7ee0f )
by Dmitry
05:03
created

Converter   B

Complexity

Total Complexity 46

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 1

Test Coverage

Coverage 94.81%

Importance

Changes 0
Metric Value
wmc 46
lcom 3
cbo 1
dl 0
loc 151
ccs 73
cts 77
cp 0.9481
rs 8.3999
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
B convertArrayToObject() 0 14 5
B toArray() 0 18 6
A toUnderscore() 0 12 4
B toCamelCase() 0 19 7
A isTuple() 0 10 4
C toObject() 0 33 11
C getDate() 0 23 8
A getTimestamp() 0 4 1

How to fix   Complexity   

Complex Class

Complex classes like Converter often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Converter, and based on these observations, apply Extract Interface, too.

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
        return !count($array) || array_keys($array) === range(0, count($array) -1);
21
    }
22
23 45
    public function toObject($data)
24
    {
25 45
        if (is_array($data)) {
26 45
            if ($this->isTuple($data)) {
27 45
                return $this->convertArrayToObject($data);
28
            }
29
        }
30
31 45
        if (is_object($data)) {
32 5
            if ($data instanceof Entity) {
33
                // keep instance
34
                return $data;
35
            }
36
37 5
            $tmp = $data;
38 5
            $data = [];
39 5
            foreach ($tmp as $k => $v) {
40 1
                $data[$k] = $v;
41
            }
42
        }
43
44 45
        $data = (object) $data;
45
46 45
        foreach ($data as $k => $v) {
47 45
            if (is_array($v) && $this->isTuple($v)) {
48 45
                $data->$k = $this->convertArrayToObject($v);
49 45
            } elseif(is_array($v) || is_object($v)) {
50 45
                $data->$k = $this->toObject($v);
51
            }
52
        }
53
54 45
        return $data;
55
    }
56
57 45
    public function convertArrayToObject($data)
58
    {
59 45
        $result = [];
60 45
        foreach ($data as $k => $v) {
61 10
            if ($this->isTuple($v)) {
62
                $result[$k] = $this->convertArrayToObject($v);
63 10
            } elseif (is_object($v) || is_array($v)) {
64
                $result[$k] = $this->toObject($v);
65
            } else {
66 10
                $result[$k] = $v;
67
            }
68
        }
69 45
        return $result;
70
    }
71
72 11
    public function toArray($data) : array
73
    {
74 11
        if (!$data) {
75 5
            return [];
76
        }
77
78 11
        if (is_object($data)) {
79 7
            $data = get_object_vars($data);
80
        }
81
82 11
        foreach ($data as $k => $v) {
83 11
            if (is_array($v) || is_object($v)) {
84 11
                $data[$k] = $this->toArray($v);
85
            }
86
        }
87
88 11
        return $data;
89
    }
90
91
    private $underscores = [];
92
93 1
    public function toUnderscore(string $input) : string
94
    {
95 1
        if (!array_key_exists($input, $this->underscores)) {
96 1
            preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches);
97 1
            $ret = $matches[0];
98 1
            foreach ($ret as &$match) {
99 1
                $match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match);
100
            }
101 1
            $this->underscores[$input] = implode('_', $ret);
102
        }
103 1
        return $this->underscores[$input];
104
    }
105
106
    private $camelcased = [];
107
108 1
    public function toCamelCase(string $string, bool $capitalize = false) : string
109
    {
110 1
        $capitalize = $capitalize ? 1 : 0;
111
112 1
        if (!array_key_exists($capitalize, $this->camelcased)) {
113 1
            $this->camelcased[$capitalize] = [];
114
        }
115
116 1
        if (!array_key_exists($string, $this->camelcased[$capitalize])) {
117 1
            $chain = explode('_', $string);
118 1
            foreach ($chain as $index => $word) {
119 1
                $chain[$index] = $index || $capitalize ? ucfirst($word) : $word;
120
            }
121
122 1
            $this->camelcased[$capitalize][$string] = implode('', $chain);
123
        }
124
125 1
        return $this->camelcased[$capitalize][$string];
126
    }
127
128
129
    private $dates = [];
130
131 1
    public function getDate($string)
132
    {
133 1
        $key = $string;
134 1
        if (func_num_args() == 3) {
135 1
            $key = implode('.', func_get_args());
136
        }
137
138 1
        if (Carbon::hasTestNow() || !array_key_exists($key, $this->dates)) {
139 1
            if (strlen($string) == 8 && is_numeric($string)) {
140 1
                $value = Carbon::createFromFormat('Ymd', $string)->setTime(0, 0, 0);
141 1
            } elseif (func_num_args() == 3) {
142 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...
143 1
                $value = Carbon::createFromDate($year, $month, $day)->setTime(0, 0, 0);
144
            } else {
145 1
                $value = Carbon::parse($string);
146
            }
147 1
            if (Carbon::hasTestNow()) {
148 1
                return $value;
149
            }
150 1
            $this->dates[$key] = $value;
151
        }
152 1
        return $this->dates[$key]->copy();
153
    }
154
155 1
    public function getTimestamp($string)
156
    {
157 1
        return $this->getDate($string)->timestamp;
158
    }
159
160
}
161