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 |
||
| 11 | class Helper { |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Get a value formatted for end-users |
||
| 15 | * |
||
| 16 | * @param int $object_id Object id to get value for (e.g. post_id, term_id etc.) |
||
| 17 | * @param string $container_type Container type to search in |
||
| 18 | * @param string $field_name Field name |
||
| 19 | * @return mixed |
||
| 20 | */ |
||
| 21 | public static function get_field_clone( $object_id, $container_type, $container_id, $field_name ) { |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Get a value formatted for end-users |
||
| 43 | * |
||
| 44 | * @param int $object_id Object id to get value for (e.g. post_id, term_id etc.) |
||
| 45 | * @param string $container_type Container type to search in |
||
| 46 | * @param string $field_name Field name |
||
| 47 | * @return mixed |
||
| 48 | */ |
||
| 49 | public static function get_value( $object_id, $container_type, $container_id, $field_name ) { |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Set value for a field |
||
| 62 | * |
||
| 63 | * @param int $object_id Object id to get value for (e.g. post_id, term_id etc.) |
||
| 64 | * @param string $container_type Container type to search in |
||
| 65 | * @param string $field_name Field name |
||
| 66 | * @param array $value Field expects a `value_set`; Complex_Field expects a `value_tree` - refer to DEVELOPMENT.md |
||
| 67 | */ |
||
| 68 | public static function set_value( $object_id, $container_type, $container_id, $field_name, $value ) { |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Shorthand for get_post_meta(). |
||
| 82 | * Uses the ID of the current post in the loop. |
||
| 83 | * |
||
| 84 | * @param string $name Custom field name. |
||
| 85 | * @return mixed Meta value. |
||
| 86 | */ |
||
| 87 | public static function get_the_post_meta( $name ) { |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Get post meta field for a post. |
||
| 93 | * |
||
| 94 | * @param int $id Post ID. |
||
| 95 | * @param string $name Custom field name. |
||
| 96 | * @return mixed Meta value. |
||
| 97 | */ |
||
| 98 | public static function get_post_meta( $id, $name, $container_id = '' ) { |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Set post meta field for a post. |
||
| 104 | * |
||
| 105 | * @param int $id Post ID |
||
| 106 | * @param string $name Custom field name |
||
| 107 | * @param array $value |
||
| 108 | * @return bool Success |
||
| 109 | */ |
||
| 110 | public static function set_post_meta( $id, $name, $value, $container_id = '' ) { |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Get theme option field value. |
||
| 116 | * |
||
| 117 | * @param string $name Custom field name |
||
| 118 | * @return mixed Option value |
||
| 119 | */ |
||
| 120 | public static function get_theme_option( $name, $container_id = '' ) { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Set theme option field value. |
||
| 126 | * |
||
| 127 | * @param string $name Field name |
||
| 128 | * @param array $value |
||
| 129 | * @return bool Success |
||
| 130 | */ |
||
| 131 | public static function set_theme_option( $name, $value, $container_id = '' ) { |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Get term meta field for a term. |
||
| 137 | * |
||
| 138 | * @param int $id Term ID. |
||
| 139 | * @param string $name Custom field name. |
||
| 140 | * @return mixed Meta value. |
||
| 141 | */ |
||
| 142 | public static function get_term_meta( $id, $name, $container_id = '' ) { |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Set term meta field for a term. |
||
| 148 | * |
||
| 149 | * @param int $id Term ID |
||
| 150 | * @param string $name Field name |
||
| 151 | * @param array $value |
||
| 152 | * @return bool Success |
||
| 153 | */ |
||
| 154 | public static function set_term_meta( $id, $name, $value, $container_id = '' ) { |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Get user meta field for a user. |
||
| 160 | * |
||
| 161 | * @param int $id User ID. |
||
| 162 | * @param string $name Custom field name. |
||
| 163 | * @return mixed Meta value. |
||
| 164 | */ |
||
| 165 | public static function get_user_meta( $id, $name, $container_id = '' ) { |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Set user meta field for a user. |
||
| 171 | * |
||
| 172 | * @param int $id User ID |
||
| 173 | * @param string $name Field name |
||
| 174 | * @param array $value |
||
| 175 | * @return bool Success |
||
| 176 | */ |
||
| 177 | public static function set_user_meta( $id, $name, $value, $container_id = '' ) { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Get comment meta field for a comment. |
||
| 183 | * |
||
| 184 | * @param int $id Comment ID. |
||
| 185 | * @param string $name Custom field name. |
||
| 186 | * @return mixed Meta value. |
||
| 187 | */ |
||
| 188 | public static function get_comment_meta( $id, $name, $container_id = '' ) { |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Set comment meta field for a comment. |
||
| 194 | * |
||
| 195 | * @param int $id Comment ID |
||
| 196 | * @param string $name Field name |
||
| 197 | * @param array $value |
||
| 198 | * @return bool Success |
||
| 199 | */ |
||
| 200 | public static function set_comment_meta( $id, $name, $value, $container_id = '' ) { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Recursive sorting function by array key. |
||
| 206 | * |
||
| 207 | * @param array &$array The input array. |
||
| 208 | * @param int $sort_flags Flags for controlling sorting behavior. |
||
| 209 | * @return array Sorted array. |
||
| 210 | */ |
||
| 211 | public static function ksort_recursive( &$array, $sort_flags = SORT_REGULAR ) { |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Get the relation type from an array similar to how meta_query works in WP_Query |
||
| 224 | * |
||
| 225 | * @param array $array |
||
| 226 | * @param array<string> $allowed_relations |
||
| 227 | * @param string $relation_key |
||
| 228 | * @return string |
||
| 229 | */ |
||
| 230 | public static function get_relation_type_from_array( $array, $allowed_relations = array( 'AND', 'OR' ), $relation_key = 'relation' ) { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Normalize a label by updating case, stripping common prefixes etc. |
||
| 249 | * |
||
| 250 | * @param string $label |
||
| 251 | * @return string |
||
| 252 | */ |
||
| 253 | View Code Duplication | public static function normalize_label( $label ) { |
|
| 265 | |||
| 266 | /** |
||
| 267 | * Normalize a type string representing an object type |
||
| 268 | * |
||
| 269 | * @param string $type |
||
| 270 | * @return string |
||
| 271 | */ |
||
| 272 | View Code Duplication | public static function normalize_type( $type ) { |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Convert a string representing an object type to a fully qualified class name |
||
| 282 | * |
||
| 283 | * @param string $type |
||
| 284 | * @param string $namespace |
||
| 285 | * @param string $class_suffix |
||
| 286 | * @return string |
||
| 287 | */ |
||
| 288 | public static function type_to_class( $type, $namespace = '', $class_suffix = '' ) { |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Convert a string representing an object type to a fully qualified class name |
||
| 304 | * |
||
| 305 | * @param string $class |
||
| 306 | * @param string $class_suffix |
||
| 307 | * @return string |
||
| 308 | */ |
||
| 309 | public static function class_to_type( $class, $class_suffix = '' ) { |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Get an array of sanitized html classes |
||
| 324 | * |
||
| 325 | * @param string|array<string> $classes |
||
| 326 | * @return array<string> |
||
| 327 | */ |
||
| 328 | public static function sanitize_classes( $classes ) { |
||
| 335 | } |
||
| 336 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.