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 boolean |
||
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 mixed |
||
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 |
||
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 ) { |
||
102 | return static::with_field_clone( |
||
103 | $object_id, |
||
104 | $container_type, |
||
105 | $container_id, |
||
106 | $field_name, |
||
107 | function( $field ) { |
||
108 | if ( ! $field ) { |
||
109 | return ''; |
||
110 | } |
||
111 | |||
112 | $field->load(); |
||
113 | return $field->get_formatted_value(); |
||
114 | } |
||
115 | ); |
||
116 | } |
||
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_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 | |||
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 = '' ) { |
||
170 | |||
171 | /** |
||
172 | * Set post meta field for a post. |
||
173 | * |
||
174 | * @param int $id Post ID |
||
175 | * @param string $name Field name |
||
176 | * @param array $value |
||
177 | * @param string $container_id |
||
178 | */ |
||
179 | public static function set_post_meta( $id, $name, $value, $container_id = '' ) { |
||
182 | |||
183 | /** |
||
184 | * Get theme option field value. |
||
185 | * |
||
186 | * @param string $name Field name |
||
187 | * @param string $container_id |
||
188 | * @return mixed |
||
189 | */ |
||
190 | public static function get_theme_option( $name, $container_id = '' ) { |
||
193 | |||
194 | /** |
||
195 | * Set theme option field value. |
||
196 | * |
||
197 | * @param string $name Field name |
||
198 | * @param array $value |
||
199 | * @param string $container_id |
||
200 | */ |
||
201 | public static function set_theme_option( $name, $value, $container_id = '' ) { |
||
204 | |||
205 | /** |
||
206 | * Get network option field value for the main site. |
||
207 | * |
||
208 | * @param string $name Field name |
||
209 | * @param string $container_id |
||
210 | * @return mixed |
||
211 | */ |
||
212 | public static function get_the_network_option( $name, $container_id = '' ) { |
||
216 | |||
217 | /** |
||
218 | * Get network option field value for a site. |
||
219 | * |
||
220 | * @param string $id Site ID |
||
221 | * @param string $name Field name |
||
222 | * @param string $container_id |
||
223 | * @return mixed |
||
224 | */ |
||
225 | public static function get_network_option( $id, $name, $container_id = '' ) { |
||
228 | |||
229 | /** |
||
230 | * Set network option field value for a site. |
||
231 | * |
||
232 | * @param string $id Site ID |
||
233 | * @param string $name Field name |
||
234 | * @param string $container_id |
||
235 | * @return mixed |
||
236 | */ |
||
237 | public static function set_network_option( $id, $name, $value, $container_id = '' ) { |
||
240 | |||
241 | /** |
||
242 | * Get term meta field for a term. |
||
243 | * |
||
244 | * @param int $id Term ID |
||
245 | * @param string $name Field name |
||
246 | * @param string $container_id |
||
247 | * @return mixed |
||
248 | */ |
||
249 | public static function get_term_meta( $id, $name, $container_id = '' ) { |
||
252 | |||
253 | /** |
||
254 | * Set term meta field for a term. |
||
255 | * |
||
256 | * @param int $id Term ID |
||
257 | * @param string $name Field name |
||
258 | * @param array $value |
||
259 | * @param string $container_id |
||
260 | */ |
||
261 | public static function set_term_meta( $id, $name, $value, $container_id = '' ) { |
||
264 | |||
265 | /** |
||
266 | * Get user meta field for a user. |
||
267 | * |
||
268 | * @param int $id User ID |
||
269 | * @param string $name Field name |
||
270 | * @param string $container_id |
||
271 | * @return mixed |
||
272 | */ |
||
273 | public static function get_user_meta( $id, $name, $container_id = '' ) { |
||
276 | |||
277 | /** |
||
278 | * Set user meta field for a user. |
||
279 | * |
||
280 | * @param int $id User ID |
||
281 | * @param string $name Field name |
||
282 | * @param array $value |
||
283 | * @param string $container_id |
||
284 | */ |
||
285 | public static function set_user_meta( $id, $name, $value, $container_id = '' ) { |
||
288 | |||
289 | /** |
||
290 | * Get comment meta field for a comment. |
||
291 | * |
||
292 | * @param int $id Comment ID |
||
293 | * @param string $name Field name |
||
294 | * @param string $container_id |
||
295 | * @return mixed |
||
296 | */ |
||
297 | public static function get_comment_meta( $id, $name, $container_id = '' ) { |
||
300 | |||
301 | /** |
||
302 | * Set comment meta field for a comment. |
||
303 | * |
||
304 | * @param int $id Comment ID |
||
305 | * @param string $name Field name |
||
306 | * @param array $value |
||
307 | * @param string $container_id |
||
308 | */ |
||
309 | public static function set_comment_meta( $id, $name, $value, $container_id = '' ) { |
||
312 | |||
313 | /** |
||
314 | * Get nav menu item meta field for a nav menu item. |
||
315 | * |
||
316 | * @param int $id Nav menu item ID |
||
317 | * @param string $name Field name |
||
318 | * @param string $container_id |
||
319 | * @return mixed |
||
320 | */ |
||
321 | public static function get_nav_menu_item_meta( $id, $name, $container_id = '' ) { |
||
324 | |||
325 | /** |
||
326 | * Set nav menu item meta field for a nav menu item. |
||
327 | * |
||
328 | * @param int $id Nav menu item ID |
||
329 | * @param string $name Field name |
||
330 | * @param array $value |
||
331 | * @param string $container_id |
||
332 | */ |
||
333 | public static function set_nav_menu_item_meta( $id, $name, $value, $container_id = '' ) { |
||
336 | |||
337 | /** |
||
338 | * Recursive sorting function by array key. |
||
339 | * |
||
340 | * @param array &$array The input array. |
||
341 | * @param int $sort_flags Flags for controlling sorting behavior. |
||
342 | * @return boolean |
||
343 | */ |
||
344 | public static function ksort_recursive( &$array, $sort_flags = SORT_REGULAR ) { |
||
354 | |||
355 | /** |
||
356 | * Get the relation type from an array similar to how meta_query works in WP_Query |
||
357 | * |
||
358 | * @param array $array |
||
359 | * @param array<string> $allowed_relations |
||
360 | * @param string $relation_key |
||
361 | * @return string |
||
362 | */ |
||
363 | public static function get_relation_type_from_array( $array, $allowed_relations = array( 'AND', 'OR' ), $relation_key = 'relation' ) { |
||
379 | |||
380 | /** |
||
381 | * Normalize a label by updating case, stripping common prefixes etc. |
||
382 | * |
||
383 | * @param string $label |
||
384 | * @return string |
||
385 | */ |
||
386 | View Code Duplication | public static function normalize_label( $label ) { |
|
398 | |||
399 | /** |
||
400 | * Normalize a type string representing an object type |
||
401 | * |
||
402 | * @param string $type |
||
403 | * @return string |
||
404 | */ |
||
405 | View Code Duplication | public static function normalize_type( $type ) { |
|
412 | |||
413 | /** |
||
414 | * Convert a string representing an object type to a fully qualified class name |
||
415 | * |
||
416 | * @param string $type |
||
417 | * @param string $namespace |
||
418 | * @param string $class_suffix |
||
419 | * @return string |
||
420 | */ |
||
421 | public static function type_to_class( $type, $namespace = '', $class_suffix = '' ) { |
||
434 | |||
435 | /** |
||
436 | * Convert a string representing an object type to a fully qualified class name |
||
437 | * |
||
438 | * @param string $class |
||
439 | * @param string $class_suffix |
||
440 | * @return string |
||
441 | */ |
||
442 | public static function class_to_type( $class, $class_suffix = '' ) { |
||
454 | |||
455 | /** |
||
456 | * Get an array of sanitized html classes |
||
457 | * |
||
458 | * @param string|array<string> $classes |
||
459 | * @return array<string> |
||
460 | */ |
||
461 | public static function sanitize_classes( $classes ) { |
||
468 | |||
469 | /** |
||
470 | * Check if an id or name for containers and fields is valid |
||
471 | * |
||
472 | * @param string $id |
||
473 | * @return boolean |
||
474 | */ |
||
475 | public static function is_valid_entity_id( $id ) { |
||
478 | |||
479 | /** |
||
480 | * Return a partial regex pettern matching allowed field name characters |
||
481 | * |
||
482 | * @return string |
||
483 | */ |
||
484 | public static function get_field_name_characters_pattern() { |
||
487 | |||
488 | /** |
||
489 | * Get an attachment ID given a file URL |
||
490 | * Modified version of https://wpscholar.com/blog/get-attachment-id-from-wp-image-url/ |
||
491 | * |
||
492 | * @param string $url |
||
493 | * @return integet |
||
494 | */ |
||
495 | public static function get_attachment_id( $url ) { |
||
531 | |||
532 | /** |
||
533 | * Returns attachment metadata from an ID. |
||
534 | * |
||
535 | * @param string $id |
||
536 | * @param string $type Value Type. Can be either id or url |
||
537 | * @return boolean |
||
538 | */ |
||
539 | public static function get_attachment_metadata( $id, $type ) { |
||
621 | |||
622 | /** |
||
623 | * Get the current $_POST or $_GET input array with compacted input values merged in |
||
624 | * |
||
625 | * @return array |
||
626 | */ |
||
627 | public static function input() { |
||
637 | |||
638 | /** |
||
639 | * Get a copy of the passed array with compacted input values merged in |
||
640 | * |
||
641 | * @param array $input |
||
642 | * @return array |
||
643 | */ |
||
644 | public static function expand_compacted_input( $input ) { |
||
651 | |||
652 | /** |
||
653 | * Get valid input from an input array compared to predefined options |
||
654 | * |
||
655 | * @param array $input |
||
656 | * @param array $options |
||
657 | * @return array |
||
658 | */ |
||
659 | public static function get_valid_options( $input, $options ) { |
||
677 | |||
678 | /** |
||
679 | * Get an array of active sidebars |
||
680 | * |
||
681 | * @return array |
||
682 | */ |
||
683 | public static function get_active_sidebars() { |
||
702 | } |
||
703 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.