Total Complexity | 43 |
Total Lines | 241 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Super_Duper_Bricks_Element 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 Super_Duper_Bricks_Element, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class Super_Duper_Bricks_Element extends \Bricks\Element { |
||
|
|||
8 | |||
9 | public $widget; |
||
10 | |||
11 | public function __construct( $element = null ) { |
||
22 | } |
||
23 | |||
24 | /** |
||
25 | * Set the element name. |
||
26 | * |
||
27 | * @return array|string|string[]|null |
||
28 | */ |
||
29 | public function get_label() { |
||
30 | $escaped_text = esc_attr( $this->widget->name ); |
||
31 | return str_replace( ' > ', ' > ', $escaped_text ); // keep our > but have it safe |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * Bricks function to set the controls |
||
36 | * |
||
37 | * @return void |
||
38 | */ |
||
39 | public function set_controls() { |
||
40 | $args = $this->sd_convert_arguments($this->widget); |
||
41 | |||
42 | if (!empty($args)) { |
||
43 | $this->controls = $this->controls + $args; |
||
44 | } |
||
45 | |||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Set the bricks control groups from the GD ones. |
||
50 | * |
||
51 | * @return void |
||
52 | */ |
||
53 | public function set_control_groups() { |
||
54 | $args = $this->sd_get_arguments(); |
||
55 | |||
56 | $groups = array(); |
||
57 | if(!empty($args)) { |
||
58 | foreach ($args as $k => $v) { |
||
59 | $g_slug = !empty($v['group']) ? sanitize_title( $v['group'] ) : ''; |
||
60 | if($g_slug && empty($groups[$g_slug])) { |
||
61 | $groups[$g_slug] = array( |
||
62 | 'title' => esc_html( $v['group'] ), |
||
63 | 'tab' => 'content', |
||
64 | ); |
||
65 | } |
||
66 | } |
||
67 | } |
||
68 | |||
69 | if(!empty($groups)) { |
||
70 | $this->control_groups = $this->control_groups + $groups; |
||
71 | } |
||
72 | |||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Get the setting input arguments. |
||
77 | * |
||
78 | * @return mixed |
||
79 | */ |
||
80 | public function sd_get_arguments() { |
||
91 | } |
||
92 | |||
93 | |||
94 | /** |
||
95 | * Simply use our own render function for the output. |
||
96 | * |
||
97 | * @return void |
||
98 | */ |
||
99 | public function render() { |
||
100 | $settings = $this->sd_maybe_convert_values( $this->settings ); |
||
101 | |||
102 | |||
103 | // set the AyeCode UI calss on the wrapper |
||
104 | $this->set_attribute( '_root', 'class', 'bsui' ); |
||
105 | |||
106 | // we might need to add a placeholder here for previews. |
||
107 | |||
108 | // add the bricks attributes to wrapper |
||
109 | echo "<div {$this->render_attributes( '_root' )}>"; |
||
110 | echo $this->widget->output($settings); |
||
111 | echo '</div>'; |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Values can never be arrays so convert if bricks setting make it an array. |
||
116 | * |
||
117 | * @param $settings |
||
118 | * @return mixed |
||
119 | */ |
||
120 | public function sd_maybe_convert_values( $settings ) { |
||
121 | |||
122 | |||
123 | if (!empty($settings)) { |
||
124 | foreach( $settings as $k => $v ) { |
||
125 | if(is_array($v)) { |
||
126 | $value = ''; |
||
127 | // is color |
||
128 | if (isset($v['hex'])) { |
||
129 | $value = $v['hex']; |
||
130 | } elseif (isset($v['icon'])) { |
||
131 | $value = $v['icon']; |
||
132 | } |
||
133 | |||
134 | |||
135 | // set the value |
||
136 | $settings[$k] = $value; |
||
137 | } |
||
138 | |||
139 | } |
||
140 | } |
||
141 | |||
142 | return $settings; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Convert SD arguments to Bricks arguments. |
||
147 | * |
||
148 | * @param $widget |
||
149 | * |
||
150 | * @return array |
||
151 | */ |
||
152 | public function sd_convert_arguments() |
||
153 | { |
||
154 | $bricks_args = array(); |
||
155 | |||
156 | $args = $this->sd_get_arguments(); |
||
157 | |||
158 | if (!empty($args)) { |
||
159 | foreach ($args as $key => $arg) { |
||
160 | |||
161 | // convert title |
||
162 | if (!empty($arg['title'])) { |
||
163 | $arg['label'] = $arg['title']; |
||
164 | unset($arg['title']); |
||
165 | } |
||
166 | |||
167 | // set fields not to use dynamic data |
||
168 | $arg['hasDynamicData'] = false; |
||
169 | |||
170 | if (!empty($arg['group'])) { |
||
171 | $arg['group'] = sanitize_title($arg['group']); |
||
172 | } |
||
173 | |||
174 | $arg['rerender'] = true; |
||
175 | |||
176 | // required |
||
177 | if(!empty($arg['element_require'])) { |
||
178 | $arg['required'] = $this->sd_convert_required($arg['element_require']); |
||
179 | unset($arg['element_require']); |
||
180 | } |
||
181 | |||
182 | // icons |
||
183 | if ('icon' === $key) { |
||
184 | $arg['type'] = 'icon'; |
||
185 | } |
||
186 | |||
187 | $bricks_args[$key] = $arg; |
||
188 | |||
189 | } |
||
190 | |||
191 | } |
||
192 | |||
193 | return $bricks_args; |
||
194 | |||
195 | } |
||
196 | |||
197 | /** |
||
198 | * Convert the SD element_required to the Bricks required syntax. |
||
199 | * |
||
200 | * @param $element_require |
||
201 | * @return array |
||
202 | */ |
||
203 | public function sd_convert_required($element_require) { |
||
204 | $bricks_required = []; |
||
205 | |||
206 | // Handle logical OR (||) for multiple values |
||
207 | if (strpos($element_require, '||') !== false) { |
||
208 | preg_match('/\[%(.+?)%\] *== *"(.*?)"/', $element_require, $matches); |
||
209 | if ($matches) { |
||
210 | $control_id = $matches[1]; |
||
211 | preg_match_all('/\[%.*?%\] *== *"(.*?)"/', $element_require, $value_matches); |
||
212 | $values = $value_matches[1]; |
||
213 | $bricks_required[] = [$control_id, '=', $values]; |
||
214 | } |
||
215 | return $bricks_required; |
||
216 | } |
||
217 | |||
218 | // Match individual conditions |
||
219 | preg_match_all('/(!)?\[%(.*?)%\](?:\s*([!=<>]=?)\s*(".*?"|\'.*?\'|\d+))?/', $element_require, $matches, PREG_SET_ORDER); |
||
220 | |||
221 | foreach ($matches as $match) { |
||
222 | $is_negation = isset($match[1]) && $match[1] === '!'; |
||
223 | $control_id = $match[2]; |
||
224 | $operator = isset($match[3]) ? str_replace('==', '=', $match[3]) : ($is_negation ? '=' : '!='); |
||
225 | $value = isset($match[4]) ? trim($match[4], '"\'') : ($is_negation ? '' : ''); |
||
226 | |||
227 | // Adjust for negation without explicit operator |
||
228 | if ($is_negation && !isset($match[3])) { |
||
229 | $operator = '='; |
||
230 | $value = ''; |
||
231 | } |
||
232 | |||
233 | $bricks_required[] = [$control_id, $operator, $value]; |
||
234 | } |
||
235 | |||
236 | return $bricks_required; |
||
237 | } |
||
238 | |||
239 | |||
240 | /** |
||
241 | * A way to remove some settings by keys. |
||
242 | * |
||
243 | * @return array |
||
244 | */ |
||
245 | public function sd_remove_arguments() |
||
248 | } |
||
249 | |||
250 | } |
||
251 | |||
252 | |||
253 | /** |
||
254 | * This implements the desktop, tablet and mobile breakpoints views with our fields that are hidden on these types and adda the icon after the label to show which it applies to. |
||
473 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths