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  | 
            ||
| 21 | final class FormElement extends AbstractFormElement  | 
            ||
| 22 | { | 
            ||
| 23 | /** @var array */  | 
            ||
| 24 | private $options = [];  | 
            ||
| 25 | /** @var array */  | 
            ||
| 26 | private $optionGroups = [];  | 
            ||
| 27 | |||
| 28 | /**  | 
            ||
| 29 | * FormElement constructor.  | 
            ||
| 30 | *  | 
            ||
| 31 | * @param string $tagName  | 
            ||
| 32 | * @param string $name  | 
            ||
| 33 | * @param string $label  | 
            ||
| 34 | * @param mixed $value  | 
            ||
| 35 | */  | 
            ||
| 36 | public function __construct($tagName, $name = '', $label = '', $value = null)  | 
            ||
| 47 | |||
| 48 | /**  | 
            ||
| 49 | * Sets element name.  | 
            ||
| 50 | *  | 
            ||
| 51 | * @param string $name  | 
            ||
| 52 | * @return FormElement  | 
            ||
| 53 | */  | 
            ||
| 54 | public function setName($name)  | 
            ||
| 60 | |||
| 61 | /**  | 
            ||
| 62 | * Returns the element name.  | 
            ||
| 63 | *  | 
            ||
| 64 | * @return string  | 
            ||
| 65 | */  | 
            ||
| 66 | public function getName()  | 
            ||
| 83 | |||
| 84 | /**  | 
            ||
| 85 | * Sets element label.  | 
            ||
| 86 | *  | 
            ||
| 87 | * @param string $label  | 
            ||
| 88 | * @return FormElement  | 
            ||
| 89 | */  | 
            ||
| 90 | public function setLabel($label)  | 
            ||
| 96 | |||
| 97 | /**  | 
            ||
| 98 | * Sets element value.  | 
            ||
| 99 | *  | 
            ||
| 100 | * @param mixed $value  | 
            ||
| 101 | * @return FormElement  | 
            ||
| 102 | */  | 
            ||
| 103 | public function setValue($value)  | 
            ||
| 109 | |||
| 110 | /**  | 
            ||
| 111 | * Gets element Id.  | 
            ||
| 112 | *  | 
            ||
| 113 | * @return string  | 
            ||
| 114 | */  | 
            ||
| 115 | public function getId()  | 
            ||
| 127 | |||
| 128 | /**  | 
            ||
| 129 | * Set label-value options for the element.  | 
            ||
| 130 | *  | 
            ||
| 131 | * @param array $options  | 
            ||
| 132 | * @throws RuntimeException  | 
            ||
| 133 | * @return FormElement  | 
            ||
| 134 | */  | 
            ||
| 135 | public function setOptions(array $options)  | 
            ||
| 145 | |||
| 146 | /**  | 
            ||
| 147 | * Checks if the element has value options.  | 
            ||
| 148 | *  | 
            ||
| 149 | * @return bool  | 
            ||
| 150 | */  | 
            ||
| 151 | public function hasOptions()  | 
            ||
| 155 | |||
| 156 | /**  | 
            ||
| 157 | * Gets element value options.  | 
            ||
| 158 | *  | 
            ||
| 159 | * @return array  | 
            ||
| 160 | */  | 
            ||
| 161 | public function getOptions()  | 
            ||
| 165 | |||
| 166 | /**  | 
            ||
| 167 | * Sets label-value option for the element.  | 
            ||
| 168 | *  | 
            ||
| 169 | * @param string $label  | 
            ||
| 170 | * @param string $value  | 
            ||
| 171 | * @param boolean $checked  | 
            ||
| 172 | * @param string $group  | 
            ||
| 173 | * @return FormElement  | 
            ||
| 174 | */  | 
            ||
| 175 | public function setOption($label, $value, $checked = false, $group = 'Default')  | 
            ||
| 202 | |||
| 203 | /**  | 
            ||
| 204 | * Checks if the Select box has groupped options.  | 
            ||
| 205 | *  | 
            ||
| 206 | * @return bool  | 
            ||
| 207 | */  | 
            ||
| 208 | public function isGrouppedSelect()  | 
            ||
| 212 | |||
| 213 | /**  | 
            ||
| 214 | * Sets element attribute.  | 
            ||
| 215 | *  | 
            ||
| 216 | * @param string $key  | 
            ||
| 217 | * @param mixed $value  | 
            ||
| 218 | * @throws InvalidArgumentException  | 
            ||
| 219 | * @return FormElement  | 
            ||
| 220 | */  | 
            ||
| 221 | public function setAttribute($key, $value)  | 
            ||
| 231 | |||
| 232 | /**  | 
            ||
| 233 | * Sets multiple attributes.  | 
            ||
| 234 | *  | 
            ||
| 235 | * @param array $attributes  | 
            ||
| 236 | * @return FormElement  | 
            ||
| 237 | */  | 
            ||
| 238 | public function setAttributes(array $attributes)  | 
            ||
| 246 | |||
| 247 | /**  | 
            ||
| 248 | * Gets element attribute.  | 
            ||
| 249 | *  | 
            ||
| 250 | * @param string $name  | 
            ||
| 251 | * @return mixed  | 
            ||
| 252 | */  | 
            ||
| 253 | public function getAttribute($name)  | 
            ||
| 261 | |||
| 262 | /**  | 
            ||
| 263 | * Sets the element errors.  | 
            ||
| 264 | *  | 
            ||
| 265 | * @param array $errors  | 
            ||
| 266 | * @return FormElement  | 
            ||
| 267 | */  | 
            ||
| 268 | public function setErrors(array $errors)  | 
            ||
| 272 | |||
| 273 | /**  | 
            ||
| 274 | * Sets the element validators.  | 
            ||
| 275 | *  | 
            ||
| 276 | * @param array<FormValidatorInterface> $validators  | 
            ||
| 277 | * @return FormElement  | 
            ||
| 278 | */  | 
            ||
| 279 | public function setValidators(array $validators)  | 
            ||
| 287 | |||
| 288 | /**  | 
            ||
| 289 | * Adds validator to the form.  | 
            ||
| 290 | *  | 
            ||
| 291 | * @param FormValidatorInterface $validator  | 
            ||
| 292 | * @return FormElement  | 
            ||
| 293 | */  | 
            ||
| 294 | public function addValidator(FormValidatorInterface $validator)  | 
            ||
| 300 | |||
| 301 | /**  | 
            ||
| 302 | * Validates element value.  | 
            ||
| 303 | *  | 
            ||
| 304 | * @param bool $reValidate  | 
            ||
| 305 | * @return bool  | 
            ||
| 306 | */  | 
            ||
| 307 | public function isValid($reValidate = false)  | 
            ||
| 322 | |||
| 323 | /**  | 
            ||
| 324 | * Sets the parent element.  | 
            ||
| 325 | *  | 
            ||
| 326 | * @param FormElementInterface $formElement  | 
            ||
| 327 | * @throws RuntimeException  | 
            ||
| 328 | * @return FormElementInterface  | 
            ||
| 329 | */  | 
            ||
| 330 | public function setParentNode(FormElementInterface $formElement)  | 
            ||
| 350 | |||
| 351 | /**  | 
            ||
| 352 | * Set the child nodes for the element.  | 
            ||
| 353 | *  | 
            ||
| 354 | * @param array<FormElementInterface> $childNodes  | 
            ||
| 355 | * @return FormElement  | 
            ||
| 356 | */  | 
            ||
| 357 | public function setChildNodes(array $childNodes)  | 
            ||
| 365 | |||
| 366 | /**  | 
            ||
| 367 | * Set child node for the element.  | 
            ||
| 368 | *  | 
            ||
| 369 | * @param FormElementInterface $childNode  | 
            ||
| 370 | * @return FormElement  | 
            ||
| 371 | */  | 
            ||
| 372 | public function addChildNode(FormElementInterface $childNode)  | 
            ||
| 380 | |||
| 381 | /**  | 
            ||
| 382 | * Gets the child nodes of the element.  | 
            ||
| 383 | *  | 
            ||
| 384 | * @return array<FormElement>  | 
            ||
| 385 | */  | 
            ||
| 386 | public function getChildNodes()  | 
            ||
| 390 | }  | 
            ||
| 391 |