Complex classes like SimpleForm 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 SimpleForm, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class SimpleForm { |
||
| 23 | var $_method; |
||
| 24 | var $_action; |
||
| 25 | var $_encoding; |
||
| 26 | var $_default_target; |
||
| 27 | var $_id; |
||
| 28 | var $_buttons; |
||
| 29 | var $_images; |
||
| 30 | var $_widgets; |
||
| 31 | var $_radios; |
||
| 32 | var $_checkboxes; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Starts with no held controls/widgets. |
||
| 36 | * @param SimpleTag $tag Form tag to read. |
||
| 37 | * @param SimplePage $page Holding page. |
||
| 38 | */ |
||
| 39 | function SimpleForm($tag, &$page) { |
||
| 40 | $this->_method = $tag->getAttribute('method'); |
||
| 41 | $this->_action = $this->_createAction($tag->getAttribute('action'), $page); |
||
| 42 | $this->_encoding = $this->_setEncodingClass($tag); |
||
| 43 | $this->_default_target = false; |
||
| 44 | $this->_id = $tag->getAttribute('id'); |
||
| 45 | $this->_buttons = array(); |
||
| 46 | $this->_images = array(); |
||
| 47 | $this->_widgets = array(); |
||
| 48 | $this->_radios = array(); |
||
| 49 | $this->_checkboxes = array(); |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Creates the request packet to be sent by the form. |
||
| 54 | * @param SimpleTag $tag Form tag to read. |
||
| 55 | * @return string Packet class. |
||
| 56 | * @access private |
||
| 57 | */ |
||
| 58 | function _setEncodingClass($tag) { |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Sets the frame target within a frameset. |
||
| 70 | * @param string $frame Name of frame. |
||
| 71 | * @access public |
||
| 72 | */ |
||
| 73 | function setDefaultTarget($frame) { |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Accessor for method of form submission. |
||
| 79 | * @return string Either get or post. |
||
| 80 | * @access public |
||
| 81 | */ |
||
| 82 | function getMethod() { |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Combined action attribute with current location |
||
| 88 | * to get an absolute form target. |
||
| 89 | * @param string $action Action attribute from form tag. |
||
| 90 | * @param SimpleUrl $base Page location. |
||
| 91 | * @return SimpleUrl Absolute form target. |
||
| 92 | */ |
||
| 93 | function _createAction($action, &$page) { |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Absolute URL of the target. |
||
| 102 | * @return SimpleUrl URL target. |
||
| 103 | * @access public |
||
| 104 | */ |
||
| 105 | function getAction() { |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Creates the encoding for the current values in the |
||
| 115 | * form. |
||
| 116 | * @return SimpleFormEncoding Request to submit. |
||
| 117 | * @access private |
||
| 118 | */ |
||
| 119 | function _encode() { |
||
| 127 | |||
| 128 | /** |
||
| 129 | * ID field of form for unique identification. |
||
| 130 | * @return string Unique tag ID. |
||
| 131 | * @access public |
||
| 132 | */ |
||
| 133 | function getId() { |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Adds a tag contents to the form. |
||
| 139 | * @param SimpleWidget $tag Input tag to add. |
||
| 140 | * @access public |
||
| 141 | */ |
||
| 142 | function addWidget(&$tag) { |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Sets the widget into the form, grouping radio |
||
| 154 | * buttons if any. |
||
| 155 | * @param SimpleWidget $tag Incoming form control. |
||
| 156 | * @access private |
||
| 157 | */ |
||
| 158 | function _setWidget(&$tag) { |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Adds a radio button, building a group if necessary. |
||
| 170 | * @param SimpleRadioButtonTag $tag Incoming form control. |
||
| 171 | * @access private |
||
| 172 | */ |
||
| 173 | function _addRadioButton(&$tag) { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Adds a checkbox, making it a group on a repeated name. |
||
| 183 | * @param SimpleCheckboxTag $tag Incoming form control. |
||
| 184 | * @access private |
||
| 185 | */ |
||
| 186 | function _addCheckbox(&$tag) { |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Extracts current value from form. |
||
| 203 | * @param SimpleSelector $selector Criteria to apply. |
||
| 204 | * @return string/array Value(s) as string or null |
||
| 205 | * if not set. |
||
| 206 | * @access public |
||
| 207 | */ |
||
| 208 | function getValue($selector) { |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Sets a widget value within the form. |
||
| 224 | * @param SimpleSelector $selector Criteria to apply. |
||
| 225 | * @param string $value Value to input into the widget. |
||
| 226 | * @return boolean True if value is legal, false |
||
| 227 | * otherwise. If the field is not |
||
| 228 | * present, nothing will be set. |
||
| 229 | * @access public |
||
| 230 | */ |
||
| 231 | function setField($selector, $value, $position=false) { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Used by the page object to set widgets labels to |
||
| 249 | * external label tags. |
||
| 250 | * @param SimpleSelector $selector Criteria to apply. |
||
| 251 | * @access public |
||
| 252 | */ |
||
| 253 | function attachLabelBySelector($selector, $label) { |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Test to see if a form has a submit button. |
||
| 266 | * @param SimpleSelector $selector Criteria to apply. |
||
| 267 | * @return boolean True if present. |
||
| 268 | * @access public |
||
| 269 | */ |
||
| 270 | function hasSubmit($selector) { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Test to see if a form has an image control. |
||
| 281 | * @param SimpleSelector $selector Criteria to apply. |
||
| 282 | * @return boolean True if present. |
||
| 283 | * @access public |
||
| 284 | */ |
||
| 285 | function hasImage($selector) { |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Gets the submit values for a selected button. |
||
| 296 | * @param SimpleSelector $selector Criteria to apply. |
||
| 297 | * @param hash $additional Additional data for the form. |
||
| 298 | * @return SimpleEncoding Submitted values or false |
||
| 299 | * if there is no such button |
||
| 300 | * in the form. |
||
| 301 | * @access public |
||
| 302 | */ |
||
| 303 | function submitButton($selector, $additional = false) { |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Gets the submit values for an image. |
||
| 320 | * @param SimpleSelector $selector Criteria to apply. |
||
| 321 | * @param integer $x X-coordinate of click. |
||
| 322 | * @param integer $y Y-coordinate of click. |
||
| 323 | * @param hash $additional Additional data for the form. |
||
| 324 | * @return SimpleEncoding Submitted values or false |
||
| 325 | * if there is no such button in the |
||
| 326 | * form. |
||
| 327 | * @access public |
||
| 328 | */ |
||
| 329 | function submitImage($selector, $x, $y, $additional = false) { |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Simply submits the form without the submit button |
||
| 346 | * value. Used when there is only one button or it |
||
| 347 | * is unimportant. |
||
| 348 | * @return hash Submitted values. |
||
| 349 | * @access public |
||
| 350 | */ |
||
| 351 | function submit() { |
||
| 354 | } |
||
| 355 | ?> |
||
| 356 |