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 Yikes_Easy_MC_Checkbox_Integration_Class 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 Yikes_Easy_MC_Checkbox_Integration_Class, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class Yikes_Easy_MC_Checkbox_Integration_Class { |
||
13 | |||
14 | /** |
||
15 | * The integration type. |
||
16 | * |
||
17 | * @var string $type |
||
18 | */ |
||
19 | protected $type = ''; |
||
20 | |||
21 | /** |
||
22 | * Determine whether the current user is subscribed to all of the lists. |
||
23 | * |
||
24 | * @author Jeremy Pry |
||
25 | * |
||
26 | * @param string $type The integration type to check. |
||
27 | * @param string $email The email address to check. |
||
28 | * |
||
29 | * @return bool Whether the current user is subscribed to a list. |
||
30 | */ |
||
31 | public function is_user_already_subscribed( $type, $email = '' ) { |
||
32 | // Make sure we have an email address to use. |
||
33 | if ( empty( $email ) ) { |
||
34 | if ( ! is_user_logged_in() ) { |
||
35 | return false; |
||
36 | } |
||
37 | |||
38 | $current_user = wp_get_current_user(); |
||
39 | $email = $current_user->user_email; |
||
40 | } |
||
41 | |||
42 | // Ensure we have a valid email. |
||
43 | if ( ! is_email( $email ) ) { |
||
44 | return false; |
||
45 | } |
||
46 | |||
47 | // Convert the integration type to a list ID. |
||
48 | $checkbox_options = get_option( 'optin-checkbox-init', '' ); |
||
49 | if ( empty( $checkbox_options ) || ! isset( $checkbox_options[ $type ] ) || ! isset( $checkbox_options[ $type ]['associated-list'] ) ) { |
||
50 | return false; |
||
51 | } |
||
52 | |||
53 | $list_ids = $checkbox_options[ $type ]['associated-list']; |
||
54 | $list_ids = is_array( $list_ids ) ? $list_ids : array( $list_ids ); |
||
55 | |||
56 | // Go through each list... |
||
57 | foreach ( $list_ids as $list_id ) { |
||
58 | if ( ! $this->is_user_subscribed( $email, $list_id, $type ) ) { |
||
59 | return false; |
||
60 | } |
||
61 | } |
||
62 | |||
63 | return true; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Determine whether a given email is subscribed to a given list. |
||
68 | * |
||
69 | * @author Jeremy Pry |
||
70 | * |
||
71 | * @param string $email The email address to check. |
||
72 | * @param string $list_id The list ID to check. |
||
73 | * @param string $type The integration type. |
||
74 | * |
||
75 | * @return bool Whether the email is subscribed to the list. |
||
76 | */ |
||
77 | public function is_user_subscribed( $email, $list_id, $type ) { |
||
107 | |||
108 | /** |
||
109 | * Render the checkbox. |
||
110 | * |
||
111 | * @return string The HTML for the checkbox. |
||
112 | */ |
||
113 | public function yikes_get_checkbox() { |
||
137 | |||
138 | /** |
||
139 | * Hook to submit the data to Mailchimp when a new integration type is submitted. |
||
140 | * |
||
141 | * @since 6.0.0 |
||
142 | * |
||
143 | * @param string $email The email address. |
||
144 | * @param string $type The integration type. |
||
145 | * @param array $merge_vars The array of form data to send. |
||
146 | * @param array $integration_vars An array of additional information that can be used to filter the subscribe request. |
||
147 | */ |
||
148 | public function subscribe_user_integration( $email, $type, $merge_vars, $integration_vars = array() ) { |
||
238 | |||
239 | /** |
||
240 | * Build merge varaibles array |
||
241 | * |
||
242 | * This is currently used in both the BuddyPress and WP Registration integrations. |
||
243 | * |
||
244 | * @param WP_User $user A WP User. |
||
245 | */ |
||
246 | public function user_merge_vars( WP_User $user ) { |
||
270 | |||
271 | /** |
||
272 | * Confirm the checkbox was checked. |
||
273 | * |
||
274 | * @param string $type The integration type. |
||
275 | * |
||
276 | * @return bool True if the checkbox was checked. |
||
277 | */ |
||
278 | public function was_checkbox_checked( $type ) { |
||
281 | } |
||
282 |
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.