Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like HTML_QuickForm_Renderer_Default 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 HTML_QuickForm_Renderer_Default, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | class HTML_QuickForm_Renderer_Default extends HTML_QuickForm_Renderer |
||
| 38 | { |
||
| 39 | private $form; |
||
| 40 | private $customElementTemplate; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @return mixed |
||
| 44 | */ |
||
| 45 | public function getCustomElementTemplate() |
||
| 49 | |||
| 50 | /** |
||
| 51 | * This template will be taken instead of the default templates by element |
||
| 52 | * @param string $customElementTemplate |
||
| 53 | */ |
||
| 54 | public function setCustomElementTemplate($customElementTemplate) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * The HTML of the form |
||
| 61 | * @var string |
||
| 62 | * @access private |
||
| 63 | */ |
||
| 64 | var $_html; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Header Template string |
||
| 68 | * @var string |
||
| 69 | * @access private |
||
| 70 | */ |
||
| 71 | var $_headerTemplate = |
||
| 72 | "\n\t<tr>\n\t\t<td style=\"white-space: nowrap; background-color: #CCCCCC;\" align=\"left\" valign=\"top\" colspan=\"2\"><b>{header}</b></td>\n\t</tr>"; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Element template string |
||
| 76 | * @var string |
||
| 77 | * @access private |
||
| 78 | */ |
||
| 79 | var $_elementTemplate = |
||
| 80 | "\n\t<tr>\n\t\t<td align=\"right\" valign=\"top\"><!-- BEGIN required --><span style=\"color: #ff0000\">*</span><!-- END required --><b>{label}</b></td>\n\t\t<td valign=\"top\" align=\"left\"><!-- BEGIN error --><span style=\"color: #ff0000\">{error}</span><br /><!-- END error -->\t{element}</td>\n\t</tr>"; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Form template string |
||
| 84 | * @var string |
||
| 85 | * @access private |
||
| 86 | */ |
||
| 87 | var $_formTemplate = |
||
| 88 | "\n<form{attributes}>\n<div>\n{hidden}<table border=\"0\">\n{content}\n</table>\n</div>\n</form>"; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Required Note template string |
||
| 92 | * @var string |
||
| 93 | * @access private |
||
| 94 | */ |
||
| 95 | var $_requiredNoteTemplate = |
||
| 96 | "\n\t<tr>\n\t\t<td></td>\n\t<td align=\"left\" valign=\"top\">{requiredNote}</td>\n\t</tr>"; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Array containing the templates for customised elements |
||
| 100 | * @var array |
||
| 101 | * @access private |
||
| 102 | */ |
||
| 103 | var $_templates = array(); |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Array containing the templates for group wraps. |
||
| 107 | * |
||
| 108 | * These templates are wrapped around group elements and groups' own |
||
| 109 | * templates wrap around them. This is set by setGroupTemplate(). |
||
| 110 | * |
||
| 111 | * @var array |
||
| 112 | * @access private |
||
| 113 | */ |
||
| 114 | var $_groupWraps = array(); |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Array containing the templates for elements within groups |
||
| 118 | * @var array |
||
| 119 | * @access private |
||
| 120 | */ |
||
| 121 | var $_groupTemplates = array(); |
||
| 122 | |||
| 123 | /** |
||
| 124 | * True if we are inside a group |
||
| 125 | * @var bool |
||
| 126 | * @access private |
||
| 127 | */ |
||
| 128 | var $_inGroup = false; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Array with HTML generated for group elements |
||
| 132 | * @var array |
||
| 133 | * @access private |
||
| 134 | */ |
||
| 135 | var $_groupElements = array(); |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Template for an element inside a group |
||
| 139 | * @var string |
||
| 140 | * @access private |
||
| 141 | */ |
||
| 142 | var $_groupElementTemplate = ''; |
||
| 143 | |||
| 144 | /** |
||
| 145 | * HTML that wraps around the group elements |
||
| 146 | * @var string |
||
| 147 | * @access private |
||
| 148 | */ |
||
| 149 | var $_groupWrap = ''; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * HTML for the current group |
||
| 153 | * @var string |
||
| 154 | * @access private |
||
| 155 | */ |
||
| 156 | var $_groupTemplate = ''; |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Collected HTML of the hidden fields |
||
| 160 | * @var string |
||
| 161 | * @access private |
||
| 162 | */ |
||
| 163 | var $_hiddenHtml = ''; |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Constructor |
||
| 167 | * |
||
| 168 | * @access public |
||
| 169 | */ |
||
| 170 | public function __construct() |
||
| 171 | { |
||
| 172 | parent::__construct(); |
||
| 173 | } // end constructor |
||
| 174 | |||
| 175 | /** |
||
| 176 | * returns the HTML generated for the form |
||
| 177 | * |
||
| 178 | * @access public |
||
| 179 | * @return string |
||
| 180 | */ |
||
| 181 | public function toHtml() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Called when visiting a form, before processing any form elements |
||
| 190 | * |
||
| 191 | * @param HTML_QuickForm form object being visited |
||
| 192 | * @access public |
||
| 193 | * @return void |
||
| 194 | */ |
||
| 195 | function startForm(&$form) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * @return FormValidator |
||
| 205 | */ |
||
| 206 | public function getForm() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param mixed $form |
||
| 213 | */ |
||
| 214 | public function setForm($form) |
||
| 218 | |||
| 219 | |||
| 220 | |||
| 221 | /** |
||
| 222 | * Called when visiting a form, after processing all form elements |
||
| 223 | * Adds required note, form attributes, validation javascript and form content. |
||
| 224 | * |
||
| 225 | * @param HTML_QuickForm form object being visited |
||
| 226 | * @access public |
||
| 227 | * @return void |
||
| 228 | */ |
||
| 229 | public function finishForm(&$form) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Called when visiting a header element |
||
| 253 | * |
||
| 254 | * @param HTML_QuickForm_header header element being visited |
||
| 255 | * @access public |
||
| 256 | * @return void |
||
| 257 | */ |
||
| 258 | function renderHeader(&$header) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Helper method for renderElement |
||
| 270 | * |
||
| 271 | * @param HTML_QuickForm_element $element |
||
| 272 | * @param bool Whether an element is required |
||
| 273 | * @param string $required Error message associated with the element |
||
| 274 | * @param string $error Label for ID |
||
| 275 | * @access private |
||
| 276 | * @see renderElement() |
||
| 277 | * @return string Html for element |
||
| 278 | */ |
||
| 279 | private function _prepareTemplate(HTML_QuickForm_element $element, $required, $error) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Renders an element Html |
||
| 348 | * Called when visiting an element |
||
| 349 | * |
||
| 350 | * @param HTML_QuickForm_element form element being visited |
||
| 351 | * @param bool Whether an element is required |
||
| 352 | * @param string An error message associated with an element |
||
| 353 | * @access public |
||
| 354 | * @return void |
||
| 355 | */ |
||
| 356 | public function renderElement(&$element, $required, $error) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Renders an hidden element |
||
| 384 | * Called when visiting a hidden element |
||
| 385 | * |
||
| 386 | * @param HTML_QuickForm_element form element being visited |
||
| 387 | * @access public |
||
| 388 | * @return void |
||
| 389 | */ |
||
| 390 | function renderHidden(&$element) |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Called when visiting a raw HTML/text pseudo-element |
||
| 397 | * |
||
| 398 | * @param HTML_QuickForm_html element being visited |
||
| 399 | * @access public |
||
| 400 | * @return void |
||
| 401 | */ |
||
| 402 | function renderHtml(&$data) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Called when visiting a group, before processing any group elements |
||
| 409 | * |
||
| 410 | * @param HTML_QuickForm_group group being visited |
||
| 411 | * @param bool Whether a group is required |
||
| 412 | * @param string An error message associated with a group |
||
| 413 | * @access public |
||
| 414 | * @return void |
||
| 415 | */ |
||
| 416 | function startGroup(&$group, $required, $error) |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Called when visiting a group, after processing all group elements |
||
| 428 | * |
||
| 429 | * @param HTML_QuickForm_group group being visited |
||
| 430 | * @access public |
||
| 431 | * @return void |
||
| 432 | */ |
||
| 433 | function finishGroup(&$group) |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Sets element template |
||
| 457 | * |
||
| 458 | * @param string The HTML surrounding an element |
||
| 459 | * @param string (optional) Name of the element to apply template for |
||
| 460 | * @access public |
||
| 461 | * @return void |
||
| 462 | */ |
||
| 463 | function setElementTemplate($html, $element = null) |
||
| 471 | |||
| 472 | |||
| 473 | /** |
||
| 474 | * Sets template for a group wrapper |
||
| 475 | * |
||
| 476 | * This template is contained within a group-as-element template |
||
| 477 | * set via setTemplate() and contains group's element templates, set |
||
| 478 | * via setGroupElementTemplate() |
||
| 479 | * |
||
| 480 | * @param string The HTML surrounding group elements |
||
| 481 | * @param string Name of the group to apply template for |
||
| 482 | * @access public |
||
| 483 | * @return void |
||
| 484 | */ |
||
| 485 | function setGroupTemplate($html, $group) |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Sets element template for elements within a group |
||
| 492 | * |
||
| 493 | * @param string The HTML surrounding an element |
||
| 494 | * @param string Name of the group to apply template for |
||
| 495 | * @access public |
||
| 496 | * @return void |
||
| 497 | */ |
||
| 498 | function setGroupElementTemplate($html, $group) |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Sets header template |
||
| 505 | * |
||
| 506 | * @param string The HTML surrounding the header |
||
| 507 | * @access public |
||
| 508 | * @return void |
||
| 509 | */ |
||
| 510 | function setHeaderTemplate($html) |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Sets form template |
||
| 517 | * |
||
| 518 | * @param string The HTML surrounding the form tags |
||
| 519 | * @access public |
||
| 520 | * @return void |
||
| 521 | */ |
||
| 522 | function setFormTemplate($html) { |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Sets the note indicating required fields template |
||
| 528 | * |
||
| 529 | * @param string The HTML surrounding the required note |
||
| 530 | * @access public |
||
| 531 | * @return void |
||
| 532 | */ |
||
| 533 | function setRequiredNoteTemplate($html) |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Clears all the HTML out of the templates that surround notes, elements, etc. |
||
| 540 | * Useful when you want to use addData() to create a completely custom form look |
||
| 541 | * |
||
| 542 | * @access public |
||
| 543 | * @return void |
||
| 544 | */ |
||
| 545 | function clearAllTemplates() |
||
| 552 | } // end class HTML_QuickForm_Renderer_Default |
||
| 553 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: