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 | View Code Duplication | public static function get_value( $object_id, $container_type, $field_name ) { |
|
37 | |||
38 | /** |
||
39 | * Set value for a field |
||
40 | * |
||
41 | * @param int $object_id Object id to get value for (e.g. post_id, term_id etc.) |
||
42 | * @param string $container_type Container type to search in |
||
43 | * @param string $field_name Field name |
||
44 | * @param array $value Field expects a `value_set`; Complex_Field expects a `value_tree` - refer to DEVELOPMENT.md |
||
45 | */ |
||
46 | View Code Duplication | public static function set_value( $object_id, $container_type, $field_name, $value ) { |
|
63 | |||
64 | /** |
||
65 | * Shorthand for get_post_meta(). |
||
66 | * Uses the ID of the current post in the loop. |
||
67 | * |
||
68 | * @param string $name Custom field name. |
||
69 | * @return mixed Meta value. |
||
70 | */ |
||
71 | public static function get_the_post_meta( $name ) { |
||
74 | |||
75 | /** |
||
76 | * Get post meta field for a post. |
||
77 | * |
||
78 | * @param int $id Post ID. |
||
79 | * @param string $name Custom field name. |
||
80 | * @return mixed Meta value. |
||
81 | */ |
||
82 | public static function get_post_meta( $id, $name ) { |
||
85 | |||
86 | /** |
||
87 | * Set post meta field for a post. |
||
88 | * |
||
89 | * @param int $id Post ID |
||
90 | * @param string $name Custom field name |
||
91 | * @param array $value |
||
92 | * @return bool Success |
||
93 | */ |
||
94 | public static function set_post_meta( $id, $name, $value ) { |
||
97 | |||
98 | /** |
||
99 | * Get theme option field value. |
||
100 | * |
||
101 | * @param string $name Custom field name |
||
102 | * @return mixed Option value |
||
103 | */ |
||
104 | public static function get_theme_option( $name ) { |
||
107 | |||
108 | /** |
||
109 | * Set theme option field value. |
||
110 | * |
||
111 | * @param string $name Field name |
||
112 | * @param array $value |
||
113 | * @return bool Success |
||
114 | */ |
||
115 | public static function set_theme_option( $name, $value ) { |
||
118 | |||
119 | /** |
||
120 | * Get term meta field for a term. |
||
121 | * |
||
122 | * @param int $id Term ID. |
||
123 | * @param string $name Custom field name. |
||
124 | * @return mixed Meta value. |
||
125 | */ |
||
126 | public static function get_term_meta( $id, $name ) { |
||
129 | |||
130 | /** |
||
131 | * Set term meta field for a term. |
||
132 | * |
||
133 | * @param int $id Term ID |
||
134 | * @param string $name Field name |
||
135 | * @param array $value |
||
136 | * @return bool Success |
||
137 | */ |
||
138 | public static function set_term_meta( $id, $name, $value ) { |
||
141 | |||
142 | /** |
||
143 | * Get user meta field for a user. |
||
144 | * |
||
145 | * @param int $id User ID. |
||
146 | * @param string $name Custom field name. |
||
147 | * @return mixed Meta value. |
||
148 | */ |
||
149 | public static function get_user_meta( $id, $name ) { |
||
152 | |||
153 | /** |
||
154 | * Set user meta field for a user. |
||
155 | * |
||
156 | * @param int $id User ID |
||
157 | * @param string $name Field name |
||
158 | * @param array $value |
||
159 | * @return bool Success |
||
160 | */ |
||
161 | public static function set_user_meta( $id, $name, $value ) { |
||
164 | |||
165 | /** |
||
166 | * Get comment meta field for a comment. |
||
167 | * |
||
168 | * @param int $id Comment ID. |
||
169 | * @param string $name Custom field name. |
||
170 | * @return mixed Meta value. |
||
171 | */ |
||
172 | public static function get_comment_meta( $id, $name ) { |
||
175 | |||
176 | /** |
||
177 | * Set comment meta field for a comment. |
||
178 | * |
||
179 | * @param int $id Comment ID |
||
180 | * @param string $name Field name |
||
181 | * @param array $value |
||
182 | * @return bool Success |
||
183 | */ |
||
184 | public static function set_comment_meta( $id, $name, $value ) { |
||
187 | |||
188 | /** |
||
189 | * Recursive sorting function by array key. |
||
190 | * |
||
191 | * @param array &$array The input array. |
||
192 | * @param int $sort_flags Flags for controlling sorting behavior. |
||
193 | * @return array Sorted array. |
||
194 | */ |
||
195 | public static function ksort_recursive( &$array, $sort_flags = SORT_REGULAR ) { |
||
205 | |||
206 | /** |
||
207 | * Get the relation type from an array similar to how meta_query works in WP_Query |
||
208 | * |
||
209 | * @param array $array |
||
210 | * @param array<string> $allowed_relations |
||
211 | * @param string $relation_key |
||
212 | * @return string |
||
213 | */ |
||
214 | public static function get_relation_type_from_array( $array, $allowed_relations = array( 'AND', 'OR' ), $relation_key = 'relation' ) { |
||
230 | |||
231 | /** |
||
232 | * Normalize a label by updating case, stripping common prefixes etc. |
||
233 | * |
||
234 | * @param string $label |
||
235 | * @return string |
||
236 | */ |
||
237 | View Code Duplication | public static function normalize_label( $label ) { |
|
249 | |||
250 | /** |
||
251 | * Normalize a type string representing an object type |
||
252 | * |
||
253 | * @param string $type |
||
254 | * @return string |
||
255 | */ |
||
256 | View Code Duplication | public static function normalize_type( $type ) { |
|
263 | |||
264 | /** |
||
265 | * Convert a string representing an object type to a fully qualified class name |
||
266 | * |
||
267 | * @param string $type |
||
268 | * @param string $namespace |
||
269 | * @param string $class_suffix |
||
270 | * @return string |
||
271 | */ |
||
272 | public static function type_to_class( $type, $namespace = '', $class_suffix = '' ) { |
||
285 | |||
286 | /** |
||
287 | * Convert a string representing an object type to a fully qualified class name |
||
288 | * |
||
289 | * @param string $class |
||
290 | * @param string $class_suffix |
||
291 | * @return string |
||
292 | */ |
||
293 | public static function class_to_type( $class, $class_suffix = '' ) { |
||
305 | |||
306 | /** |
||
307 | * Get an array of sanitized html classes |
||
308 | * |
||
309 | * @param string|array<string> $classes |
||
310 | * @return array<string> |
||
311 | */ |
||
312 | public static function sanitize_classes( $classes ) { |
||
319 | } |
||
320 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.