SupportTrait   F
last analyzed

Complexity

Total Complexity 65

Size/Duplication

Total Lines 289
Duplicated Lines 0 %

Test Coverage

Coverage 94.26%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 65
eloc 121
c 3
b 0
f 0
dl 0
loc 289
ccs 115
cts 122
cp 0.9426
rs 3.2

13 Methods

Rating   Name   Duplication   Size   Complexity  
A arrayOnlyNumeric() 0 11 3
A convertValue() 0 14 5
A prepareFrom() 0 14 6
B prepareFields() 0 16 7
A prepareOrder() 0 8 3
C prepareValues() 0 49 13
A checkFields() 0 11 5
A withCommaSeparator() 0 7 2
A prepareLimit() 0 8 3
A prepareWhere() 0 16 5
A convertDate() 0 15 4
A prepareNull() 0 15 5
A prepareValuesSet() 0 13 4

How to fix   Complexity   

Complex Class

Complex classes like SupportTrait 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.

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 SupportTrait, and based on these observations, apply Extract Interface, too.

1
<?php namespace AgelxNash\Modx\Evo\Database\Traits;
2
3
use AgelxNash\Modx\Evo\Database\Exceptions;
4
5
trait SupportTrait
6
{
7
    /**
8
     * @param $value
9
     * @return mixed
10
     */
11 19
    protected function convertValue($value)
12
    {
13
        switch (true) {
14 19
            case (\is_numeric($value) && ! \is_float(1 * $value)):
15 15
                $value = (int)$value;
16 15
                break;
17 15
            case \is_numeric($value) && \is_float(1*$value):
18 1
                $value = (float)$value;
19 1
                break;
20
            default:
21 15
                break;
22
        }
23
24 19
        return $value;
25
    }
26
27
    /**
28
     * @param int $timestamp
29
     * @param string $fieldType
30
     * @return bool|false|string
31
     * @deprecated
32
     */
33 1
    public function convertDate($timestamp, $fieldType = 'DATETIME')
34
    {
35 1
        $date = false;
36 1
        if (! empty($timestamp) && $timestamp > 0) {
37
            $format = [
38 1
                'DATE' => 'Y-m-d',
39
                'TIME' => 'H:i:s',
40
                'YEAR' => 'Y',
41
                'DATETIME' => 'Y-m-d H:i:s',
42
            ];
43 1
            $use = isset($format[$fieldType]) ? $format[$fieldType] : 'Y-m-d H:i:s';
44 1
            $date = date($use, $timestamp);
45
        }
46
47 1
        return $date;
48
    }
49
50
    /**
51
     * @param string|array $data
52
     * @param bool $ignoreAlias
53
     * @return string
54
     */
55 9
    protected function prepareFields($data, $ignoreAlias = false)
56
    {
57 9
        if (\is_array($data)) {
58 5
            $tmp = [];
59 5
            foreach ($data as $alias => $field) {
60 4
                $tmp[] = ($alias !== $field && ! \is_int($alias) && $ignoreAlias === false) ?
61 4
                    ($field . ' as `' . $alias . '`') : $field;
62
            }
63
64 5
            $data = implode(',', $tmp);
65
        }
66 9
        if (empty($data)) {
67 1
            $data = '*';
68
        }
69
70 9
        return $data;
71
    }
72
73
    /**
74
     * @param string|null $value
75
     * @return string
76
     * @throws Exceptions\InvalidFieldException
77
     */
78 17
    protected function prepareNull($value)
79
    {
80
        switch (true) {
81 17
            case ($value === null || (\is_scalar($value) && strtolower($value) === 'null')):
82 5
                $value = 'NULL';
83 5
                break;
84 17
            case \is_scalar($value):
85 17
                $value = "'" . $value . "'";
86 17
                break;
87
            default:
88 1
                throw (new Exceptions\InvalidFieldException('NULL'))
89 1
                    ->setData($value);
90
        }
91
92 17
        return $value;
93
    }
94
95
    /**
96
     * @param string|array $data
97
     * @param int $level
98
     * @param bool $skipFieldNames
99
     * @return array|string
100
     * @throws Exceptions\InvalidFieldException
101
     * @throws Exceptions\TooManyLoopsException
102
     */
103 13
    protected function prepareValues($data, $level = 1, $skipFieldNames = false)
104
    {
105 13
        $fields = [];
106 13
        $values = [];
107 13
        $maxLevel = $level;
108 13
        $wrap = false;
109
110 13
        if (\is_array($data)) {
111 13
            foreach ($data as $key => $value) {
112 13
                if (\is_array($value)) {
113 1
                    if ($level > 2) {
114
                        throw new Exceptions\TooManyLoopsException('Values');
115
                    }
116 1
                    $maxLevel++;
117 1
                    $out = $this->prepareValues($value, $level + 1);
118 1
                    if (empty($fields)) {
119 1
                        $fields = $out['fields'];
120 1
                    } elseif ($fields !== $out['fields'] && $skipFieldNames === false) {
121
                        throw (new Exceptions\InvalidFieldException("Don't match field names"))
122
                            ->setData($data);
123
                    }
124 1
                    $wrap = true;
125 1
                    $values[] = $out['values'];
126
                } else {
127 13
                    $fields[] = $key;
128 13
                    $values[] = $this->prepareNull($value);
129
                }
130
            }
131 13
            $values = $this->withCommaSeparator($values);
132 13
            if ($wrap === false) {
133 13
                $values = '(' . $values . ')';
134
            }
135
        }
136
137 13
        if (! \is_scalar($values)) {
138
            throw (new Exceptions\InvalidFieldException('values'))
139
                ->setData($values);
140
        }
141
142 13
        if (($fields = $this->checkFields($fields, $maxLevel, $skipFieldNames)) === false) {
143
            throw (new Exceptions\InvalidFieldException('fields name'))
144
                ->setData($data);
145
        }
146
147 13
        if ($level === 2) {
148 1
            return compact('fields', 'values');
149
        }
150
151 13
        return (empty($fields) ? '' : $fields . ' VALUES ') . $values;
152
    }
153
154
    /**
155
     * @param mixed $fields
156
     * @param int $level
157
     * @param bool $skipFieldNames
158
     * @return bool|string
159
     */
160 13
    protected function checkFields($fields, $level = 1, $skipFieldNames = false)
161
    {
162 13
        if (\is_array($fields) && $skipFieldNames === false) {
163 13
            if ($this->arrayOnlyNumeric($fields) === true) {
164 1
                $fields = ($level === 2) ? false : '';
165
            } else {
166 13
                $fields = '(`' . implode('`, `', $fields) . '`)';
167
            }
168
        }
169
170 13
        return $fields;
171
    }
172
173
    /**
174
     * @param array $data
175
     * @return bool
176
     */
177 16
    protected function arrayOnlyNumeric(array $data)
178
    {
179 16
        $onlyNumbers = true;
180 16
        foreach ($data as $value) {
181 16
            if (! \is_numeric($value)) {
182 14
                $onlyNumbers = false;
183 16
                break;
184
            }
185
        }
186
187 16
        return $onlyNumbers;
188
    }
189
190
    /**
191
     * @param string|array $data
192
     * @return string
193
     * @throws Exceptions\InvalidFieldException
194
     */
195 5
    protected function prepareValuesSet($data)
196
    {
197 5
        if (\is_array($data)) {
198 5
            foreach ($data as $key => $value) {
199 5
                $data[$key] = $this->prepareNull($value);
200
            }
201
202 5
            foreach ($data as $key => $value) {
203 5
                $data[$key] = "`{$key}` = " . $value;
204
            }
205
        }
206
207 5
        return $this->withCommaSeparator($data);
208
    }
209
210
    /**
211
     * @param string|array $data
212
     * @param bool $hasArray
213
     * @return string
214
     * @throws Exceptions\TableNotDefinedException
215
     */
216 22
    protected function prepareFrom($data, $hasArray = false)
217
    {
218 22
        if (\is_array($data) && $hasArray === true) {
219 3
            $tmp = [];
220 3
            foreach ($data as $table) {
221 3
                $tmp[] = $table;
222
            }
223 3
            $data = implode(' ', $tmp);
224
        }
225 22
        if (! is_scalar($data) || empty($data)) {
226 1
            throw new Exceptions\TableNotDefinedException($data);
227
        }
228
229 22
        return $data;
230
    }
231
232
    /**
233
     * @param array|string $data
234
     * @return string
235
     * @throws Exceptions\InvalidFieldException
236
     */
237 13
    protected function prepareWhere($data)
238
    {
239 13
        if (\is_array($data)) {
240 3
            if ($this->arrayOnlyNumeric(array_keys($data)) === true) {
241 3
                $data = implode(' ', $data);
242
            } else {
243 1
                throw (new Exceptions\InvalidFieldException('WHERE'))
244 1
                    ->setData($data);
245
            }
246
        }
247 13
        $data = trim($data);
248 13
        if (! empty($data) && stripos($data, 'WHERE') !== 0) {
249 11
            $data = "WHERE {$data}";
250
        }
251
252 13
        return $data;
253
    }
254
255
    /**
256
     * @param string $data
257
     * @return string
258
     */
259 9
    protected function prepareOrder($data)
260
    {
261 9
        $data = trim($data);
262 9
        if (! empty($data) && stripos($data, 'ORDER') !== 0) {
263 5
            $data = "ORDER BY {$data}";
264
        }
265
266 9
        return $data;
267
    }
268
269
    /**
270
     * @param string $data
271
     * @return string
272
     */
273 11
    protected function prepareLimit($data)
274
    {
275 11
        $data = trim($data);
276 11
        if (! empty($data) && stripos($data, 'LIMIT') !== 0) {
277 5
            $data = "LIMIT {$data}";
278
        }
279
280 11
        return $data;
281
    }
282
283
    /**
284
     * @param $values
285
     * @return string
286
     */
287 16
    protected function withCommaSeparator($values)
288
    {
289 16
        if (\is_array($values)) {
290 16
            $values = implode(', ', $values);
291
        }
292
293 16
        return trim($values);
294
    }
295
}
296