Passed
Push — master ( 5b5bba...354920 )
by Carlos
06:23
created

VisualItem   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 225
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 44
eloc 75
dl 0
loc 225
rs 8.8798
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
C applyOperatorFromOption() 0 46 14
A __construct() 0 9 2
A setToken() 0 3 1
A setLevel() 0 3 1
A css() 0 3 1
A getLevel() 0 3 1
A getColorFromOption() 0 3 2
A getUniqueId() 0 4 1
C colorToClass() 0 23 17
A combineClasses() 0 10 3
A getToken() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like VisualItem 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 VisualItem, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * This file is part of FacturaScripts
4
 * Copyright (C) 2017-2021 Carlos Garcia Gomez <[email protected]>
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as
8
 * published by the Free Software Foundation, either version 3 of the
9
 * License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public License
17
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
namespace FacturaScripts\Core\Lib\Widget;
21
22
use FacturaScripts\Core\Base\Translator;
23
24
/**
25
 * Description of VisualItem
26
 *
27
 * @author Carlos García Gómez <[email protected]>
28
 */
29
class VisualItem
30
{
31
32
    /**
33
     * @var string
34
     */
35
    public $class;
36
37
    /**
38
     * @var Translator
39
     */
40
    protected static $i18n;
41
42
    /**
43
     * Identifies the object with a defined name in the view
44
     *
45
     * @var string
46
     */
47
    public $id;
48
49
    /**
50
     * Selected security level.
51
     *
52
     * @var int
53
     */
54
    private static $level = 0;
55
56
    /**
57
     * Name defined in the view as key
58
     *
59
     * @var string
60
     */
61
    public $name;
62
63
    /**
64
     * @var string
65
     */
66
    private static $token = '';
67
68
    /**
69
     * @var int
70
     */
71
    protected static $uniqueId = -1;
72
73
    /**
74
     * @param array $data
75
     */
76
    public function __construct(array $data)
77
    {
78
        if (!isset(static::$i18n)) {
79
            static::$i18n = new Translator();
80
        }
81
82
        $this->class = $data['class'] ?? '';
83
        $this->id = $data['id'] ?? '';
84
        $this->name = $data['name'] ?? '';
85
    }
86
87
    /**
88
     * @return int
89
     */
90
    public static function getLevel(): int
91
    {
92
        return self::$level;
93
    }
94
95
    /**
96
     * @return string
97
     */
98
    public static function getToken(): string
99
    {
100
        return self::$token;
101
    }
102
103
    /**
104
     * @param int $new
105
     */
106
    public static function setLevel(int $new)
107
    {
108
        self::$level = $new;
109
    }
110
111
    /**
112
     * @param string $token
113
     */
114
    public static function setToken(string $token)
115
    {
116
        self::$token = $token;
117
    }
118
119
    /**
120
     * @param string $color
121
     * @param string $prefix
122
     *
123
     * @return string
124
     */
125
    protected function colorToClass(string $color, string $prefix): string
126
    {
127
        switch ($color) {
128
            case 'danger':
129
            case 'dark':
130
            case 'info':
131
            case 'light':
132
            case 'outline-danger':
133
            case 'outline-dark':
134
            case 'outline-info':
135
            case 'outline-light':
136
            case 'outline-primary':
137
            case 'outline-secondary':
138
            case 'outline-success':
139
            case 'outline-warning':
140
            case 'primary':
141
            case 'secondary':
142
            case 'success':
143
            case 'warning':
144
                return $prefix . $color;
145
        }
146
147
        return '';
148
    }
149
150
    /**
151
     * Calculate color from option configuration
152
     *
153
     * @param string[] $option
154
     * @param mixed $value
155
     * @param string $prefix
156
     *
157
     * @return string
158
     */
159
    public function getColorFromOption($option, $value, $prefix): string
160
    {
161
        return $this->applyOperatorFromOption($option, $value) ? $this->colorToClass($option['color'], $prefix) : '';
162
    }
163
164
    /**
165
     * @param string[] $option
166
     * @param mixed $value
167
     *
168
     * @return bool
169
     */
170
    protected function applyOperatorFromOption($option, $value): bool
171
    {
172
        $text = $option['text'] ?? '';
173
174
        $applyOperator = '';
175
        $operators = ['>', 'gt:', 'gte:', '<', 'lt:', 'lte:', '!', 'neq:', 'like:', 'null:', 'notnull:'];
176
        foreach ($operators as $operator) {
177
            if (0 === strpos($text, $operator)) {
178
                $applyOperator = $operator;
179
                break;
180
            }
181
        }
182
183
        $matchValue = substr($text, strlen($applyOperator));
184
        $apply = $matchValue == $value;
185
186
        switch ($applyOperator) {
187
            case '>':
188
            case 'gt:':
189
                return (float)$value > (float)$matchValue;
190
191
            case 'gte:':
192
                return (float)$value >= (float)$matchValue;
193
194
            case '<':
195
            case 'lt:':
196
                return (float)$value < (float)$matchValue;
197
198
            case 'lte:':
199
                return (float)$value <= (float)$matchValue;
200
201
            case '!':
202
            case 'neq:':
203
                return $value != $matchValue;
204
205
            case 'like:':
206
                return false !== stripos($value, $matchValue);
207
208
            case 'null:':
209
                return null === $value;
210
211
            case 'notnull:':
212
                return null !== $value;
213
        }
214
215
        return $apply;
216
    }
217
218
    /**
219
     * @param array $classes
220
     *
221
     * @return string
222
     */
223
    protected function combineClasses(...$classes): string
224
    {
225
        $mix = [];
226
        foreach ($classes as $class) {
227
            if (!empty($class)) {
228
                $mix[] = $class;
229
            }
230
        }
231
232
        return implode(' ', $mix);
233
    }
234
235
    /**
236
     * Returns equivalent css class to $class. To extend in plugins.
237
     *
238
     * @param string $class
239
     *
240
     * @return string
241
     */
242
    protected function css(string $class): string
243
    {
244
        return $class;
245
    }
246
247
    /**
248
     * @return int
249
     */
250
    protected function getUniqueId(): int
251
    {
252
        static::$uniqueId++;
253
        return static::$uniqueId;
254
    }
255
}
256