Conditions | 5 |
Paths | 2 |
Total Lines | 115 |
Code Lines | 82 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
69 | public function generate() |
||
70 | { |
||
71 | $form = new \EE_Form_Section_Proper( |
||
72 | array( |
||
73 | 'name' => 'admin_option_settings', |
||
74 | 'html_id' => 'admin_option_settings', |
||
75 | 'layout_strategy' => new EE_Admin_Two_Column_Layout(), |
||
76 | 'subsections' => array( |
||
77 | 'help_tour_activation_hdr' => new EE_Form_Section_HTML( |
||
78 | EEH_HTML::h2( |
||
79 | esc_html__('Help Tour Global Activation', 'event_espresso') |
||
80 | . ' ' |
||
81 | . EEH_HTML::span( |
||
82 | EEH_Template::get_help_tab_link('help_tour_activation_info'), |
||
83 | 'help_tour_activation' |
||
84 | ), |
||
85 | '', 'ee-admin-settings-hdr' |
||
86 | ) |
||
87 | ), |
||
88 | 'help_tour_activation' => new EE_Yes_No_Input( |
||
89 | array( |
||
90 | 'html_label_text' => esc_html__('Activate Global Help Tours?', 'event_espresso'), |
||
91 | 'html_help_text' => esc_html__( |
||
92 | 'This toggles whether the Event Espresso help tours are active globally or not.', |
||
93 | 'event_espresso' |
||
94 | ), |
||
95 | 'default' => isset($this->registry->CFG->admin->help_tour_activation) |
||
96 | ? filter_var($this->registry->CFG->admin->help_tour_activation, FILTER_VALIDATE_BOOLEAN) |
||
97 | : true, |
||
98 | 'required' => false |
||
99 | ) |
||
100 | ), |
||
101 | 'compatibility_hdr' => new EE_Form_Section_HTML( |
||
102 | EEH_HTML::h2( |
||
103 | esc_html__('Compatibility Settings', 'event_espresso'), |
||
104 | '', 'ee-admin-settings-hdr' |
||
105 | ) |
||
106 | ), |
||
107 | 'encode_session_data' => new EE_Yes_No_Input( |
||
108 | array( |
||
109 | 'html_label_text' => esc_html__('Encode Session Data?', 'event_espresso'), |
||
110 | 'html_help_text' => sprintf( |
||
111 | esc_html__( |
||
112 | 'Some servers and database configurations can cause problems when saving the Event Espresso session data. Setting this option to "Yes" adds an extra layer of encoding to session data to prevent serialization errors, but can be incompatible with some server configurations.%1$sIf you receive "500 internal server" type errors during registration, try turning this option on.%1$sIf you get fatal PHP errors regarding missing base64 functions, then turn this option off.', |
||
113 | 'event_espresso' |
||
114 | ), |
||
115 | '<br>' |
||
116 | ), |
||
117 | 'default' => $this->registry->CFG->admin->encode_session_data(), |
||
118 | 'required' => false |
||
119 | ) |
||
120 | ), |
||
121 | ) |
||
122 | ) |
||
123 | ); |
||
124 | if ( |
||
125 | $this->registry->CAP->current_user_can( |
||
126 | 'manage_options', |
||
127 | 'display_admin_settings_options_promote_and_affiliate' |
||
128 | ) |
||
129 | ) { |
||
130 | $form->add_subsections( |
||
131 | array( |
||
132 | 'promote_ee_hdr' => new EE_Form_Section_HTML( |
||
133 | EEH_HTML::h2( |
||
134 | esc_html__('Promote Event Espresso', 'event_espresso') |
||
135 | . ' ' |
||
136 | . EEH_HTML::span( |
||
137 | EEH_Template::get_help_tab_link('affiliate_info'), |
||
138 | 'affiliate_info' |
||
139 | ), |
||
140 | '', 'ee-admin-settings-hdr' |
||
141 | ) |
||
142 | ), |
||
143 | 'show_reg_footer' => new EE_Yes_No_Input( |
||
144 | array( |
||
145 | 'html_label_text' => esc_html__( |
||
146 | 'Link to Event Espresso in your Registration Page?', |
||
147 | 'event_espresso' |
||
148 | ) |
||
149 | . EEH_Template::get_help_tab_link('email_validation_info'), |
||
150 | 'html_help_text' => esc_html__( |
||
151 | 'adds an unobtrusive link to Event Espresso\'s website in the footer of your registration form. Get an affiliate link (see below) and make money if people click the link and purchase Event Espresso.', |
||
152 | 'event_espresso' |
||
153 | ), |
||
154 | 'default' => isset($this->registry->CFG->admin->show_reg_footer) |
||
155 | ? filter_var($this->registry->CFG->admin->show_reg_footer, FILTER_VALIDATE_BOOLEAN) |
||
156 | : true, |
||
157 | 'required' => false |
||
158 | ) |
||
159 | ), |
||
160 | 'affiliate_id' => new EE_Text_Input( |
||
161 | array( |
||
162 | 'html_label_text' => sprintf( |
||
163 | esc_html__('Event Espresso %sAffiliate%s ID', 'event_espresso'), |
||
164 | '<a href="http://eventespresso.com/affiliates/" target="_blank">', |
||
165 | '</a>' |
||
166 | ), |
||
167 | 'html_help_text' => esc_html__( |
||
168 | 'Earn cash for promoting Event Espresso.', |
||
169 | 'event_espresso' |
||
170 | ), |
||
171 | 'html_class' => 'regular-text', |
||
172 | 'default' => isset($this->registry->CFG->admin->affiliate_id) |
||
173 | ? $this->registry->CFG->admin->get_pretty('affiliate_id') |
||
174 | : '', |
||
175 | 'required' => false |
||
176 | ) |
||
177 | ), |
||
178 | ), |
||
179 | 'help_tour_activation_hdr' |
||
180 | ); |
||
181 | } |
||
182 | return $form; |
||
183 | } |
||
184 | |||
254 | // Location: EventEspresso\admin_pages\general_settings/AdminOptionsSettings.php |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.