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:
| 1 | <?php |
||
| 12 | class Helper { |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Get a value formatted for end-users |
||
| 16 | * |
||
| 17 | * @param int $object_id Object id to get value for (e.g. post_id, term_id etc.) |
||
| 18 | * @param string $container_type Container type to search in |
||
| 19 | * @param string $field_name Field name |
||
| 20 | * @return mixed |
||
| 21 | */ |
||
| 22 | public static function get_value( $object_id, $container_type, $field_name ) { |
||
| 23 | $repository = App::resolve( 'container_repository' ); |
||
| 24 | $field = $repository->get_field_in_containers( $field_name, $container_type ); |
||
| 25 | |||
| 26 | if ( ! $field ) { |
||
| 27 | Incorrect_Syntax_Exception::raise( 'Could not find a field which satisfies the supplied pattern: ' . $field_name ); |
||
| 28 | } |
||
| 29 | |||
| 30 | $clone = clone $field; |
||
| 31 | if ( $object_id !== null ) { |
||
| 32 | $clone->get_datastore()->set_id( $object_id ); |
||
| 33 | } |
||
| 34 | |||
| 35 | $clone->load(); |
||
| 36 | return $clone->get_formatted_value(); |
||
| 37 | } |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Set value for a field |
||
| 41 | * |
||
| 42 | * @param int $object_id Object id to get value for (e.g. post_id, term_id etc.) |
||
| 43 | * @param string $container_type Container type to search in |
||
| 44 | * @param string $field_name Field name |
||
| 45 | * @param array $value Field expects a `value_set`; Complex_Field expects a `value_tree` - refer to DEVELOPMENT.md |
||
| 46 | */ |
||
| 47 | public static function set_value( $object_id, $container_type, $field_name, $value ) { |
||
| 48 | $repository = App::resolve( 'container_repository' ); |
||
| 49 | $field = $repository->get_field_in_containers( $field_name, $container_type ); |
||
| 50 | |||
| 51 | if ( ! $field ) { |
||
| 52 | Incorrect_Syntax_Exception::raise( 'Could not find a field which satisfies the supplied pattern: ' . $field_name ); |
||
| 53 | } |
||
| 54 | |||
| 55 | $clone = clone $field; |
||
| 56 | if ( $object_id !== null ) { |
||
| 57 | $clone->get_datastore()->set_id( $object_id ); |
||
| 58 | } |
||
| 59 | |||
| 60 | if ( is_a( $clone, 'Carbon_Fields\\Field\\Complex_Field' ) ) { |
||
| 61 | $value_tree = ( ! empty( $value ) ) ? $value : array( 'value_set' => array(), 'groups' => array() ); |
||
| 62 | $clone->set_value( $value_tree['value_set'] ); |
||
| 63 | $clone->set_value_tree( $value_tree ); |
||
| 64 | } else { |
||
| 65 | $clone->set_value( $value ); |
||
| 66 | } |
||
| 67 | $clone->save(); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Shorthand for get_post_meta(). |
||
| 72 | * Uses the ID of the current post in the loop. |
||
| 73 | * |
||
| 74 | * @param string $name Custom field name. |
||
| 75 | * @return mixed Meta value. |
||
| 76 | */ |
||
| 77 | public static function get_the_post_meta( $name ) { |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Get post meta field for a post. |
||
| 83 | * |
||
| 84 | * @param int $id Post ID. |
||
| 85 | * @param string $name Custom field name. |
||
| 86 | * @return mixed Meta value. |
||
| 87 | */ |
||
| 88 | public static function get_post_meta( $id, $name ) { |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Set post meta field for a post. |
||
| 94 | * |
||
| 95 | * @param int $id Post ID |
||
| 96 | * @param string $name Custom field name |
||
| 97 | * @param array $value |
||
| 98 | * @return bool Success |
||
| 99 | */ |
||
| 100 | public static function set_post_meta( $id, $name, $value ) { |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Get theme option field value. |
||
| 106 | * |
||
| 107 | * @param string $name Custom field name |
||
| 108 | * @return mixed Option value |
||
| 109 | */ |
||
| 110 | public static function get_theme_option( $name ) { |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Set theme option field value. |
||
| 116 | * |
||
| 117 | * @param string $name Field name |
||
| 118 | * @param array $value |
||
| 119 | * @return bool Success |
||
| 120 | */ |
||
| 121 | public static function set_theme_option( $name, $value ) { |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Get term meta field for a term. |
||
| 127 | * |
||
| 128 | * @param int $id Term ID. |
||
| 129 | * @param string $name Custom field name. |
||
| 130 | * @return mixed Meta value. |
||
| 131 | */ |
||
| 132 | public static function get_term_meta( $id, $name ) { |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Set term meta field for a term. |
||
| 138 | * |
||
| 139 | * @param int $id Term ID |
||
| 140 | * @param string $name Field name |
||
| 141 | * @param array $value |
||
| 142 | * @return bool Success |
||
| 143 | */ |
||
| 144 | public static function set_term_meta( $id, $name, $value ) { |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Get user meta field for a user. |
||
| 150 | * |
||
| 151 | * @param int $id User ID. |
||
| 152 | * @param string $name Custom field name. |
||
| 153 | * @return mixed Meta value. |
||
| 154 | */ |
||
| 155 | public static function get_user_meta( $id, $name ) { |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Set user meta field for a user. |
||
| 161 | * |
||
| 162 | * @param int $id User ID |
||
| 163 | * @param string $name Field name |
||
| 164 | * @param array $value |
||
| 165 | * @return bool Success |
||
| 166 | */ |
||
| 167 | public static function set_user_meta( $id, $name, $value ) { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Get comment meta field for a comment. |
||
| 173 | * |
||
| 174 | * @param int $id Comment ID. |
||
| 175 | * @param string $name Custom field name. |
||
| 176 | * @return mixed Meta value. |
||
| 177 | */ |
||
| 178 | public static function get_comment_meta( $id, $name ) { |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Set comment meta field for a comment. |
||
| 184 | * |
||
| 185 | * @param int $id Comment ID |
||
| 186 | * @param string $name Field name |
||
| 187 | * @param array $value |
||
| 188 | * @return bool Success |
||
| 189 | */ |
||
| 190 | public static function set_comment_meta( $id, $name, $value ) { |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Recursive sorting function by array key. |
||
| 196 | * |
||
| 197 | * @param array &$array The input array. |
||
| 198 | * @param int $sort_flags Flags for controlling sorting behavior. |
||
| 199 | * @return array Sorted array. |
||
| 200 | */ |
||
| 201 | public static function ksort_recursive( &$array, $sort_flags = SORT_REGULAR ) { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Get the relation type from an array similar to how meta_query works in WP_Query |
||
| 214 | * |
||
| 215 | * @param array $array |
||
| 216 | * @param array<string> $allowed_relations |
||
| 217 | * @param string $relation_key |
||
| 218 | * @return string |
||
| 219 | */ |
||
| 220 | public static function get_relation_type_from_array( $array, $allowed_relations = array( 'AND', 'OR' ), $relation_key = 'relation' ) { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Normalize a type string representing an object type |
||
| 239 | * |
||
| 240 | * @param string $type |
||
| 241 | * @return string |
||
| 242 | */ |
||
| 243 | public static function normalize_type( $type ) { |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Convert a string representing an object type to a fully qualified class name |
||
| 253 | * |
||
| 254 | * @param string $type |
||
| 255 | * @param string $namespace |
||
| 256 | * @param string $class_suffix |
||
| 257 | * @return string |
||
| 258 | */ |
||
| 259 | public static function type_to_class( $type, $namespace = '', $class_suffix = '' ) { |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Convert a string representing an object type to a fully qualified class name |
||
| 275 | * |
||
| 276 | * @param string $class |
||
| 277 | * @param string $class_suffix |
||
| 278 | * @return string |
||
| 279 | */ |
||
| 280 | public static function class_to_type( $class, $class_suffix = '' ) { |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Get an array of sanitized html classes |
||
| 294 | * |
||
| 295 | * @param string|array<strnig> $classes |
||
| 296 | * @return array<string> |
||
| 297 | */ |
||
| 298 | public static function sanitize_classes( $classes ) { |
||
| 305 | } |
||
| 306 |