Complex classes like AbstractElement 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 AbstractElement, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | abstract class AbstractElement implements FormElementInterface, Iterator |
||
| 28 | { |
||
| 29 | /** @var string */ |
||
| 30 | protected $type = ''; |
||
| 31 | /** @var int */ |
||
| 32 | protected static $tabIndex = 1; |
||
| 33 | |||
| 34 | /** @var string */ |
||
| 35 | protected $name; |
||
| 36 | /** @var string */ |
||
| 37 | protected $label; |
||
| 38 | /** @var mixed */ |
||
| 39 | protected $value; |
||
| 40 | /** @var array */ |
||
| 41 | protected $attributes = []; |
||
| 42 | /** @var array<ValidatorInterface> */ |
||
| 43 | protected $validators = []; |
||
| 44 | /** @var array */ |
||
| 45 | protected $errors = []; |
||
| 46 | /** @var FormElementInterface */ |
||
| 47 | protected $parentNode; |
||
| 48 | /** @var array */ |
||
| 49 | protected $mandatoryParentTypes = []; |
||
| 50 | |||
| 51 | // The implementation of the Iterator interface. |
||
| 52 | use IteratorTrait; |
||
| 53 | // CamelCase to under_score converter |
||
| 54 | use CamelCaseToUnderScoreTrait; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * AbstractFormElement constructor. |
||
| 58 | * |
||
| 59 | * @param string $name |
||
| 60 | * @param string $label |
||
| 61 | * @param mixed $value |
||
| 62 | */ |
||
| 63 | 26 | public function __construct($name = '', $label = '', $value = null) |
|
| 69 | |||
| 70 | /** |
||
| 71 | * Resets the object when cloning. |
||
| 72 | */ |
||
| 73 | 4 | public function __clone() |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Returns the element type. |
||
| 87 | * |
||
| 88 | * @throws Exception |
||
| 89 | * @return string |
||
| 90 | */ |
||
| 91 | 2 | final public function getType() |
|
| 104 | |||
| 105 | /** |
||
| 106 | * Sets element name. The implementation should decide if it is allowed after init. |
||
| 107 | * |
||
| 108 | * @param string $name |
||
| 109 | * @return AbstractElement |
||
| 110 | */ |
||
| 111 | 13 | public function setName($name) |
|
| 117 | |||
| 118 | /** |
||
| 119 | * Returns the element name. If parameter is TRUE, then the method should include all the parents' names as well. |
||
| 120 | * |
||
| 121 | * @param boolean $getFulNodeName |
||
| 122 | * @return string |
||
| 123 | */ |
||
| 124 | 14 | public function getName($getFulNodeName = true) |
|
| 136 | |||
| 137 | /** |
||
| 138 | * Sets element label. |
||
| 139 | * |
||
| 140 | * @param string $label |
||
| 141 | * @return AbstractElement |
||
| 142 | */ |
||
| 143 | 2 | public function setLabel($label) |
|
| 149 | |||
| 150 | /** |
||
| 151 | * Returns the element label. |
||
| 152 | * |
||
| 153 | * @return string |
||
| 154 | */ |
||
| 155 | 2 | public function getLabel() |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Sets element value. |
||
| 162 | * |
||
| 163 | * @param mixed $value |
||
| 164 | * @return AbstractElement |
||
| 165 | */ |
||
| 166 | 9 | public function setValue($value) |
|
| 172 | |||
| 173 | /** |
||
| 174 | * Returns element value. |
||
| 175 | * |
||
| 176 | * @return mixed |
||
| 177 | */ |
||
| 178 | 2 | public function getValue() |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Gets element Id. |
||
| 185 | * |
||
| 186 | * @return string |
||
| 187 | */ |
||
| 188 | 1 | public function getId() |
|
| 203 | |||
| 204 | /** |
||
| 205 | * Sets multiple attributes. |
||
| 206 | * |
||
| 207 | * @param array $attributes |
||
| 208 | * @return AbstractElement |
||
| 209 | */ |
||
| 210 | 13 | public function setAttributes(array $attributes) |
|
| 216 | |||
| 217 | /** |
||
| 218 | * Adds multiple attributes. |
||
| 219 | * |
||
| 220 | * @param array $attributes |
||
| 221 | * @return AbstractElement |
||
| 222 | */ |
||
| 223 | 13 | public function addAttributes(array $attributes) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Sets element attribute. |
||
| 234 | * |
||
| 235 | * @param string $key |
||
| 236 | * @param mixed $value |
||
| 237 | * @throws InvalidArgumentException |
||
| 238 | * @return AbstractElement |
||
| 239 | */ |
||
| 240 | 13 | protected function setAttribute($key, $value) |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Gets all the attributes. |
||
| 260 | * |
||
| 261 | * @return array |
||
| 262 | */ |
||
| 263 | 6 | public function getAttributes() |
|
| 267 | |||
| 268 | /** |
||
| 269 | * Gets element attribute. |
||
| 270 | * |
||
| 271 | * @param string $name |
||
| 272 | * @return mixed |
||
| 273 | */ |
||
| 274 | 3 | public function getAttribute($name) |
|
| 282 | |||
| 283 | /** |
||
| 284 | * Resets the tabulator index internal counter. |
||
| 285 | * |
||
| 286 | * @return AbstractElement |
||
| 287 | */ |
||
| 288 | 5 | public function resetTabIndex() |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Sets and increments the tabulator index globally. This method should be used only on visible elements. |
||
| 297 | * |
||
| 298 | * @return AbstractElement |
||
| 299 | */ |
||
| 300 | 25 | public function setTabIndex() |
|
| 306 | |||
| 307 | /** |
||
| 308 | * Sets the element errors. Usually the validator should set it, but it is allowed to set from outside too. |
||
| 309 | * |
||
| 310 | * @param array $errors |
||
| 311 | * @return AbstractElement |
||
| 312 | */ |
||
| 313 | 1 | public function setErrors(array $errors) |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Checks if there are error messages set. |
||
| 322 | * |
||
| 323 | * @return boolean |
||
| 324 | */ |
||
| 325 | 1 | public function hasErrors() |
|
| 329 | |||
| 330 | /** |
||
| 331 | * Gets validation errors. |
||
| 332 | * |
||
| 333 | * @return array |
||
| 334 | */ |
||
| 335 | 2 | public function getErrors() |
|
| 339 | |||
| 340 | /** |
||
| 341 | * Sets the element validators. |
||
| 342 | * |
||
| 343 | * @param array<ValidatorInterface> $validators |
||
| 344 | * @return FormElementInterface |
||
| 345 | */ |
||
| 346 | 1 | public function setValidators(array $validators) |
|
| 356 | |||
| 357 | /** |
||
| 358 | * Adds validator to the form. |
||
| 359 | * |
||
| 360 | * @param ValidatorInterface $validator |
||
| 361 | * @return AbstractElement |
||
| 362 | */ |
||
| 363 | 1 | protected function addValidator(ValidatorInterface $validator) |
|
| 369 | |||
| 370 | /** |
||
| 371 | * Gets the element validators. |
||
| 372 | * |
||
| 373 | * @return array<ValidatorInterface> |
||
| 374 | */ |
||
| 375 | 1 | public function getValidators() |
|
| 379 | |||
| 380 | /** |
||
| 381 | * Validates element value. |
||
| 382 | * |
||
| 383 | * @param bool $reValidate |
||
| 384 | * @return bool |
||
| 385 | */ |
||
| 386 | 3 | public function isValid($reValidate = false) |
|
| 401 | |||
| 402 | /** |
||
| 403 | * Sets the parent element. |
||
| 404 | * |
||
| 405 | * @param FormElementInterface $formElement |
||
| 406 | * @throws RuntimeException |
||
| 407 | * @return AbstractElement |
||
| 408 | */ |
||
| 409 | 10 | public function setParentNode(FormElementInterface $formElement) |
|
| 425 | |||
| 426 | /** |
||
| 427 | * Gets the parent element. |
||
| 428 | * |
||
| 429 | * @return FormElementInterface |
||
| 430 | */ |
||
| 431 | 1 | public function getParentNode() |
|
| 435 | } |
||
| 436 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.