Complex classes like FieldAbstract 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 FieldAbstract, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | abstract class FieldAbstract implements FieldInterface |
||
| 23 | { |
||
| 24 | |||
| 25 | /** @var array $dynamicFormCollection */ |
||
| 26 | private $dynamicFormCollection; |
||
| 27 | |||
| 28 | /** @var FilterCollection $filterCollection */ |
||
| 29 | private $filterCollection; |
||
| 30 | |||
| 31 | /** @var ValidatorCollection $validatorCollection */ |
||
| 32 | private $validatorCollection; |
||
| 33 | |||
| 34 | /** @var FieldRendererInterface $renderer */ |
||
| 35 | private $renderer; |
||
| 36 | |||
| 37 | /** @var array $errorMessages */ |
||
| 38 | private $errorMessages; |
||
| 39 | |||
| 40 | /** @var string $customErrorMessage */ |
||
| 41 | private $customErrorMessage; |
||
| 42 | |||
| 43 | /** @var string $label */ |
||
| 44 | private $label; |
||
| 45 | |||
| 46 | /** @var bool $required */ |
||
| 47 | private $required; |
||
| 48 | |||
| 49 | use HasAttributesTrait; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @return string |
||
| 53 | */ |
||
| 54 | abstract public function getTag(); |
||
| 55 | |||
| 56 | abstract public function init(); |
||
| 57 | |||
| 58 | 42 | public function __construct($name, $value = null) |
|
| 69 | |||
| 70 | /** |
||
| 71 | * @return string |
||
| 72 | */ |
||
| 73 | 28 | public function getName() |
|
| 77 | |||
| 78 | /** |
||
| 79 | * @param string $name |
||
| 80 | * @return FieldAbstract |
||
| 81 | */ |
||
| 82 | 42 | public function setName($name) |
|
| 87 | |||
| 88 | /** |
||
| 89 | * @return string |
||
| 90 | */ |
||
| 91 | 27 | public function getId() |
|
| 95 | |||
| 96 | /** |
||
| 97 | * @param string $id |
||
| 98 | * @return FieldAbstract |
||
| 99 | */ |
||
| 100 | 8 | public function setId($id) |
|
| 105 | |||
| 106 | /** |
||
| 107 | * @return string |
||
| 108 | */ |
||
| 109 | 1 | public function getClass() |
|
| 113 | |||
| 114 | /** |
||
| 115 | * @param string $class |
||
| 116 | * @return FieldAbstract |
||
| 117 | */ |
||
| 118 | 5 | public function setClass($class) |
|
| 123 | |||
| 124 | /** |
||
| 125 | * @return mixed |
||
| 126 | */ |
||
| 127 | 31 | public function getValue() |
|
| 131 | |||
| 132 | /** |
||
| 133 | * @param mixed $value |
||
| 134 | * @return FieldAbstract |
||
| 135 | */ |
||
| 136 | 25 | public function setValue($value) |
|
| 142 | |||
| 143 | /** |
||
| 144 | * @param ValidatorInterface $validator |
||
| 145 | * @return $this |
||
| 146 | */ |
||
| 147 | 15 | public function addValidator(ValidatorInterface $validator) |
|
| 152 | |||
| 153 | /** |
||
| 154 | * @return ValidatorCollection |
||
| 155 | */ |
||
| 156 | 1 | public function getValidators() |
|
| 160 | |||
| 161 | /** |
||
| 162 | * @param FilterInterface $filter |
||
| 163 | * @return $this |
||
| 164 | */ |
||
| 165 | 22 | public function addFilter(FilterInterface $filter) |
|
| 170 | |||
| 171 | /** |
||
| 172 | * @return FilterCollection |
||
| 173 | */ |
||
| 174 | 1 | public function getFilters() |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Runs the checkForErrors method for each field, which adds to errorMessages if invalid |
||
| 181 | * |
||
| 182 | * @return bool |
||
| 183 | * @throws Exception If validation of $value is impossible |
||
| 184 | */ |
||
| 185 | 27 | public function isValid() |
|
| 196 | |||
| 197 | /** |
||
| 198 | * @param ValidatorInterface $validator |
||
| 199 | */ |
||
| 200 | 14 | private function checkForErrors(ValidatorInterface $validator) |
|
| 208 | |||
| 209 | 25 | private function filterValue() |
|
| 220 | |||
| 221 | /** |
||
| 222 | * @return array |
||
| 223 | */ |
||
| 224 | 8 | public function getMessages() |
|
| 228 | |||
| 229 | /** |
||
| 230 | * @return string |
||
| 231 | */ |
||
| 232 | 26 | public function getLabel() |
|
| 236 | |||
| 237 | /** |
||
| 238 | * @param string $label |
||
| 239 | * @return $this |
||
| 240 | */ |
||
| 241 | 14 | public function setLabel($label) |
|
| 246 | |||
| 247 | /** |
||
| 248 | * @param string $message |
||
| 249 | * @return $this |
||
| 250 | */ |
||
| 251 | 2 | public function setCustomErrorMessage($message) |
|
| 256 | |||
| 257 | /** |
||
| 258 | * @return bool |
||
| 259 | */ |
||
| 260 | 4 | public function hasCustomErrorMessage() |
|
| 264 | |||
| 265 | /** |
||
| 266 | * @return string |
||
| 267 | */ |
||
| 268 | 2 | public function getCustomErrorMessage() |
|
| 272 | |||
| 273 | /** |
||
| 274 | * @return FieldRendererInterface |
||
| 275 | */ |
||
| 276 | 27 | public function getRenderer() |
|
| 280 | |||
| 281 | /** |
||
| 282 | * @param FieldRendererInterface $renderer |
||
| 283 | * @return $this |
||
| 284 | */ |
||
| 285 | 29 | public function setRenderer(FieldRendererInterface $renderer) |
|
| 290 | |||
| 291 | /** |
||
| 292 | * If a field is required then it must have a value |
||
| 293 | * We add a not empty validator |
||
| 294 | * |
||
| 295 | * @return boolean |
||
| 296 | */ |
||
| 297 | 31 | public function isRequired() |
|
| 301 | |||
| 302 | /** |
||
| 303 | * @param boolean $required |
||
| 304 | * @return FieldAbstract |
||
| 305 | */ |
||
| 306 | 12 | public function setRequired($required) |
|
| 312 | |||
| 313 | 12 | private function addNotEmptyValidator() |
|
| 318 | |||
| 319 | 1 | private function removeNotEmptyValidator() |
|
| 330 | |||
| 331 | /** |
||
| 332 | * @param FormInterface $form |
||
| 333 | * @param $triggerValue |
||
| 334 | * @return $this |
||
| 335 | */ |
||
| 336 | public function addDynamicForm(FormInterface $form, $triggerValue) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * @return bool |
||
| 353 | */ |
||
| 354 | 20 | public function hasDynamicForms() |
|
| 358 | |||
| 359 | /** |
||
| 360 | * @return FormInterface[] |
||
| 361 | * @throws Exception |
||
| 362 | */ |
||
| 363 | public function getDynamicForms() |
||
| 370 | } |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: