Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Helper 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Helper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class Helper { |
||
13 | |||
14 | /** |
||
15 | * Get a field from a specific container type or id |
||
16 | * |
||
17 | * @param string $container_type Container type to search in. Optional if $container_id is supplied |
||
18 | * @param string $container_id Container id to search in. Optional if $container_type is supplied |
||
19 | * @param string $field_name Field name to search for |
||
20 | * @return \Carbon_Fields\Field\Field |
||
21 | */ |
||
22 | public static function get_field( $container_type, $container_id, $field_name ) { |
||
23 | \Carbon_Fields\Carbon_Fields::verify_fields_registered(); |
||
24 | |||
25 | $repository = \Carbon_Fields\Carbon_Fields::resolve( 'container_repository' ); |
||
26 | if ( $container_id ) { |
||
27 | return $repository->get_field_in_container( $field_name, $container_id ); |
||
28 | } |
||
29 | return $repository->get_field_in_containers( $field_name, $container_type ); |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * Get a clone of a field with a value loaded. |
||
34 | * WARNING: The datastore is cloned! |
||
35 | * |
||
36 | * @param int $object_id Object id to get value for (e.g. post_id, term_id etc.) |
||
37 | * @param string $container_type Container type to search in. Optional if $container_id is supplied |
||
38 | * @param string $container_id Container id to search in. Optional if $container_type is supplied |
||
39 | * @param string $field_name Field name to search for |
||
40 | * @return \Carbon_Fields\Field\Field |
||
41 | */ |
||
42 | public static function get_field_clone( $object_id, $container_type, $container_id, $field_name ) { |
||
43 | $field = static::get_field( $container_type, $container_id, $field_name ); |
||
44 | |||
45 | if ( ! $field ) { |
||
46 | return null; |
||
47 | } |
||
48 | |||
49 | $clone = clone $field; |
||
50 | if ( $object_id !== null ) { |
||
51 | $clone->set_datastore( clone $clone->get_datastore(), $clone->has_default_datastore() ); |
||
52 | $clone->get_datastore()->set_object_id( $object_id ); |
||
53 | } |
||
54 | return $clone; |
||
55 | } |
||
56 | |||
57 | /** |
||
58 | * Execute an action with a clone of a field with a value loaded. |
||
59 | * WARNING: The datastore reference is kept! |
||
60 | * |
||
61 | * @param int $object_id Object id to get value for (e.g. post_id, term_id etc.) |
||
62 | * @param string $container_type Container type to search in. Optional if $container_id is supplied |
||
63 | * @param string $container_id Container id to search in. Optional if $container_type is supplied |
||
64 | * @param string $field_name Field name to search for |
||
65 | * @param \Closure $action Action to execute |
||
66 | * @return void|mixed |
||
67 | */ |
||
68 | public static function with_field_clone( $object_id, $container_type, $container_id, $field_name, $action ) { |
||
69 | $field = static::get_field( $container_type, $container_id, $field_name ); |
||
70 | |||
71 | if ( ! $field ) { |
||
72 | return; |
||
73 | } |
||
74 | |||
75 | $clone = clone $field; |
||
76 | $datastore = $clone->get_datastore(); |
||
77 | $datastore_object_id = $datastore->get_object_id(); |
||
78 | |||
79 | if ( $object_id !== null ) { |
||
80 | $datastore->set_object_id( $object_id ); |
||
81 | } |
||
82 | |||
83 | $result = $action($clone); |
||
84 | |||
85 | if ( $object_id !== null ) { |
||
86 | $datastore->set_object_id( $datastore_object_id ); |
||
87 | } |
||
88 | |||
89 | return $result; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Get a value formatted for end-users |
||
94 | * |
||
95 | * @param int $object_id Object id to get value for (e.g. post_id, term_id etc.) |
||
96 | * @param string $container_type Container type to search in |
||
97 | * @param string $container_id |
||
98 | * @param string $field_name Field name |
||
99 | * @return mixed |
||
100 | */ |
||
101 | public static function get_value( $object_id, $container_type, $container_id, $field_name ) { |
||
117 | |||
118 | /** |
||
119 | * Set value for a field |
||
120 | * |
||
121 | * @param int $object_id Object id to get value for (e.g. post_id, term_id etc.) |
||
122 | * @param string $container_type Container type to search in |
||
123 | * @param string $container_id |
||
124 | * @param string $field_name Field name |
||
125 | * @param array $value Field expects a `value_set`. Complex_Field expects a `value_tree` - refer to DEVELOPMENT.md |
||
126 | * @return void |
||
127 | */ |
||
128 | public static function set_value( $object_id, $container_type, $container_id, $field_name, $value ) { |
||
129 | static::with_field_clone( |
||
130 | $object_id, |
||
131 | $container_type, |
||
132 | $container_id, |
||
133 | $field_name, |
||
134 | function( $field ) use ( $container_type, $container_id, $field_name, $value ) { |
||
135 | if ( ! $field ) { |
||
136 | $container_message = $container_id ? 'in container with id "' . $container_id . '"' : 'in containers of type "' . $container_type . '"'; |
||
137 | Incorrect_Syntax_Exception::raise( 'Could not find a field which satisfies the supplied pattern ' . $container_message . ': ' . $field_name ); |
||
138 | return; |
||
139 | } |
||
140 | /** @var \Carbon_Fields\Field\Field $field */ |
||
141 | $field->set_value( $value ); |
||
142 | $field->save(); |
||
143 | } |
||
144 | ); |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Shorthand for get_post_meta(). |
||
149 | * Uses the ID of the current post in the loop. |
||
150 | * |
||
151 | * @param string $name Field name |
||
152 | * @param string $container_id |
||
153 | * @return mixed |
||
154 | */ |
||
155 | public static function get_the_post_meta( $name, $container_id = '' ) { |
||
158 | |||
159 | /** |
||
160 | * Get post meta field for a post. |
||
161 | * |
||
162 | * @param int $id Post ID |
||
163 | * @param string $name Field name |
||
164 | * @param string $container_id |
||
165 | * @return mixed |
||
166 | */ |
||
167 | public static function get_post_meta( $id, $name, $container_id = '' ) { |
||
171 | |||
172 | /** |
||
173 | * Set post meta field for a post. |
||
174 | * |
||
175 | * @param int $id Post ID |
||
176 | * @param string $name Field name |
||
177 | * @param array $value |
||
178 | * @param string $container_id |
||
179 | */ |
||
180 | public static function set_post_meta( $id, $name, $value, $container_id = '' ) { |
||
181 | static::set_value( $id, 'post_meta', $container_id, $name, $value ); |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Get theme option field value. |
||
186 | * |
||
187 | * @param string $name Field name |
||
188 | * @param string $container_id |
||
189 | * @return mixed |
||
190 | */ |
||
191 | public static function get_theme_option( $name, $container_id = '' ) { |
||
194 | |||
195 | /** |
||
196 | * Set theme option field value. |
||
197 | * |
||
198 | * @param string $name Field name |
||
199 | * @param array $value |
||
200 | * @param string $container_id |
||
201 | */ |
||
202 | public static function set_theme_option( $name, $value, $container_id = '' ) { |
||
203 | static::set_value( null, 'theme_options', $container_id, $name, $value ); |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * Get network option field value for the main site. |
||
208 | * |
||
209 | * @param string $name Field name |
||
210 | * @param string $container_id |
||
211 | * @return mixed |
||
212 | */ |
||
213 | public static function get_the_network_option( $name, $container_id = '' ) { |
||
217 | |||
218 | /** |
||
219 | * Get network option field value for a site. |
||
220 | * |
||
221 | * @param string $id Site ID |
||
222 | * @param string $name Field name |
||
223 | * @param string $container_id |
||
224 | * @return mixed |
||
225 | */ |
||
226 | public static function get_network_option( $id, $name, $container_id = '' ) { |
||
229 | |||
230 | /** |
||
231 | * Set network option field value for a site. |
||
232 | * |
||
233 | * @param string $id Site ID |
||
234 | * @param string $name Field name |
||
235 | * @param array $value |
||
236 | * @param string $container_id |
||
237 | */ |
||
238 | public static function set_network_option( $id, $name, $value, $container_id = '' ) { |
||
239 | static::set_value( $id, 'network', $container_id, $name, $value ); |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * Get term meta field for a term. |
||
244 | * |
||
245 | * @param int $id Term ID |
||
246 | * @param string $name Field name |
||
247 | * @param string $container_id |
||
248 | * @return mixed |
||
249 | */ |
||
250 | public static function get_term_meta( $id, $name, $container_id = '' ) { |
||
253 | |||
254 | /** |
||
255 | * Set term meta field for a term. |
||
256 | * |
||
257 | * @param int $id Term ID |
||
258 | * @param string $name Field name |
||
259 | * @param array $value |
||
260 | * @param string $container_id |
||
261 | */ |
||
262 | public static function set_term_meta( $id, $name, $value, $container_id = '' ) { |
||
263 | static::set_value( $id, 'term_meta', $container_id, $name, $value ); |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * Get user meta field for a user. |
||
268 | * |
||
269 | * @param int $id User ID |
||
270 | * @param string $name Field name |
||
271 | * @param string $container_id |
||
272 | * @return mixed |
||
273 | */ |
||
274 | public static function get_user_meta( $id, $name, $container_id = '' ) { |
||
277 | |||
278 | /** |
||
279 | * Set user meta field for a user. |
||
280 | * |
||
281 | * @param int $id User ID |
||
282 | * @param string $name Field name |
||
283 | * @param array $value |
||
284 | * @param string $container_id |
||
285 | */ |
||
286 | public static function set_user_meta( $id, $name, $value, $container_id = '' ) { |
||
287 | static::set_value( $id, 'user_meta', $container_id, $name, $value ); |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Get comment meta field for a comment. |
||
292 | * |
||
293 | * @param int $id Comment ID |
||
294 | * @param string $name Field name |
||
295 | * @param string $container_id |
||
296 | * @return mixed |
||
297 | */ |
||
298 | public static function get_comment_meta( $id, $name, $container_id = '' ) { |
||
301 | |||
302 | /** |
||
303 | * Set comment meta field for a comment. |
||
304 | * |
||
305 | * @param int $id Comment ID |
||
306 | * @param string $name Field name |
||
307 | * @param array $value |
||
308 | * @param string $container_id |
||
309 | */ |
||
310 | public static function set_comment_meta( $id, $name, $value, $container_id = '' ) { |
||
311 | static::set_value( $id, 'comment_meta', $container_id, $name, $value ); |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * Get nav menu item meta field for a nav menu item. |
||
316 | * |
||
317 | * @param int $id Nav menu item ID |
||
318 | * @param string $name Field name |
||
319 | * @param string $container_id |
||
320 | * @return mixed |
||
321 | */ |
||
322 | public static function get_nav_menu_item_meta( $id, $name, $container_id = '' ) { |
||
325 | |||
326 | /** |
||
327 | * Set nav menu item meta field for a nav menu item. |
||
328 | * |
||
329 | * @param int $id Nav menu item ID |
||
330 | * @param string $name Field name |
||
331 | * @param array $value |
||
332 | * @param string $container_id |
||
333 | */ |
||
334 | public static function set_nav_menu_item_meta( $id, $name, $value, $container_id = '' ) { |
||
335 | static::set_value( $id, 'nav_menu_item', $container_id, $name, $value ); |
||
336 | } |
||
337 | |||
338 | /** |
||
339 | * Recursive sorting function by array key. |
||
340 | * |
||
341 | * @param array &$array The input array. |
||
342 | * @param int $sort_flags Flags for controlling sorting behavior. |
||
343 | * @return boolean |
||
344 | */ |
||
345 | public static function ksort_recursive( &$array, $sort_flags = SORT_REGULAR ) { |
||
346 | if ( ! is_array( $array ) ) { |
||
347 | return false; |
||
348 | } |
||
349 | ksort( $array, $sort_flags ); |
||
350 | foreach ( $array as $key => $value ) { |
||
351 | self::ksort_recursive( $array[ $key ], $sort_flags ); |
||
352 | } |
||
353 | return true; |
||
354 | } |
||
355 | |||
356 | /** |
||
357 | * Get the relation type from an array similar to how meta_query works in WP_Query |
||
358 | * |
||
359 | * @param array $array |
||
360 | * @param array<string> $allowed_relations |
||
361 | * @param string $relation_key |
||
362 | * @return string |
||
363 | */ |
||
364 | public static function get_relation_type_from_array( $array, $allowed_relations = array( 'AND', 'OR' ), $relation_key = 'relation' ) { |
||
365 | $allowed_relations = array_values( $allowed_relations ); |
||
366 | $allowed_relations = array_map( 'strtoupper', $allowed_relations ); |
||
367 | $relation = isset( $allowed_relations[0] ) ? $allowed_relations[0] : ''; |
||
368 | |||
369 | if ( isset( $array[ $relation_key ] ) ) { |
||
370 | $relation = strtoupper( $array[ $relation_key ] ); |
||
371 | } |
||
372 | |||
373 | View Code Duplication | if ( ! in_array( $relation, $allowed_relations ) ) { |
|
374 | Incorrect_Syntax_Exception::raise( 'Invalid relation type ' . $relation . '. ' . |
||
375 | 'The rule should be one of the following: "' . implode( '", "', $allowed_relations ) . '"' ); |
||
376 | } |
||
377 | |||
378 | return $relation; |
||
379 | } |
||
380 | |||
381 | /** |
||
382 | * Normalize a label by updating case, stripping common prefixes etc. |
||
383 | * |
||
384 | * @param string $label |
||
385 | * @return string |
||
386 | */ |
||
387 | View Code Duplication | public static function normalize_label( $label ) { |
|
399 | |||
400 | /** |
||
401 | * Normalize a type string representing an object type |
||
402 | * |
||
403 | * @param string $type |
||
404 | * @return string |
||
405 | */ |
||
406 | View Code Duplication | public static function normalize_type( $type ) { |
|
413 | |||
414 | /** |
||
415 | * Convert a string representing an object type to a fully qualified class name |
||
416 | * |
||
417 | * @param string $type |
||
418 | * @param string $namespace |
||
419 | * @param string $class_suffix |
||
420 | * @return string |
||
421 | */ |
||
422 | public static function type_to_class( $type, $namespace = '', $class_suffix = '' ) { |
||
423 | $classlike_type = static::normalize_type( $type ); |
||
424 | $classlike_type = str_replace( '_', ' ', $classlike_type ); |
||
425 | $classlike_type = ucwords( $classlike_type ); |
||
426 | $classlike_type = str_replace( ' ', '_', $classlike_type ); |
||
427 | |||
428 | $class = $classlike_type . $class_suffix; |
||
429 | if ( $namespace ) { |
||
430 | $class = $namespace . '\\' . $class; |
||
431 | } |
||
432 | |||
433 | return $class; |
||
434 | } |
||
435 | |||
436 | /** |
||
437 | * Convert a string representing an object type to a fully qualified class name |
||
438 | * |
||
439 | * @param string $class |
||
440 | * @param string $class_suffix |
||
441 | * @return string |
||
442 | */ |
||
443 | public static function class_to_type( $class, $class_suffix = '' ) { |
||
444 | $reflection = new \ReflectionClass( $class ); |
||
445 | $type = $reflection->getShortName(); |
||
446 | |||
447 | if ( $class_suffix ) { |
||
448 | $type = preg_replace( '/(' . preg_quote( $class_suffix, '/' ) . ')$/i', '', $type ); |
||
449 | } |
||
450 | |||
451 | $type = static::normalize_type( $type ); |
||
452 | |||
453 | return $type; |
||
454 | } |
||
455 | |||
456 | /** |
||
457 | * Get an array of sanitized html classes |
||
458 | * |
||
459 | * @param string|array<string> $classes |
||
460 | * @return array<string> |
||
461 | */ |
||
462 | public static function sanitize_classes( $classes ) { |
||
463 | if ( ! is_array( $classes ) ) { |
||
464 | $classes = array_values( array_filter( explode( ' ', $classes ) ) ); |
||
465 | } |
||
466 | $classes = array_map( 'sanitize_html_class', $classes ); |
||
467 | return $classes; |
||
468 | } |
||
469 | |||
470 | /** |
||
471 | * Check if an id or name for containers and fields is valid |
||
472 | * |
||
473 | * @param string $id |
||
474 | * @return boolean |
||
475 | */ |
||
476 | public static function is_valid_entity_id( $id ) { |
||
479 | |||
480 | /** |
||
481 | * Return a partial regex pettern matching allowed field name characters |
||
482 | * |
||
483 | * @return string |
||
484 | */ |
||
485 | public static function get_field_name_characters_pattern() { |
||
488 | |||
489 | /** |
||
490 | * Get an attachment ID given a file URL |
||
491 | * Modified version of https://wpscholar.com/blog/get-attachment-id-from-wp-image-url/ |
||
492 | * |
||
493 | * @static |
||
494 | * @access public |
||
495 | * |
||
496 | * @param string $url |
||
497 | * @return integer |
||
498 | */ |
||
499 | public static function get_attachment_id( $url ) { |
||
555 | |||
556 | /** |
||
557 | * Returns attachment metadata from an ID. |
||
558 | * |
||
559 | * @static |
||
560 | * @access public |
||
561 | * |
||
562 | * @param string $id |
||
563 | * @param string $type Value Type. Can be either id or url. |
||
564 | * @return array |
||
565 | */ |
||
566 | public static function get_attachment_metadata( $id, $type ) { |
||
627 | |||
628 | /** |
||
629 | * Get the current $_POST or $_GET input array with compacted input values merged in |
||
630 | * |
||
631 | * @return array |
||
632 | */ |
||
633 | public static function input() { |
||
643 | |||
644 | /** |
||
645 | * Get a copy of the passed array with compacted input values merged in |
||
646 | * |
||
647 | * @param array $input |
||
648 | * @return array |
||
649 | */ |
||
650 | public static function expand_compacted_input( $input ) { |
||
657 | |||
658 | /** |
||
659 | * Get valid input from an input array compared to predefined options |
||
660 | * |
||
661 | * @param array $input |
||
662 | * @param array $options |
||
663 | * @return array |
||
664 | */ |
||
665 | public static function get_valid_options( $input, $options ) { |
||
683 | |||
684 | /** |
||
685 | * Get an array of active sidebars |
||
686 | * |
||
687 | * @return array |
||
688 | */ |
||
689 | public static function get_active_sidebars() { |
||
708 | } |
||
709 |