Complex classes like Form 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 Form, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class Form extends GroupComponent implements FormData, Synchronizable |
||
14 | { |
||
15 | |||
16 | const TARGET_SELF = "_self"; |
||
17 | const TARGET_BLANK = "_blank"; |
||
18 | |||
19 | private |
||
20 | $action, |
||
21 | $method, |
||
22 | $target, |
||
23 | $validators = array(), |
||
24 | $callback = array(), |
||
25 | $activateCallbacks = array(), |
||
26 | $javascriptCallback = array(), |
||
27 | |||
28 | $forceSubmit = false, |
||
29 | $dataFields, |
||
30 | |||
31 | $isUserSubmitted = false, |
||
32 | $isProcessed = false, |
||
33 | $isCallbacksubmitted = false; |
||
34 | |||
35 | /** |
||
36 | * Instantiates a new Form |
||
37 | * |
||
38 | * @param string $slug |
||
39 | * @param string $action |
||
40 | * @param Method $method |
||
41 | * @param string $target |
||
42 | */ |
||
43 | public function __construct ($slug, $action = '', $method = null, $target = self::TARGET_SELF) |
||
52 | |||
53 | /** |
||
54 | * Resets state and field values. DOES NOT remove validators and callbacks |
||
55 | */ |
||
56 | public function reset () |
||
63 | |||
64 | /** |
||
65 | * Registers the form globally |
||
66 | * @return Form this |
||
67 | */ |
||
68 | private function register () |
||
73 | |||
74 | /** |
||
75 | * Searches form fields |
||
76 | * |
||
77 | * @param string $query ID to search for |
||
78 | * @param bool $includeInactiveFields Whether to include inactive fields in the search |
||
79 | * |
||
80 | * @return null|Field closest match or null if not found |
||
81 | */ |
||
82 | public function f ($query, $includeInactiveFields = false) |
||
97 | |||
98 | /** |
||
99 | * Searches form fields and returns its value |
||
100 | * |
||
101 | * @param string $query id to search for |
||
102 | * @param string $default default value |
||
103 | * |
||
104 | * @return string value of closest match or default value if field not found |
||
105 | */ |
||
106 | public function v ($query, $default = '') |
||
114 | |||
115 | /** |
||
116 | * Returns an array of fields of which data is to be collected |
||
117 | * @return Field[] |
||
118 | */ |
||
119 | public function c () |
||
128 | |||
129 | /** |
||
130 | * Adds a new form-level validator |
||
131 | * |
||
132 | * @param FormValidator $validator validator |
||
133 | * @param boolean $unshift Whether to add the validator to the front of the array |
||
134 | * |
||
135 | * @return static |
||
136 | */ |
||
137 | public function addValidator (FormValidator $validator, $unshift = false) |
||
145 | |||
146 | public function getDataFieldName ($slug) |
||
150 | |||
151 | public function getSubmitConfirmFieldName () |
||
155 | |||
156 | /** |
||
157 | * Finds a field by its localslug value |
||
158 | * |
||
159 | * @param $localSlug |
||
160 | * |
||
161 | * @return Field|null |
||
162 | */ |
||
163 | public function getField ($localSlug) |
||
171 | |||
172 | public function getValue ($key, $default = '') |
||
176 | |||
177 | public function hasValue ($key) |
||
181 | |||
182 | /** |
||
183 | * Attach a function that will be called upon submitting the form. The first argument passed to the function will |
||
184 | * be an instance of FormData. |
||
185 | * |
||
186 | * @param callable $callback |
||
187 | */ |
||
188 | public function attachCallback ($callback) |
||
192 | |||
193 | public function attachActivateCallback ($callback) |
||
197 | |||
198 | public function attachJavascriptCallback ($callback) |
||
202 | |||
203 | public function getDataFields () |
||
207 | |||
208 | public function getAction () |
||
212 | |||
213 | public function setAction ($action) |
||
217 | |||
218 | public function getAttributes () |
||
226 | |||
227 | public function getClasses () |
||
233 | |||
234 | /** |
||
235 | * Gets the script that will instantiate the form |
||
236 | * @return string |
||
237 | */ |
||
238 | public function getScript () |
||
242 | |||
243 | /** |
||
244 | * Gets the script tag that will instantiate the form |
||
245 | * @return string |
||
246 | */ |
||
247 | public function getScriptHTML () |
||
253 | |||
254 | static public function renderFormError ($errorMsg) |
||
258 | |||
259 | /** |
||
260 | * Get all the error messages for form-wide validators that returned invalid |
||
261 | * |
||
262 | * @return string[] |
||
263 | */ |
||
264 | public function getFormErrors () |
||
272 | |||
273 | /** |
||
274 | * Gets the markup that is to be outputted before the actual contents of the form. This method could be used for |
||
275 | * even more manual control over outputting with a custom markup. |
||
276 | * @return string |
||
277 | */ |
||
278 | public function getWrapBefore () |
||
286 | |||
287 | /** |
||
288 | * Gets the markup that is to be outputted after the actual contents of the form. This method could be used for |
||
289 | * even more manual control over outputting with a custom markup. |
||
290 | * |
||
291 | * @param bool $includeScripts Whether to include the scripts |
||
292 | * |
||
293 | * @return string |
||
294 | */ |
||
295 | public function getWrapAfter ($includeScripts = true) |
||
299 | |||
300 | /** |
||
301 | * Wraps the <pre>$content</pre> inside a form tag, declaring error messages, datafields and the script contents. |
||
302 | * This method could be useful for using a form with custom custom markup. |
||
303 | * |
||
304 | * @param string $content The form "content" to be wrapped. Should declare all the required input fields. |
||
305 | * @param bool $includeScripts Whether to include the scripts |
||
306 | * |
||
307 | * @return string The HTML content |
||
308 | */ |
||
309 | public function wrap ($content, $includeScripts = true) |
||
313 | |||
314 | /** |
||
315 | * Renders and returns complete form markup as HTML. Use this to echo the form using default markup to the webpage. |
||
316 | * |
||
317 | * @param bool $showLabel |
||
318 | * |
||
319 | * @return string |
||
320 | */ |
||
321 | public function getHTML ($showLabel = true) |
||
325 | |||
326 | public function getID () |
||
330 | |||
331 | /** |
||
332 | * Retrieves an array of this form's fields |
||
333 | * |
||
334 | * @param boolean $includeInactiveFields whether to include inactive fields as well |
||
335 | * |
||
336 | * @return Field[] |
||
337 | */ |
||
338 | public function getFields ($includeInactiveFields = false) |
||
346 | |||
347 | public function getJsonData () |
||
369 | |||
370 | public function activateHandlers () |
||
376 | |||
377 | /** |
||
378 | * Will attempt to submit the form upon calling the process function, even if the user has not activated it |
||
379 | */ |
||
380 | public function forceSubmit () |
||
384 | |||
385 | /** |
||
386 | * Submits the form internally. You're not usually supposed to call this function directly. |
||
387 | */ |
||
388 | private function submit () |
||
398 | |||
399 | public function isValid () |
||
409 | |||
410 | public function getErrorMessages () |
||
419 | |||
420 | /** |
||
421 | * Check if form was activated, then validates, calls handlers, then submits. Call this function before displaying |
||
422 | * the form. |
||
423 | */ |
||
424 | public function process () |
||
444 | |||
445 | /** |
||
446 | * Checks whether the form has been processed |
||
447 | * @return boolean |
||
448 | */ |
||
449 | public function isProcessed () |
||
453 | |||
454 | /** |
||
455 | * Indicates whether the user has tried to submit the form |
||
456 | * @return boolean |
||
457 | */ |
||
458 | public function isRequested () |
||
462 | |||
463 | /** |
||
464 | * Indicates whether the form was validated correctly |
||
465 | * @return boolean |
||
466 | */ |
||
467 | public function isSubmitted () |
||
471 | |||
472 | /** |
||
473 | * Returns a short human-readable slug/string describing the object |
||
474 | * @return string |
||
475 | */ |
||
476 | function describeObject () |
||
480 | |||
481 | /** |
||
482 | * Gets a complete associated array containing all the data that needs to be stored |
||
483 | * |
||
484 | * @param bool $useName Whether to use the field name (if not, the fields shorter local slug is used) |
||
485 | * @param bool $includeDataFields Whether to include the data fields |
||
486 | * |
||
487 | * @return array |
||
488 | */ |
||
489 | function getValues ($useName = true, $includeDataFields = true) |
||
502 | |||
503 | /** |
||
504 | * Restores the values from an associated array. Only defined properties will be overwritten |
||
505 | * |
||
506 | * @param array $values |
||
507 | */ |
||
508 | function setValues (array $values = array()) |
||
517 | |||
518 | /** |
||
519 | * @return string |
||
520 | */ |
||
521 | protected function getDataFieldsHTML () |
||
528 | |||
529 | /** |
||
530 | * @return string |
||
531 | */ |
||
532 | public function getInnerHTML () |
||
536 | } |
||
537 |