Total Complexity | 44 |
Total Lines | 225 |
Duplicated Lines | 0 % |
Changes | 0 |
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 |
||
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 |
||
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 |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * @return int |
||
249 | */ |
||
250 | protected function getUniqueId(): int |
||
254 | } |
||
255 | } |
||
256 |