Total Complexity | 51 |
Total Lines | 253 |
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() { |
||
71 | } |
||
72 | |||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Get the setting input arguments. |
||
77 | * |
||
78 | * @return mixed |
||
79 | */ |
||
80 | public function sd_get_arguments() { |
||
97 | } |
||
98 | |||
99 | |||
100 | /** |
||
101 | * Simply use our own render function for the output. |
||
102 | * |
||
103 | * @return void |
||
104 | */ |
||
105 | public function render() { |
||
106 | $settings = $this->sd_maybe_convert_values( $this->settings ); |
||
107 | |||
108 | // Set the AyeCode UI calss on the wrapper |
||
109 | $this->set_attribute( '_root', 'class', 'bsui' ); |
||
110 | |||
111 | // We might need to add a placeholder here for previews. |
||
112 | |||
113 | do_action( 'super_duper_before_render_bricks_element', $settings, $this->widget, $this ); |
||
114 | |||
115 | // Add the bricks attributes to wrapper |
||
116 | echo "<div {$this->render_attributes( '_root' )}>"; |
||
117 | echo $this->widget->output( $settings ); |
||
118 | echo '</div>'; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * Values can never be arrays so convert if bricks setting make it an array. |
||
123 | * |
||
124 | * @param $settings |
||
125 | * @return mixed |
||
126 | */ |
||
127 | public function sd_maybe_convert_values( $settings ) { |
||
128 | |||
129 | |||
130 | if (!empty($settings)) { |
||
131 | foreach( $settings as $k => $v ) { |
||
132 | if(is_array($v)) { |
||
133 | $value = ''; |
||
134 | // is color |
||
135 | if (isset($v['hex'])) { |
||
136 | $value = $v['hex']; |
||
137 | } elseif (isset($v['icon'])) { |
||
138 | $value = $v['icon']; |
||
139 | } |
||
140 | |||
141 | |||
142 | // set the value |
||
143 | $settings[$k] = $value; |
||
144 | } |
||
145 | |||
146 | } |
||
147 | } |
||
148 | |||
149 | return $settings; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Convert SD arguments to Bricks arguments. |
||
154 | * |
||
155 | * @param $widget |
||
156 | * |
||
157 | * @return array |
||
158 | */ |
||
159 | public function sd_convert_arguments() { |
||
160 | $bricks_args = array(); |
||
161 | |||
162 | $args = $this->sd_get_arguments(); |
||
163 | |||
164 | if ( ! empty( $args ) ) { |
||
165 | foreach ( $args as $key => $arg ) { |
||
166 | // convert title |
||
167 | if ( ! empty( $arg['title'] ) ) { |
||
168 | $arg['label'] = $arg['title']; |
||
169 | unset( $arg['title'] ); |
||
170 | } |
||
171 | |||
172 | // set fields not to use dynamic data |
||
173 | $arg['hasDynamicData'] = false; |
||
174 | |||
175 | if ( ! empty( $arg['group'] ) ) { |
||
176 | $arg['group'] = sanitize_title( $arg['group'] ); |
||
177 | } |
||
178 | |||
179 | $arg['rerender'] = true; |
||
180 | |||
181 | // required |
||
182 | if( ! empty( $arg['element_require'] ) ) { |
||
183 | $arg['required'] = $this->sd_convert_required( $arg['element_require'] ); |
||
184 | unset( $arg['element_require'] ); |
||
185 | } |
||
186 | |||
187 | // icons |
||
188 | if ( 'icon' === $key ) { |
||
189 | $arg['type'] = 'icon'; |
||
190 | } |
||
191 | |||
192 | // Bricks don't render dropdown when first option key is 0. |
||
193 | if ( in_array( $key, array( 'zoom', 'mapzoom' ) ) && ! empty( $arg['options'] ) && is_array( $arg['options'] ) && ( $option_keys = array_keys( $arg['options'] ) ) ) { |
||
194 | // Move first element to last. |
||
195 | if ( $option_keys[0] === 0 || $option_keys[0] === '0' ) { |
||
196 | $options = $arg['options']; |
||
197 | unset( $arg['options'][0] ); |
||
198 | $arg['options'][0] = $options[0]; |
||
199 | } |
||
200 | } |
||
201 | |||
202 | $bricks_args[$key] = $arg; |
||
203 | } |
||
204 | } |
||
205 | |||
206 | return $bricks_args; |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * Convert the SD element_required to the Bricks required syntax. |
||
211 | * |
||
212 | * @param $element_require |
||
213 | * @return array |
||
214 | */ |
||
215 | public function sd_convert_required($element_require) { |
||
216 | $bricks_required = []; |
||
217 | |||
218 | // Handle logical OR (||) for multiple values |
||
219 | if (strpos($element_require, '||') !== false) { |
||
220 | preg_match('/\[%(.+?)%\] *== *"(.*?)"/', $element_require, $matches); |
||
221 | if ($matches) { |
||
222 | $control_id = $matches[1]; |
||
223 | preg_match_all('/\[%.*?%\] *== *"(.*?)"/', $element_require, $value_matches); |
||
224 | $values = $value_matches[1]; |
||
225 | $bricks_required[] = [$control_id, '=', $values]; |
||
226 | } |
||
227 | return $bricks_required; |
||
228 | } |
||
229 | |||
230 | // Match individual conditions |
||
231 | preg_match_all('/(!)?\[%(.*?)%\](?:\s*([!=<>]=?)\s*(".*?"|\'.*?\'|\d+))?/', $element_require, $matches, PREG_SET_ORDER); |
||
232 | |||
233 | foreach ($matches as $match) { |
||
234 | $is_negation = isset($match[1]) && $match[1] === '!'; |
||
235 | $control_id = $match[2]; |
||
236 | $operator = isset($match[3]) ? str_replace('==', '=', $match[3]) : ($is_negation ? '=' : '!='); |
||
237 | $value = isset($match[4]) ? trim($match[4], '"\'') : ($is_negation ? '' : ''); |
||
238 | |||
239 | // Adjust for negation without explicit operator |
||
240 | if ($is_negation && !isset($match[3])) { |
||
241 | $operator = '='; |
||
242 | $value = ''; |
||
243 | } |
||
244 | |||
245 | $bricks_required[] = [$control_id, $operator, $value]; |
||
246 | } |
||
247 | |||
248 | return $bricks_required; |
||
249 | } |
||
250 | |||
251 | |||
252 | /** |
||
253 | * A way to remove some settings by keys. |
||
254 | * |
||
255 | * @return array |
||
256 | */ |
||
257 | public function sd_remove_arguments() |
||
260 | } |
||
261 | |||
262 | } |
||
263 | |||
264 | |||
485 |
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