Complex classes like GravityView_Field_Unsubscribe 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 GravityView_Field_Unsubscribe, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class GravityView_Field_Unsubscribe extends GravityView_Field { |
||
10 | |||
11 | var $name = 'unsubscribe'; |
||
12 | |||
13 | var $group = 'gravityview'; |
||
14 | |||
15 | var $is_searchable = false; |
||
16 | |||
17 | var $contexts = array( 'single', 'multiple' ); |
||
18 | |||
19 | public function __construct() { |
||
27 | |||
28 | /** |
||
29 | * Hooks called from constructor. |
||
30 | * |
||
31 | * @return void |
||
32 | */ |
||
33 | public function add_hooks() { |
||
40 | |||
41 | /** |
||
42 | * Configure the field options. |
||
43 | * |
||
44 | * Called from the `gravityview_entry_default_fields` filter. |
||
45 | * |
||
46 | * Remove the logged in, new window and show as link options. |
||
47 | * Add the allow unsubscribe for all admins option. |
||
48 | * |
||
49 | * @param array $field_options The options. |
||
50 | * @param string $template_id The template ID. |
||
51 | * @param int|string|float $field_id The field ID. |
||
52 | * @param string $context The configuration context (edit, single, etc.) |
||
53 | * @param string $input_type The input type. |
||
54 | * @param int $form_id The form ID. |
||
55 | * |
||
56 | * @return array The field options. |
||
57 | */ |
||
58 | public function field_options( $field_options, $template_id, $field_id, $context, $input_type, $form_id ) { |
||
76 | |||
77 | /** |
||
78 | * Hide the field from the renderer. Perhaps. |
||
79 | * |
||
80 | * Called from `gravityview/field/is_visible` |
||
81 | * |
||
82 | * Hide the field for non-logged in users for sure. |
||
83 | * |
||
84 | * @param bool $visible Consider visible or not. |
||
85 | * @param \GV\Field $field The field. |
||
86 | * |
||
87 | * @return bool Visible or not. |
||
88 | */ |
||
89 | 45 | public function maybe_not_visible( $visible, $field ) { |
|
95 | /** |
||
96 | * Add the unsubsribe to the configuration fields. |
||
97 | * |
||
98 | * Only if a subscription feed is active for the current form. |
||
99 | * |
||
100 | * Called from `gravityview_entry_default_fields` |
||
101 | * |
||
102 | * @param array $entry_default_fields An array of available for configuration |
||
103 | * @param array|int $form Form ID or array |
||
104 | * @param string $context The configuration context (edit, single, etc.) |
||
105 | * |
||
106 | * @return array The array of available default fields. |
||
107 | */ |
||
108 | public function filter_gravityview_entry_default_field( $entry_default_fields, $form, $context ) { |
||
154 | |||
155 | /** |
||
156 | * Modify the render content. |
||
157 | * |
||
158 | * Called from `gravityview_field_entry_value_unsubscribe` |
||
159 | * |
||
160 | * @param string $output The output. |
||
161 | * @param array $entry The entry. |
||
162 | * @param array $field_settings The field settings. |
||
163 | * @param \GV\Field $field The field. |
||
164 | * |
||
165 | * @return string The content. |
||
166 | */ |
||
167 | 2 | public function modify_entry_value_unsubscribe( $output, $entry, $field_settings, $field ) { |
|
206 | |||
207 | /** |
||
208 | * Try to unsubscribe from the entry. |
||
209 | * |
||
210 | * Called during a POST request. Checks nonce, feeds, entry ID. |
||
211 | * Does not check user permissions. This is left as an exercise for the caller. |
||
212 | * |
||
213 | * Entry View inclusion is checked ad-hoc during the rendering of the field. |
||
214 | * User permissions are also checked ad-hoc during the rendering process. |
||
215 | * |
||
216 | * @param array $entry The entry |
||
217 | * |
||
218 | * @return array $entry The entry |
||
219 | */ |
||
220 | 2 | private function maybe_unsubscribe( $entry ) { |
|
276 | } |
||
277 | |||
279 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.