Complex classes like EE_Question 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 EE_Question, and based on these observations, apply Extract Interface, too.
1 | <?php if ( !defined( 'EVENT_ESPRESSO_VERSION' ) ) { |
||
29 | class EE_Question extends EE_Soft_Delete_Base_Class implements EEI_Duplicatable { |
||
30 | |||
31 | /** |
||
32 | * |
||
33 | * @param array $props_n_values incoming values |
||
34 | * @param string $timezone incoming timezone (if not set the timezone set for the website will be |
||
35 | * used.) |
||
36 | * @param array $date_formats incoming date_formats in an array where the first value is the |
||
37 | * date_format and the second value is the time format |
||
38 | * @return EE_Question |
||
39 | */ |
||
40 | public static function new_instance( $props_n_values = array(), $timezone = null, $date_formats = array() ) { |
||
44 | |||
45 | |||
46 | |||
47 | /** |
||
48 | * @param array $props_n_values incoming values from the database |
||
49 | * @param string $timezone incoming timezone as set by the model. If not set the timezone for |
||
50 | * the website will be used. |
||
51 | * @return EE_Question |
||
52 | */ |
||
53 | public static function new_instance_from_db( $props_n_values = array(), $timezone = null ) { |
||
56 | |||
57 | |||
58 | |||
59 | /** |
||
60 | * Set Question display text |
||
61 | * |
||
62 | * @access public |
||
63 | * @param string $QST_display_text |
||
64 | */ |
||
65 | public function set_display_text( $QST_display_text = '' ) { |
||
68 | |||
69 | |||
70 | |||
71 | /** |
||
72 | * Set Question admin text |
||
73 | * |
||
74 | * @access public |
||
75 | * @param string $QST_admin_label |
||
76 | */ |
||
77 | public function set_admin_label( $QST_admin_label = '' ) { |
||
80 | |||
81 | |||
82 | |||
83 | /** |
||
84 | * Set system name |
||
85 | * |
||
86 | * @access public |
||
87 | * @param mixed $QST_system |
||
88 | */ |
||
89 | public function set_system_ID( $QST_system = '' ) { |
||
92 | |||
93 | |||
94 | |||
95 | /** |
||
96 | * Set question's type |
||
97 | * |
||
98 | * @access public |
||
99 | * @param string $QST_type |
||
100 | */ |
||
101 | public function set_question_type( $QST_type = '' ) { |
||
104 | |||
105 | |||
106 | |||
107 | /** |
||
108 | * Sets whether this question must be answered when presented in a form |
||
109 | * |
||
110 | * @access public |
||
111 | * @param bool $QST_required |
||
112 | */ |
||
113 | public function set_required( $QST_required = FALSE ) { |
||
116 | |||
117 | |||
118 | |||
119 | /** |
||
120 | * Set Question display text |
||
121 | * |
||
122 | * @access public |
||
123 | * @param string $QST_required_text |
||
124 | */ |
||
125 | public function set_required_text( $QST_required_text = '' ) { |
||
128 | |||
129 | |||
130 | |||
131 | /** |
||
132 | * Sets the order of this question when placed in a sequence of questions |
||
133 | * |
||
134 | * @access public |
||
135 | * @param int $QST_order |
||
136 | */ |
||
137 | public function set_order( $QST_order = 0 ) { |
||
140 | |||
141 | |||
142 | |||
143 | /** |
||
144 | * Sets whether the question is admin-only |
||
145 | * |
||
146 | * @access public |
||
147 | * @param bool $QST_admin_only |
||
148 | */ |
||
149 | public function set_admin_only( $QST_admin_only = FALSE ) { |
||
152 | |||
153 | |||
154 | |||
155 | /** |
||
156 | * Sets the wordpress user ID on the question |
||
157 | * |
||
158 | * @access public |
||
159 | * @param int $QST_wp_user |
||
160 | */ |
||
161 | public function set_wp_user( $QST_wp_user = 1 ) { |
||
164 | |||
165 | |||
166 | |||
167 | /** |
||
168 | * Sets whether the question has been deleted |
||
169 | * (we use this boolean instead of actually |
||
170 | * deleting it because when users delete this question |
||
171 | * they really want to remove the question from future |
||
172 | * forms, BUT keep their old answers which depend |
||
173 | * on this record actually existing. |
||
174 | * |
||
175 | * @access public |
||
176 | * @param bool $QST_deleted |
||
177 | */ |
||
178 | public function set_deleted( $QST_deleted = FALSE ) { |
||
181 | |||
182 | |||
183 | |||
184 | /** |
||
185 | * returns the text for displaying the question to users |
||
186 | * @access public |
||
187 | * @return string |
||
188 | */ |
||
189 | public function display_text() { |
||
192 | |||
193 | |||
194 | |||
195 | /** |
||
196 | * returns the text for the administrative label |
||
197 | * @access public |
||
198 | * @return string |
||
199 | */ |
||
200 | public function admin_label() { |
||
203 | |||
204 | |||
205 | |||
206 | /** |
||
207 | * returns the attendee column name for this question |
||
208 | * @access public |
||
209 | * @return string |
||
210 | */ |
||
211 | public function system_ID() { |
||
214 | |||
215 | |||
216 | |||
217 | /** |
||
218 | * returns either a string of 'text', 'textfield', etc. |
||
219 | * @access public |
||
220 | * @return boolean |
||
221 | */ |
||
222 | public function required() { |
||
225 | |||
226 | |||
227 | |||
228 | /** |
||
229 | * returns the text which should be displayed when a user |
||
230 | * doesn't answer this question in a form |
||
231 | * @access public |
||
232 | * @return string |
||
233 | */ |
||
234 | public function required_text() { |
||
237 | |||
238 | |||
239 | |||
240 | /** |
||
241 | * returns the type of this question |
||
242 | * @access public |
||
243 | * @return string |
||
244 | */ |
||
245 | public function type() { |
||
248 | |||
249 | |||
250 | |||
251 | /** |
||
252 | * returns an integer showing where this question should |
||
253 | * be placed in a sequence of questions |
||
254 | * @access public |
||
255 | * @return int |
||
256 | */ |
||
257 | public function order() { |
||
260 | |||
261 | |||
262 | |||
263 | /** |
||
264 | * returns whether this question should only appears to admins, |
||
265 | * or to everyone |
||
266 | * @access public |
||
267 | * @return boolean |
||
268 | */ |
||
269 | public function admin_only() { |
||
272 | |||
273 | |||
274 | |||
275 | /** |
||
276 | * returns the id the wordpress user who created this question |
||
277 | * @access public |
||
278 | * @return int |
||
279 | */ |
||
280 | public function wp_user() { |
||
283 | |||
284 | |||
285 | |||
286 | /** |
||
287 | * returns whether this question has been marked as 'deleted' |
||
288 | * @access public |
||
289 | * @return boolean |
||
290 | */ |
||
291 | public function deleted() { |
||
294 | |||
295 | |||
296 | |||
297 | /** |
||
298 | * Gets an array of related EE_Answer to this EE_Question |
||
299 | * @return EE_Answer[] |
||
300 | */ |
||
301 | public function answers() { |
||
304 | |||
305 | |||
306 | |||
307 | /** |
||
308 | * Boolean check for if there are answers on this question in th db |
||
309 | * @return boolean true = has answers, false = no answers. |
||
310 | */ |
||
311 | public function has_answers() { |
||
314 | |||
315 | |||
316 | |||
317 | /** |
||
318 | * gets an array of EE_Question_Group which relate to this question |
||
319 | * @return EE_Question_Group[] |
||
320 | */ |
||
321 | public function question_groups() { |
||
324 | |||
325 | |||
326 | |||
327 | /** |
||
328 | * Returns all the options for this question. By default, it returns only the not-yet-deleted ones. |
||
329 | * @param boolean $notDeletedOptionsOnly 1 |
||
330 | * whether to return ALL options, or only the ones which have not yet been deleleted |
||
331 | * @param string|array $selected_value_to_always_include , when retrieving options to an ANSWERED question, |
||
332 | * we want to usually only show non-deleted options AND the value that was selected for the answer, |
||
333 | * whether it was trashed or not. |
||
334 | * @return EE_Question_Option[] |
||
335 | */ |
||
336 | public function options( $notDeletedOptionsOnly = TRUE, $selected_value_to_always_include = NULL ) { |
||
355 | |||
356 | |||
357 | |||
358 | /** |
||
359 | * returns an array of EE_Question_Options which relate to this question |
||
360 | * @return \EE_Question_Option[] |
||
361 | */ |
||
362 | public function temp_options() { |
||
365 | |||
366 | |||
367 | |||
368 | /** |
||
369 | * Adds an option for this question. Note: if the option were previously associated with a different |
||
370 | * Question, that relationship will be overwritten. |
||
371 | * @param EE_Question_Option $option |
||
372 | * @return boolean success |
||
373 | */ |
||
374 | public function add_option( EE_Question_Option $option ) { |
||
377 | |||
378 | |||
379 | |||
380 | /** |
||
381 | * Adds an option directly to this question without saving to the db |
||
382 | * @param EE_Question_Option $option |
||
383 | * @return boolean success |
||
384 | */ |
||
385 | public function add_temp_option( EE_Question_Option $option ) { |
||
389 | |||
390 | |||
391 | |||
392 | /** |
||
393 | * Marks the option as deleted. |
||
394 | * @param EE_Question_Option $option |
||
395 | * @return boolean success |
||
396 | */ |
||
397 | public function remove_option( EE_Question_Option $option ) { |
||
400 | |||
401 | |||
402 | |||
403 | /** |
||
404 | * @return bool |
||
405 | */ |
||
406 | public function is_system_question() { |
||
410 | |||
411 | |||
412 | |||
413 | /** |
||
414 | * The purpose of this method is set the question order this question order to be the max out of all questions |
||
415 | * |
||
416 | * @access public |
||
417 | * @return void |
||
418 | */ |
||
419 | public function set_order_to_latest() { |
||
424 | |||
425 | |||
426 | |||
427 | /** |
||
428 | * Retrieves the list of allowed question types from the model. |
||
429 | * @return string[] |
||
430 | */ |
||
431 | private function _allowed_question_types() { |
||
436 | |||
437 | /** |
||
438 | * Duplicates this question and its question options |
||
439 | * @return \EE_Question |
||
440 | */ |
||
441 | public function duplicate( $options = array() ) { |
||
461 | |||
462 | /** |
||
463 | * Returns the question's maximum allowed response size |
||
464 | * @return int|float |
||
465 | */ |
||
466 | public function max() { |
||
469 | |||
470 | /** |
||
471 | * Sets the question's maximum allowed response size |
||
472 | * @param int|float $new_max |
||
473 | * @return int|float |
||
474 | */ |
||
475 | public function set_max( $new_max ) { |
||
478 | |||
479 | |||
480 | } |
||
481 |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.