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 | public function maybe_not_visible( $visible, $field ) { |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Add the unsubsribe to the configuration fields. |
||
| 95 | * |
||
| 96 | * Only if a subscription feed is active for the current form. |
||
| 97 | * |
||
| 98 | * Called from `gravityview_entry_default_fields` |
||
| 99 | * |
||
| 100 | * @param array $entry_default_fields An array of available for configuration. |
||
| 101 | * @param array $form The form. |
||
| 102 | * @param string $context The configuration context (edit, single, etc.) |
||
| 103 | * |
||
| 104 | * @return array The array of available default fields. |
||
| 105 | */ |
||
| 106 | public function filter_gravityview_entry_default_field( $entry_default_fields, $form, $context ) { |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Modify the render content. |
||
| 151 | * |
||
| 152 | * Called from `gravityview_field_entry_value_unsubscribe` |
||
| 153 | * |
||
| 154 | * @param string $output The output. |
||
| 155 | * @param array $entry The entry. |
||
| 156 | * @param array $field_settings The field settings. |
||
| 157 | * @param \GV\Field $field The field. |
||
| 158 | * |
||
| 159 | * @return string The content. |
||
| 160 | */ |
||
| 161 | public function modify_entry_value_unsubscribe( $output, $entry, $field_settings, $field ) { |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Try to unsubscribe from the entry. |
||
| 203 | * |
||
| 204 | * Called during a POST request. Checks nonce, feeds, entry ID. |
||
| 205 | * Does not check user permissions. This is left as an exercise for the caller. |
||
| 206 | * |
||
| 207 | * Entry View inclusion is checked ad-hoc during the rendering of the field. |
||
| 208 | * User permissions are also checked ad-hoc during the rendering process. |
||
| 209 | * |
||
| 210 | * @param array $entry The entry |
||
| 211 | * |
||
| 212 | * @return array $entry The entry |
||
| 213 | */ |
||
| 214 | private function maybe_unsubscribe( $entry ) { |
||
| 270 | } |
||
| 271 | |||
| 273 |
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
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey 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.