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 acf_compatibility 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 acf_compatibility, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
3 | class acf_compatibility { |
||
4 | |||
5 | /* |
||
6 | * __construct |
||
7 | * |
||
8 | * description |
||
9 | * |
||
10 | * @type function |
||
11 | * @date 30/04/2014 |
||
12 | * @since 5.0.0 |
||
13 | * |
||
14 | * @param $post_id (int) |
||
15 | * @return $post_id (int) |
||
16 | */ |
||
17 | |||
18 | function __construct() { |
||
44 | |||
45 | |||
46 | /* |
||
47 | * settings |
||
48 | * |
||
49 | * description |
||
50 | * |
||
51 | * @type function |
||
52 | * @date 19/05/2014 |
||
53 | * @since 5.0.0 |
||
54 | * |
||
55 | * @param $post_id (int) |
||
56 | * @return $post_id (int) |
||
57 | */ |
||
58 | |||
59 | function settings_acf_lite( $setting ) { |
||
73 | |||
74 | function settings_export_textdomain( $setting ) { |
||
80 | |||
81 | function settings_export_translate( $setting ) { |
||
87 | |||
88 | |||
89 | /* |
||
90 | * get_valid_field |
||
91 | * |
||
92 | * This function will provide compatibility with ACF4 fields |
||
93 | * |
||
94 | * @type function |
||
95 | * @date 23/04/2014 |
||
96 | * @since 5.0.0 |
||
97 | * |
||
98 | * @param $field (array) |
||
99 | * @return $field |
||
100 | */ |
||
101 | |||
102 | function get_valid_field( $field ) { |
||
161 | |||
162 | |||
163 | /* |
||
164 | * get_valid_relationship_field |
||
165 | * |
||
166 | * This function will provide compatibility with ACF4 fields |
||
167 | * |
||
168 | * @type function |
||
169 | * @date 23/04/2014 |
||
170 | * @since 5.0.0 |
||
171 | * |
||
172 | * @param $field (array) |
||
173 | * @return $field |
||
174 | */ |
||
175 | |||
176 | function get_valid_relationship_field( $field ) { |
||
211 | |||
212 | |||
213 | /* |
||
214 | * get_valid_textarea_field |
||
215 | * |
||
216 | * This function will provide compatibility with ACF4 fields |
||
217 | * |
||
218 | * @type function |
||
219 | * @date 23/04/2014 |
||
220 | * @since 5.0.0 |
||
221 | * |
||
222 | * @param $field (array) |
||
223 | * @return $field |
||
224 | */ |
||
225 | |||
226 | function get_valid_textarea_field( $field ) { |
||
241 | |||
242 | |||
243 | /* |
||
244 | * get_valid_image_field |
||
245 | * |
||
246 | * This function will provide compatibility with ACF4 fields |
||
247 | * |
||
248 | * @type function |
||
249 | * @date 23/04/2014 |
||
250 | * @since 5.0.0 |
||
251 | * |
||
252 | * @param $field (array) |
||
253 | * @return $field |
||
254 | */ |
||
255 | |||
256 | function get_valid_image_field( $field ) { |
||
277 | |||
278 | |||
279 | /* |
||
280 | * get_valid_wysiwyg_field |
||
281 | * |
||
282 | * This function will provide compatibility with ACF4 fields |
||
283 | * |
||
284 | * @type function |
||
285 | * @date 23/04/2014 |
||
286 | * @since 5.0.0 |
||
287 | * |
||
288 | * @param $field (array) |
||
289 | * @return $field |
||
290 | */ |
||
291 | |||
292 | function get_valid_wysiwyg_field( $field ) { |
||
309 | |||
310 | |||
311 | /* |
||
312 | * get_valid_date_picker_field |
||
313 | * |
||
314 | * This function will provide compatibility with ACF4 fields |
||
315 | * |
||
316 | * @type function |
||
317 | * @date 23/04/2014 |
||
318 | * @since 5.0.0 |
||
319 | * |
||
320 | * @param $field (array) |
||
321 | * @return $field |
||
322 | */ |
||
323 | |||
324 | function get_valid_date_picker_field( $field ) { |
||
350 | |||
351 | |||
352 | /* |
||
353 | * get_valid_taxonomy_field |
||
354 | * |
||
355 | * This function will provide compatibility with ACF4 fields |
||
356 | * |
||
357 | * @type function |
||
358 | * @date 23/04/2014 |
||
359 | * @since 5.0.0 |
||
360 | * |
||
361 | * @param $field (array) |
||
362 | * @return $field |
||
363 | */ |
||
364 | |||
365 | function get_valid_taxonomy_field( $field ) { |
||
379 | |||
380 | |||
381 | /* |
||
382 | * get_valid_field_group |
||
383 | * |
||
384 | * This function will provide compatibility with ACF4 field groups |
||
385 | * |
||
386 | * @type function |
||
387 | * @date 23/04/2014 |
||
388 | * @since 5.0.0 |
||
389 | * |
||
390 | * @param $field_group (array) |
||
391 | * @return $field_group |
||
392 | */ |
||
393 | |||
394 | function get_valid_field_group( $field_group ) { |
||
574 | |||
575 | } |
||
576 | |||
580 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.