Completed
Push — master ( de3d4d...27649e )
by Dmitry
03:19
created

Converter   C

Complexity

Total Complexity 55

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 1

Test Coverage

Coverage 95.18%

Importance

Changes 0
Metric Value
wmc 55
lcom 3
cbo 1
dl 0
loc 161
ccs 79
cts 83
cp 0.9518
rs 6
c 0
b 0
f 0

9 Methods

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

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

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
147 1
            } elseif (func_num_args() == 3) {
148 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...
149 1
                $value = Carbon::createFromDate($year, $month, $day)->setTime(0, 0, 0);
150
            } else {
151 1
                $value = Carbon::parse($string);
152
            }
153 1
            if (Carbon::hasTestNow()) {
154 1
                return $value;
155
            }
156 1
            $this->dates[$key] = $value;
157
        }
158 1
        return $this->dates[$key]->copy();
159
    }
160
161 1
    public function getTimestamp($string)
162
    {
163 1
        return $this->getDate($string)->timestamp;
164
    }
165
166 1
    public function getPluralForm($n, $forms)
167
    {
168 1
        return is_float($n)?$forms[1]:($n%10==1&&$n%100!=11?$forms[0]:($n%10>=2&&$n%10<=4&&($n%100<10||$n%100>=20)?$forms[1]:$forms[2]));
169
    }
170
}
171