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 PodsForm 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 PodsForm, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class PodsForm { |
||
|
|||
6 | |||
7 | /** |
||
8 | * @var PodsForm |
||
9 | */ |
||
10 | protected static $instance = null; |
||
11 | |||
12 | /** |
||
13 | * @var string |
||
14 | */ |
||
15 | static $field = null; |
||
16 | |||
17 | /** |
||
18 | * @var string |
||
19 | */ |
||
20 | static $field_group = null; |
||
21 | |||
22 | /** |
||
23 | * @var string |
||
24 | */ |
||
25 | static $field_type = null; |
||
26 | |||
27 | /** |
||
28 | * @var array |
||
29 | */ |
||
30 | static $field_types = array(); |
||
31 | |||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | static $loaded = array(); |
||
36 | |||
37 | /** |
||
38 | * @var int |
||
39 | */ |
||
40 | static $form_counter = 0; |
||
41 | |||
42 | /** |
||
43 | * Singleton handling for a basic pods_form() request |
||
44 | * |
||
45 | * @return \PodsForm |
||
46 | * |
||
47 | * @since 2.3.5 |
||
48 | */ |
||
49 | public static function init () { |
||
55 | |||
56 | /** |
||
57 | * Master handler for all field / form methods |
||
58 | * |
||
59 | * @return \PodsForm |
||
60 | * |
||
61 | * @license http://www.gnu.org/licenses/gpl-2.0.html |
||
62 | * @since 2.0 |
||
63 | */ |
||
64 | private function __construct() { |
||
67 | |||
68 | /** |
||
69 | * Prevent clones |
||
70 | * |
||
71 | * @since 2.3 |
||
72 | */ |
||
73 | private function __clone() { |
||
76 | |||
77 | /** |
||
78 | * Output a field's label |
||
79 | * |
||
80 | * @since 2.0 |
||
81 | */ |
||
82 | |||
83 | /** |
||
84 | * Output a field's label |
||
85 | * |
||
86 | * @param string $name Field name |
||
87 | * @param string $label Label text |
||
88 | * @param string $help Help text |
||
89 | * @param array $options Field options |
||
90 | * |
||
91 | * @return string Label HTML |
||
92 | * |
||
93 | * @since 2.0 |
||
94 | */ |
||
95 | public static function label( $name, $label, $help = '', $options = null ) { |
||
128 | |||
129 | /** |
||
130 | * Output a Field Comment Paragraph |
||
131 | * |
||
132 | * @param string $name Field name |
||
133 | * @param string $message Field comments |
||
134 | * @param array $options Field options |
||
135 | * |
||
136 | * @return string Comment HTML |
||
137 | * |
||
138 | * @since 2.0 |
||
139 | */ |
||
140 | public static function comment( $name, $message = null, $options = null ) { |
||
165 | |||
166 | /** |
||
167 | * Output a field |
||
168 | * |
||
169 | * @param string $name Field name |
||
170 | * @param mixed $value Field value |
||
171 | * @param string $type Field type |
||
172 | * @param array $options Field options |
||
173 | * @param array $pod Pod data |
||
174 | * @param int $id Item ID |
||
175 | * |
||
176 | * @return string Field HTML |
||
177 | * |
||
178 | * @since 2.0 |
||
179 | */ |
||
180 | public static function field( $name, $value, $type = 'text', $options = null, $pod = null, $id = null ) { |
||
230 | |||
231 | /** |
||
232 | * Output a flexible relationship button |
||
233 | * |
||
234 | * @param array $field Field options array |
||
235 | * |
||
236 | * @param int $item_id |
||
237 | * |
||
238 | * @return string Button HTML |
||
239 | * |
||
240 | * @since 2.7 |
||
241 | */ |
||
242 | public static function flex_relationship( $field, $item_id ) { |
||
243 | |||
244 | $output = ''; |
||
245 | |||
246 | // Early exit if any of: |
||
247 | // * Not admin |
||
248 | // * this isn't a flexible relationship enabled field |
||
249 | // * we're already in a modal |
||
250 | if ( ! is_admin() || empty( $field[ 'type' ] ) || 'pick' != $field[ 'type' ] || empty( $field[ 'pick_flexible' ] ) || pods_is_modal_window() ) { |
||
251 | return $output; |
||
252 | } |
||
253 | |||
254 | wp_enqueue_style( 'responsive-modal' ); |
||
255 | wp_enqueue_script( 'responsive-modal' ); |
||
256 | |||
257 | // Set the file name and args based on the content type of the relationship |
||
258 | switch ( $field[ 'pick_object' ] ) { |
||
259 | case 'post_type': |
||
260 | $file_name = 'post-new.php'; |
||
261 | $query_args = array( |
||
262 | 'post_type' => $field[ 'pick_val' ], |
||
263 | ); |
||
264 | break; |
||
265 | |||
266 | case 'taxonomy': |
||
267 | $file_name = 'edit-tags.php'; |
||
268 | $query_args = array( |
||
269 | 'taxonomy' => $field[ 'pick_val' ], |
||
270 | ); |
||
271 | break; |
||
272 | |||
273 | case 'user': |
||
274 | $file_name = 'user-new.php'; |
||
275 | $query_args = array(); |
||
276 | break; |
||
277 | |||
278 | case 'pod': |
||
279 | $file_name = 'admin.php'; |
||
280 | $query_args = array( |
||
281 | 'page' => 'pods-manage-' . $field[ 'pick_val' ], |
||
282 | 'action' => 'add' |
||
283 | ); |
||
284 | break; |
||
285 | |||
286 | // Something unsupported |
||
287 | default: |
||
288 | return $output; |
||
289 | break; |
||
290 | } |
||
291 | |||
292 | // Add args we always need |
||
293 | $query_args = array_merge( |
||
294 | $query_args, |
||
295 | array( |
||
296 | 'pods_modal' => '1', // @todo: Replace string literal with defined constant |
||
297 | ) |
||
298 | ); |
||
299 | |||
300 | // Assemble the URL |
||
301 | $url = add_query_arg( $query_args, admin_url( $file_name ) ); |
||
302 | |||
303 | ob_start(); |
||
304 | |||
305 | ?> |
||
306 | <div class="podsform-flex-relationship-container"> |
||
307 | <a href="<?php echo esc_url( $url ); ?>" |
||
308 | data-modal-show="yes please, if it's no trouble" |
||
309 | id="pods-related-edit-<?php echo esc_attr( $field[ 'name' ] ); ?>" |
||
310 | class="button pods-related-edit pods-modal" |
||
311 | data-pod-id="<?php echo esc_attr( $field[ 'pod_id' ] ); ?>" |
||
312 | data-field-id="<?php echo esc_attr( $field[ 'id' ] ); ?>" |
||
313 | data-item-id="<?php echo esc_attr( $item_id ); ?>"> |
||
314 | Add New |
||
315 | </a> |
||
316 | </div> |
||
317 | <?php |
||
318 | |||
319 | $output = ob_get_clean(); |
||
320 | |||
321 | // @todo: add a filter |
||
322 | return $output; |
||
323 | } |
||
324 | |||
325 | /** |
||
326 | * Output field type 'db' |
||
327 | * |
||
328 | * Used for field names and other places where only [a-z0-9_] is accepted |
||
329 | * |
||
330 | * @since 2.0 |
||
331 | */ |
||
332 | View Code Duplication | protected static function field_db( $name, $value = null, $options = null ) { |
|
343 | |||
344 | /** |
||
345 | * Output a hidden field |
||
346 | */ |
||
347 | View Code Duplication | protected static function field_hidden( $name, $value = null, $options = null ) { |
|
358 | |||
359 | /** |
||
360 | * Returns a submit button, with provided text and appropriate class, copied from WP Core for use on the frontend |
||
361 | * |
||
362 | * @see get_submit_button |
||
363 | * |
||
364 | * @param string $text The text of the button (defaults to 'Save Changes') |
||
365 | * @param string $type The type of button. One of: primary, secondary, delete |
||
366 | * @param string $name The HTML name of the submit button. Defaults to "submit". If no id attribute |
||
367 | * is given in $other_attributes below, $name will be used as the button's id. |
||
368 | * @param bool $wrap True if the output button should be wrapped in a paragraph tag, |
||
369 | * false otherwise. Defaults to true |
||
370 | * @param array|string $other_attributes Other attributes that should be output with the button, |
||
371 | * mapping attributes to their values, such as array( 'tabindex' => '1' ). |
||
372 | * These attributes will be output as attribute="value", such as tabindex="1". |
||
373 | * Defaults to no other attributes. Other attributes can also be provided as a |
||
374 | * string such as 'tabindex="1"', though the array format is typically cleaner. |
||
375 | * |
||
376 | * @since 3.0 |
||
377 | */ |
||
378 | public static function submit_button( $text = null, $type = 'primary large', $name = 'submit', $wrap = true, $other_attributes = null ) { |
||
443 | |||
444 | /** |
||
445 | * Output a row (label, field, and comment) |
||
446 | * |
||
447 | * @param string $name Field name |
||
448 | * @param mixed $value Field value |
||
449 | * @param string $type Field type |
||
450 | * @param array $options Field options |
||
451 | * @param array $pod Pod data |
||
452 | * @param int $id Item ID |
||
453 | * |
||
454 | * @return string Row HTML |
||
455 | * |
||
456 | * @since 2.0 |
||
457 | */ |
||
458 | public static function row( $name, $value, $type = 'text', $options = null, $pod = null, $id = null ) { |
||
469 | |||
470 | /** |
||
471 | * Output a field's attributes |
||
472 | * |
||
473 | * @since 2.0 |
||
474 | */ |
||
475 | public static function attributes( $attributes, $name = null, $type = null, $options = null ) { |
||
485 | |||
486 | /** |
||
487 | * Output a field's data (for use with jQuery) |
||
488 | * |
||
489 | * @since 2.0 |
||
490 | */ |
||
491 | public static function data( $data, $name = null, $type = null, $options = null ) { |
||
506 | |||
507 | /** |
||
508 | * Merge attributes and handle classes |
||
509 | * |
||
510 | * @since 2.0 |
||
511 | */ |
||
512 | public static function merge_attributes( $attributes, $name = null, $type = null, $options = null, $classes = '' ) { |
||
578 | |||
579 | /** |
||
580 | * Setup options for a field and store them for later use |
||
581 | * |
||
582 | * @param $type |
||
583 | * @param $options |
||
584 | * |
||
585 | * @return array |
||
586 | * |
||
587 | * @static |
||
588 | * |
||
589 | * @since 2.0 |
||
590 | */ |
||
591 | public static function options( $type, $options ) { |
||
638 | |||
639 | /** |
||
640 | * Get options for a field type and setup defaults |
||
641 | * |
||
642 | * @static |
||
643 | * |
||
644 | * @param $type |
||
645 | * |
||
646 | * @return array|null |
||
647 | * |
||
648 | * @since 2.0 |
||
649 | */ |
||
650 | public static function options_setup( $type = null, $options = null ) { |
||
696 | |||
697 | /** |
||
698 | * Get Admin options for a field type and setup defaults |
||
699 | * |
||
700 | * @static |
||
701 | * |
||
702 | * @param $type |
||
703 | * |
||
704 | * @return array|null |
||
705 | * |
||
706 | * @since 2.0 |
||
707 | */ |
||
708 | public static function ui_options( $type ) { |
||
744 | |||
745 | /** |
||
746 | * Get options for a field and setup defaults |
||
747 | * |
||
748 | * |
||
749 | * @param null $fields |
||
750 | * @param null $core_defaults |
||
751 | * @param bool $single |
||
752 | * |
||
753 | * @return array|null |
||
754 | * |
||
755 | * @static |
||
756 | * @since 2.0 |
||
757 | */ |
||
758 | public static function fields_setup( $fields = null, $core_defaults = null, $single = false ) { |
||
795 | |||
796 | /** |
||
797 | * Get options for a field and setup defaults |
||
798 | * |
||
799 | * @static |
||
800 | * |
||
801 | * @param null $field |
||
802 | * @param null $core_defaults |
||
803 | * @param null $type |
||
804 | * |
||
805 | * @return array|null |
||
806 | * |
||
807 | * @since 2.0 |
||
808 | */ |
||
809 | public static function field_setup( $field = null, $core_defaults = null, $type = null ) { |
||
866 | |||
867 | /** |
||
868 | * Setup dependency / exclusion classes |
||
869 | * |
||
870 | * @param array $options array( 'depends-on' => ..., 'excludes-on' => ...) |
||
871 | * @param string $prefix |
||
872 | * |
||
873 | * @return string |
||
874 | * @static |
||
875 | * @since 2.0 |
||
876 | */ |
||
877 | public static function dependencies( $options, $prefix = '' ) { |
||
922 | |||
923 | /** |
||
924 | * Change the value of the field |
||
925 | * |
||
926 | * @param mixed $value |
||
927 | * @param string $name |
||
928 | * @param array $options |
||
929 | * @param array $fields |
||
930 | * @param array $pod |
||
931 | * @param int $id |
||
932 | * @param array $traverse |
||
933 | * |
||
934 | * @since 2.3 |
||
935 | */ |
||
936 | public static function value( $type, $value = null, $name = null, $options = null, $pod = null, $id = null, $traverse = null ) { |
||
964 | |||
965 | /** |
||
966 | * Change the way the value of the field is displayed with Pods::get |
||
967 | * |
||
968 | * @param mixed $value |
||
969 | * @param string $name |
||
970 | * @param array $options |
||
971 | * @param array $fields |
||
972 | * @param array $pod |
||
973 | * @param int $id |
||
974 | * @param array $traverse |
||
975 | * |
||
976 | * @since 2.0 |
||
977 | */ |
||
978 | public static function display( $type, $value = null, $name = null, $options = null, $pod = null, $id = null, $traverse = null ) { |
||
997 | |||
998 | /** |
||
999 | * Setup regex for JS / PHP |
||
1000 | * |
||
1001 | * @static |
||
1002 | * |
||
1003 | * @param $type |
||
1004 | * @param $options |
||
1005 | * |
||
1006 | * @return mixed|void |
||
1007 | * @since 2.0 |
||
1008 | */ |
||
1009 | View Code Duplication | public static function regex( $type, $options ) { |
|
1021 | |||
1022 | /** |
||
1023 | * Setup value preparation for sprintf |
||
1024 | * |
||
1025 | * @static |
||
1026 | * |
||
1027 | * @param $type |
||
1028 | * @param $options |
||
1029 | * |
||
1030 | * @return mixed|void |
||
1031 | * @since 2.0 |
||
1032 | */ |
||
1033 | View Code Duplication | public static function prepare( $type, $options ) { |
|
1045 | |||
1046 | /** |
||
1047 | * Validate a value before it's saved |
||
1048 | * |
||
1049 | * @param string $type |
||
1050 | * @param mixed $value |
||
1051 | * @param string $name |
||
1052 | * @param array $options |
||
1053 | * @param array $fields |
||
1054 | * @param array $pod |
||
1055 | * @param int $id |
||
1056 | * @param array|object $params |
||
1057 | * |
||
1058 | * @static |
||
1059 | * |
||
1060 | * @since 2.0 |
||
1061 | */ |
||
1062 | public static function validate( $type, $value, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) { |
||
1074 | |||
1075 | /** |
||
1076 | * Change the value or perform actions after validation but before saving to the DB |
||
1077 | * |
||
1078 | * @param string $type |
||
1079 | * @param mixed $value |
||
1080 | * @param int $id |
||
1081 | * @param string $name |
||
1082 | * @param array $options |
||
1083 | * @param array $fields |
||
1084 | * @param array $pod |
||
1085 | * @param object $params |
||
1086 | * |
||
1087 | * @static |
||
1088 | * |
||
1089 | * @since 2.0 |
||
1090 | */ |
||
1091 | View Code Duplication | public static function pre_save( $type, $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { |
|
1099 | |||
1100 | /** |
||
1101 | * Save the value to the DB |
||
1102 | * |
||
1103 | * @param string $type |
||
1104 | * @param mixed $value |
||
1105 | * @param int $id |
||
1106 | * @param string $name |
||
1107 | * @param array $options |
||
1108 | * @param array $fields |
||
1109 | * @param array $pod |
||
1110 | * @param object $params |
||
1111 | * |
||
1112 | * @static |
||
1113 | * |
||
1114 | * @since 2.3 |
||
1115 | */ |
||
1116 | View Code Duplication | public static function save( $type, $value, $id = null, $name = null, $options = null, $fields = null, $pod = null, $params = null ) { |
|
1126 | |||
1127 | /** |
||
1128 | * Delete the value from the DB |
||
1129 | * |
||
1130 | * @param string $type |
||
1131 | * @param int $id |
||
1132 | * @param string $name |
||
1133 | * @param array $options |
||
1134 | * @param array $pod |
||
1135 | * |
||
1136 | * @static |
||
1137 | * |
||
1138 | * @since 2.3 |
||
1139 | */ |
||
1140 | View Code Duplication | public static function delete( $type, $id = null, $name = null, $options = null, $pod = null ) { |
|
1150 | |||
1151 | /** |
||
1152 | * Check if a user has permission to be editing a field |
||
1153 | * |
||
1154 | * @param $type |
||
1155 | * @param null $name |
||
1156 | * @param null $options |
||
1157 | * @param null $fields |
||
1158 | * @param null $pod |
||
1159 | * @param null $id |
||
1160 | * @param null $params |
||
1161 | * |
||
1162 | * @static |
||
1163 | * |
||
1164 | * @since 2.0 |
||
1165 | */ |
||
1166 | public static function permission( $type, $name = null, $options = null, $fields = null, $pod = null, $id = null, $params = null ) { |
||
1173 | |||
1174 | /** |
||
1175 | * Parse the default the value |
||
1176 | * |
||
1177 | * @since 2.0 |
||
1178 | */ |
||
1179 | public static function default_value( $value, $type = 'text', $name = null, $options = null, $pod = null, $id = null ) { |
||
1205 | |||
1206 | /** |
||
1207 | * Clean a value for use in class / id |
||
1208 | * |
||
1209 | * @since 2.0 |
||
1210 | */ |
||
1211 | public static function clean( $input, $noarray = false, $db_field = false ) { |
||
1223 | |||
1224 | /** |
||
1225 | * Run admin_init methods for each field type |
||
1226 | * |
||
1227 | * @since 2.3 |
||
1228 | */ |
||
1229 | public function admin_init() { |
||
1252 | |||
1253 | /** |
||
1254 | * Autoload a Field Type's class |
||
1255 | * |
||
1256 | * @param string $field_type Field Type indentifier |
||
1257 | * @param string $file The Field Type class file location |
||
1258 | * |
||
1259 | * @return string |
||
1260 | * @access public |
||
1261 | * @static |
||
1262 | * @since 2.0 |
||
1263 | */ |
||
1264 | public static function field_loader( $field_type, $file = '' ) { |
||
1318 | |||
1319 | /** |
||
1320 | * Run a method from a Field Type's class |
||
1321 | * |
||
1322 | * @param string $field_type Field Type indentifier |
||
1323 | * @param string $method Method name |
||
1324 | * @param mixed $arg More arguments |
||
1325 | * |
||
1326 | * @return mixed |
||
1327 | * @access public |
||
1328 | * @static |
||
1329 | * @since 2.0 |
||
1330 | */ |
||
1331 | public static function field_method() { |
||
1347 | |||
1348 | /** |
||
1349 | * Add a new Pod field type |
||
1350 | * |
||
1351 | * @param string $type The new field type identifier |
||
1352 | * @param string $file The new field type class file location |
||
1353 | * |
||
1354 | * @return array Field Type data |
||
1355 | * |
||
1356 | * @since 2.3 |
||
1357 | */ |
||
1358 | public static function register_field_type( $type, $file = null ) { |
||
1376 | |||
1377 | /** |
||
1378 | * Get a list of all available field types and include |
||
1379 | * |
||
1380 | * @return array Registered Field Types data |
||
1381 | * |
||
1382 | * @since 2.3 |
||
1383 | */ |
||
1384 | public static function field_types() { |
||
1444 | |||
1445 | /** |
||
1446 | * Get list of available tableless field types |
||
1447 | * |
||
1448 | * @return array Tableless field types |
||
1449 | * |
||
1450 | * @since 2.3 |
||
1451 | */ |
||
1452 | View Code Duplication | public static function tableless_field_types() { |
|
1462 | |||
1463 | /** |
||
1464 | * Get list of available file field types |
||
1465 | * |
||
1466 | * @return array File field types |
||
1467 | * |
||
1468 | * @since 2.3 |
||
1469 | */ |
||
1470 | View Code Duplication | public static function file_field_types() { |
|
1480 | |||
1481 | /** |
||
1482 | * Get list of available repeatable field types |
||
1483 | * |
||
1484 | * @return array Repeatable field types |
||
1485 | * |
||
1486 | * @since 2.3 |
||
1487 | */ |
||
1488 | public static function repeatable_field_types() { |
||
1512 | |||
1513 | /** |
||
1514 | * Get list of available number field types |
||
1515 | * |
||
1516 | * @return array Number field types |
||
1517 | * |
||
1518 | * @since 2.3 |
||
1519 | */ |
||
1520 | View Code Duplication | public static function number_field_types() { |
|
1530 | |||
1531 | /** |
||
1532 | * Get list of available date field types |
||
1533 | * |
||
1534 | * @return array Date field types |
||
1535 | * |
||
1536 | * @since 2.3 |
||
1537 | */ |
||
1538 | View Code Duplication | public static function date_field_types() { |
|
1548 | |||
1549 | /** |
||
1550 | * Get list of available text field types |
||
1551 | * |
||
1552 | * @return array Text field types |
||
1553 | * |
||
1554 | * @since 2.3 |
||
1555 | */ |
||
1556 | public static function text_field_types() { |
||
1566 | |||
1567 | /** |
||
1568 | * Get list of available text field types |
||
1569 | * |
||
1570 | * @return array Text field types |
||
1571 | * |
||
1572 | * @since 2.3 |
||
1573 | */ |
||
1574 | public static function block_field_types() { |
||
1592 | |||
1593 | /** |
||
1594 | * Get list of available text field types |
||
1595 | * |
||
1596 | * @return array Text field types |
||
1597 | * |
||
1598 | * @since 2.3 |
||
1599 | */ |
||
1600 | public static function simple_tableless_objects() { |
||
1608 | |||
1609 | } |
||
1610 |