Converter::getPluralForm()   B
last analyzed

Complexity

Conditions 8
Paths 80

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 8

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 8.4444
c 0
b 0
f 0
cc 8
nc 80
nop 2
crap 8
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 56
    public function isTuple($array)
13
    {
14 56
        if (is_object($array)) {
15 1
            $array = get_object_vars($array);
16
        }
17 56
        if (!is_array($array)) {
18 7
            return false;
19
        }
20 56
        if (!count($array)) {
21 56
            return true;
22
        }
23 56
        if (version_compare(PHP_VERSION, '7.3.0') >= 0) {
24 56
            return array_key_last($array) === count($array) - 1;
25
        }
26
        return array_values($array) == $array;
27
    }
28
29 56
    public function toObject($data)
30
    {
31 56
        if (is_array($data)) {
32 56
            if ($this->isTuple($data)) {
33 56
                return $this->convertArrayToObject($data);
34
            }
35
        }
36
37 56
        if (is_object($data)) {
38 5
            if ($data instanceof Entity) {
39
                // keep instance
40 3
                return $data;
41
            }
42
43 2
            $tmp = $data;
44 2
            $data = [];
45 2
            foreach ($tmp as $k => $v) {
46 2
                $data[$k] = $v;
47
            }
48
        }
49
50 56
        $data = (object) $data;
51
52 56
        foreach ($data as $k => $v) {
53 56
            if (is_array($v) && $this->isTuple($v)) {
54 56
                $data->$k = $this->convertArrayToObject($v);
55 56
            } elseif (is_array($v) || is_object($v)) {
56 10
                $data->$k = $this->toObject($v);
57
            }
58
        }
59
60 56
        return $data;
61
    }
62
63 56
    public function convertArrayToObject($data)
64
    {
65 56
        $result = [];
66 56
        foreach ($data as $k => $v) {
67 7
            if ($this->isTuple($v)) {
68
                $result[$k] = $this->convertArrayToObject($v);
69 7
            } elseif (is_object($v) || is_array($v)) {
70
                $result[$k] = $this->toObject($v);
71
            } else {
72 7
                $result[$k] = $v;
73
            }
74
        }
75 56
        return $result;
76
    }
77
78 11
    public function toArray($data) : array
79
    {
80 11
        if (!$data) {
81 2
            return [];
82
        }
83
84 11
        if (is_object($data)) {
85 9
            $data = get_object_vars($data);
86
        }
87
88 11
        foreach ($data as $k => $v) {
89 11
            if (is_array($v) || is_object($v)) {
90 4
                $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