Complex classes like FormElement 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 FormElement, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | final class FormElement implements Iterator |
||
| 23 | { |
||
| 24 | /** HTML5 form elements */ |
||
| 25 | const TAG_FORM = 'form'; |
||
| 26 | const TAG_INPUT_CHECKBOX = 'checkbox'; |
||
| 27 | const TAG_INPUT_COLOR = 'color'; |
||
| 28 | const TAG_INPUT_DATA = 'date'; |
||
| 29 | const TAG_INPUT_DATETIME = 'datetime'; |
||
| 30 | const TAG_INPUT_DATETIME_LOCAL = 'datetime-local'; |
||
| 31 | const TAG_INPUT_EMAIL = 'email'; |
||
| 32 | const TAG_INPUT_FILE = 'file'; |
||
| 33 | const TAG_INPUT_HIDDEN = 'hidden'; |
||
| 34 | const TAG_INPUT_IMAGE = 'image'; |
||
| 35 | const TAG_INPUT_MONTH = 'month'; |
||
| 36 | const TAG_INPUT_NUMBER = 'number'; |
||
| 37 | const TAG_INPUT_PASSWORD = 'password'; |
||
| 38 | const TAG_INPUT_RADIO = 'radio'; |
||
| 39 | const TAG_INPUT_RANGE = 'range'; |
||
| 40 | const TAG_INPUT_SEARCH = 'search'; |
||
| 41 | const TAG_INPUT_TEL = 'tel'; |
||
| 42 | const TAG_INPUT_TEXT = 'text'; |
||
| 43 | const TAG_INPUT_TIME = 'time'; |
||
| 44 | const TAG_INPUT_URL = 'url'; |
||
| 45 | const TAG_INPUT_WEEK = 'week'; |
||
| 46 | const TAG_TEXTAREA = 'textarea'; |
||
| 47 | const TAG_FIELDSET = 'fieldset'; |
||
| 48 | const TAG_LEGEND = 'legend'; |
||
| 49 | const TAG_LABEL = 'label'; |
||
| 50 | const TAG_BUTTON_SUBMIT = 'submit'; |
||
| 51 | const TAG_BUTTON_RESET = 'reset'; |
||
| 52 | const TAG_BUTTON = 'button'; |
||
| 53 | const TAG_DATALIST = 'datalist'; |
||
| 54 | const TAG_SELECT = 'select'; |
||
| 55 | const TAG_OPTION_GROUP = 'optgroup'; |
||
| 56 | const TAG_OPTION = 'option'; |
||
| 57 | const TAG_KEYGEN = 'keygen'; |
||
| 58 | const TAG_OUTPUT = 'output'; |
||
| 59 | |||
| 60 | /** @var int */ |
||
| 61 | protected static $tabIndex = 1; |
||
| 62 | /** @var string */ |
||
| 63 | private $tagName; |
||
| 64 | /** @var string */ |
||
| 65 | private $name; |
||
| 66 | /** @var string */ |
||
| 67 | private $uniqueFormNamePostfix = ''; |
||
| 68 | /** @var string */ |
||
| 69 | private $label; |
||
| 70 | /** @var mixed */ |
||
| 71 | private $value; |
||
| 72 | /** @var array */ |
||
| 73 | private $options = []; |
||
| 74 | /** @var array */ |
||
| 75 | private $optionGroups = []; |
||
| 76 | /** @var array */ |
||
| 77 | private $attributes; |
||
| 78 | /** @var FormElement */ |
||
| 79 | private $parentNode; |
||
| 80 | /** @var array<FormElement> */ |
||
| 81 | private $childNodes; |
||
| 82 | /** @var array<FormValidatorInterface> */ |
||
| 83 | private $validators; |
||
| 84 | /** @var array */ |
||
| 85 | private $mandatoryTagParents = [ |
||
| 86 | self::TAG_FORM => [], |
||
| 87 | self::TAG_LEGEND => [ |
||
| 88 | self::TAG_FIELDSET |
||
| 89 | ], |
||
| 90 | self::TAG_OPTION => [ |
||
| 91 | self::TAG_DATALIST, |
||
| 92 | self::TAG_OPTION_GROUP, |
||
| 93 | self::TAG_SELECT |
||
| 94 | ], |
||
| 95 | self::TAG_OPTION_GROUP => [ |
||
| 96 | self::TAG_SELECT |
||
| 97 | ], |
||
| 98 | ]; |
||
| 99 | /** @var array */ |
||
| 100 | private $multiOptionTags = [ |
||
| 101 | self::TAG_SELECT, |
||
| 102 | self::TAG_INPUT_RADIO, |
||
| 103 | self::TAG_INPUT_CHECKBOX, |
||
| 104 | self::TAG_DATALIST |
||
| 105 | ]; |
||
| 106 | /** @var array */ |
||
| 107 | private $tabIndexableTags = [ |
||
| 108 | self::TAG_INPUT_CHECKBOX, |
||
| 109 | self::TAG_INPUT_COLOR, |
||
| 110 | self::TAG_INPUT_DATA, |
||
| 111 | self::TAG_INPUT_DATETIME, |
||
| 112 | self::TAG_INPUT_DATETIME_LOCAL, |
||
| 113 | self::TAG_INPUT_EMAIL, |
||
| 114 | self::TAG_INPUT_FILE, |
||
| 115 | self::TAG_INPUT_IMAGE, |
||
| 116 | self::TAG_INPUT_MONTH, |
||
| 117 | self::TAG_INPUT_NUMBER, |
||
| 118 | self::TAG_INPUT_PASSWORD, |
||
| 119 | self::TAG_INPUT_RADIO, |
||
| 120 | self::TAG_INPUT_RANGE, |
||
| 121 | self::TAG_INPUT_SEARCH, |
||
| 122 | self::TAG_INPUT_TEL, |
||
| 123 | self::TAG_INPUT_TEXT, |
||
| 124 | self::TAG_INPUT_TIME, |
||
| 125 | self::TAG_INPUT_URL, |
||
| 126 | self::TAG_INPUT_WEEK, |
||
| 127 | self::TAG_TEXTAREA, |
||
| 128 | self::TAG_BUTTON_SUBMIT, |
||
| 129 | self::TAG_BUTTON_RESET, |
||
| 130 | self::TAG_BUTTON, |
||
| 131 | self::TAG_DATALIST, |
||
| 132 | self::TAG_SELECT, |
||
| 133 | self::TAG_KEYGEN, |
||
| 134 | ]; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * FormElement constructor. |
||
| 138 | * |
||
| 139 | * @param string $tagName |
||
| 140 | * @param string $name |
||
| 141 | * @param string $label |
||
| 142 | */ |
||
| 143 | public function __construct($tagName, $name, $label = '') |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Set unique identifier for the form. |
||
| 156 | * |
||
| 157 | * @param string $uniqueFormNamePostfix |
||
| 158 | * @return FormElement |
||
| 159 | */ |
||
| 160 | public function setUniqueFormNamePostfix($uniqueFormNamePostfix) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Returns the element tag name. |
||
| 173 | * |
||
| 174 | * @return string |
||
| 175 | */ |
||
| 176 | public function getTagName() |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Sets parent element name |
||
| 183 | * |
||
| 184 | * @param FormElement $formElement |
||
| 185 | * @throws RuntimeException |
||
| 186 | * @return FormElement |
||
| 187 | */ |
||
| 188 | public function setParentNode(FormElement $formElement) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Returns the element name. |
||
| 211 | * |
||
| 212 | * @return string |
||
| 213 | */ |
||
| 214 | public function getName() |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Gets element Id. |
||
| 236 | * |
||
| 237 | * @return string |
||
| 238 | */ |
||
| 239 | public function getId() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Returns the element label. |
||
| 246 | * |
||
| 247 | * @return string |
||
| 248 | */ |
||
| 249 | public function getLabel() |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Sets element value. |
||
| 256 | * |
||
| 257 | * @param mixed $value |
||
| 258 | * @return FormElement |
||
| 259 | */ |
||
| 260 | public function setValue($value) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Returns element value. |
||
| 269 | * |
||
| 270 | * @return mixed |
||
| 271 | */ |
||
| 272 | public function getValue() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Set label-value options for the element. |
||
| 279 | * |
||
| 280 | * @param array $options |
||
| 281 | * @throws RuntimeException |
||
| 282 | * @return FormElement |
||
| 283 | */ |
||
| 284 | public function setOptions(array $options) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Sets label-value option for the element. |
||
| 297 | * |
||
| 298 | * @param string $label |
||
| 299 | * @param string $value |
||
| 300 | * @param boolean $checked |
||
| 301 | * @param string $group |
||
| 302 | * @return FormElement |
||
| 303 | */ |
||
| 304 | public function setOption($label, $value, $checked = false, $group = 'Default') |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Checks if the element has value options. |
||
| 334 | * |
||
| 335 | * @return bool |
||
| 336 | */ |
||
| 337 | public function hasOptions() |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Checks if the Select box has groupped options. |
||
| 344 | * |
||
| 345 | * @return bool |
||
| 346 | */ |
||
| 347 | public function isGrouppedSelect() |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Gets element value options. |
||
| 354 | * |
||
| 355 | * @return array |
||
| 356 | */ |
||
| 357 | public function getOptions() |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Set child node for the element. |
||
| 364 | * |
||
| 365 | * @param FormElement $childNode |
||
| 366 | * @return FormElement |
||
| 367 | */ |
||
| 368 | public function addChildNode(FormElement $childNode) |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Gets the child nodes of the element. |
||
| 379 | * |
||
| 380 | * @return array<FormElement> |
||
| 381 | */ |
||
| 382 | public function getChildNodes() |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Sets element attribute. |
||
| 389 | * |
||
| 390 | * @param string $key |
||
| 391 | * @param string $value |
||
| 392 | * @throws InvalidArgumentException |
||
| 393 | * @return FormElement |
||
| 394 | */ |
||
| 395 | public function setAttribute($key, $value) |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Sets multiple attributes. |
||
| 416 | * |
||
| 417 | * @param array $attributes |
||
| 418 | * @return FormElement |
||
| 419 | */ |
||
| 420 | public function setAttributes(array $attributes) |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Gets element attribute. |
||
| 431 | * |
||
| 432 | * @param string $name |
||
| 433 | * @return mixed |
||
| 434 | */ |
||
| 435 | public function getAttribute($name) |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Gets all the attributes. |
||
| 446 | * |
||
| 447 | * @return array |
||
| 448 | */ |
||
| 449 | public function getAttributes() |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Adds validator to the form. |
||
| 456 | * |
||
| 457 | * @param FormValidatorInterface $validator |
||
| 458 | * @return FormElement |
||
| 459 | */ |
||
| 460 | public function addValidator(FormValidatorInterface $validator) |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Validates element value. |
||
| 469 | * |
||
| 470 | * @return bool |
||
| 471 | */ |
||
| 472 | public function isValid() |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Return the current element. |
||
| 485 | * |
||
| 486 | * @return FormElement |
||
| 487 | */ |
||
| 488 | final public function current() |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Moves the pointer forward to next element. |
||
| 495 | * |
||
| 496 | * @return void |
||
| 497 | */ |
||
| 498 | final public function next() |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Returns the key of the current element. |
||
| 505 | * |
||
| 506 | * @return mixed |
||
| 507 | */ |
||
| 508 | final public function key() |
||
| 512 | |||
| 513 | /** |
||
| 514 | * Checks if current position is valid. |
||
| 515 | * |
||
| 516 | * @return boolean |
||
| 517 | */ |
||
| 518 | final public function valid() |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Rewinds the Iterator to the first element. |
||
| 527 | * |
||
| 528 | * @return void |
||
| 529 | */ |
||
| 530 | final public function rewind() |
||
| 534 | } |
||
| 535 |