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 ) { |
||
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 | * Get an array of sanitized html classes |
||
239 | * |
||
240 | * @param string|array<strnig> $classes |
||
241 | * @return array<string> |
||
242 | */ |
||
243 | public static function sanitize_classes( $classes ) { |
||
250 | } |
||
251 |