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 |
||
| 21 | abstract class FieldAbstract implements FieldInterface |
||
| 22 | {
|
||
| 23 | |||
| 24 | /** @var FormInterface[] $dynamicFormCollection */ |
||
| 25 | private $dynamicFormCollection; |
||
| 26 | |||
| 27 | /** @var FilterCollection $filterCollection */ |
||
| 28 | private $filterCollection; |
||
| 29 | |||
| 30 | /** @var ValidatorCollection $validatorCollection */ |
||
| 31 | private $validatorCollection; |
||
| 32 | |||
| 33 | /** @var FieldRendererInterface $renderer */ |
||
| 34 | private $renderer; |
||
| 35 | |||
| 36 | /** @var array $errorMessages */ |
||
| 37 | private $errorMessages; |
||
| 38 | |||
| 39 | /** @var string $customErrorMessage */ |
||
| 40 | private $customErrorMessage; |
||
| 41 | |||
| 42 | /** @var string $label */ |
||
| 43 | private $label; |
||
| 44 | |||
| 45 | /** @var bool $required */ |
||
| 46 | private $required; |
||
| 47 | |||
| 48 | use HasAttributesTrait; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @return string |
||
| 52 | */ |
||
| 53 | abstract public function getTag(); |
||
| 54 | |||
| 55 | abstract public function init(); |
||
| 56 | |||
| 57 | 47 | public function __construct($name, $value = null) |
|
| 68 | |||
| 69 | /** |
||
| 70 | * @return string |
||
| 71 | */ |
||
| 72 | 31 | public function getName() |
|
| 76 | |||
| 77 | /** |
||
| 78 | * @param string $name |
||
| 79 | * @return FieldAbstract |
||
| 80 | */ |
||
| 81 | 47 | public function setName($name) |
|
| 86 | |||
| 87 | /** |
||
| 88 | * @return string |
||
| 89 | */ |
||
| 90 | 28 | public function getId() |
|
| 94 | |||
| 95 | /** |
||
| 96 | * @param string $id |
||
| 97 | * @return FieldAbstract |
||
| 98 | */ |
||
| 99 | 8 | public function setId($id) |
|
| 104 | |||
| 105 | /** |
||
| 106 | * @return string |
||
| 107 | */ |
||
| 108 | 1 | public function getClass() |
|
| 112 | |||
| 113 | /** |
||
| 114 | * @param string $class |
||
| 115 | * @return FieldAbstract |
||
| 116 | */ |
||
| 117 | 8 | public function setClass($class) |
|
| 122 | |||
| 123 | /** |
||
| 124 | * @return mixed |
||
| 125 | */ |
||
| 126 | 34 | public function getValue() |
|
| 130 | |||
| 131 | /** |
||
| 132 | * @param mixed $value |
||
| 133 | * @return FieldAbstract |
||
| 134 | */ |
||
| 135 | 28 | public function setValue($value) |
|
| 141 | |||
| 142 | /** |
||
| 143 | * @param ValidatorInterface $validator |
||
| 144 | * @return $this |
||
| 145 | */ |
||
| 146 | 19 | public function addValidator(ValidatorInterface $validator) |
|
| 151 | |||
| 152 | /** |
||
| 153 | * @return ValidatorCollection |
||
| 154 | */ |
||
| 155 | 2 | public function getValidators() |
|
| 159 | |||
| 160 | /** |
||
| 161 | * @param FilterInterface $filter |
||
| 162 | * @return $this |
||
| 163 | */ |
||
| 164 | 27 | public function addFilter(FilterInterface $filter) |
|
| 169 | |||
| 170 | /** |
||
| 171 | * @return FilterCollection |
||
| 172 | */ |
||
| 173 | 1 | public function getFilters() |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Runs the checkForErrors method for each field, which adds to errorMessages if invalid |
||
| 180 | * |
||
| 181 | * @return bool |
||
| 182 | * @throws Exception If validation of $value is impossible |
||
| 183 | */ |
||
| 184 | 29 | public function isValid() |
|
| 195 | |||
| 196 | /** |
||
| 197 | * @param ValidatorInterface $validator |
||
| 198 | */ |
||
| 199 | 16 | private function checkForErrors(ValidatorInterface $validator) |
|
| 207 | |||
| 208 | 28 | private function filterValue() |
|
| 219 | |||
| 220 | /** |
||
| 221 | * @return array |
||
| 222 | */ |
||
| 223 | 9 | public function getMessages() |
|
| 227 | |||
| 228 | /** |
||
| 229 | * @return string |
||
| 230 | */ |
||
| 231 | 27 | public function getLabel() |
|
| 235 | |||
| 236 | /** |
||
| 237 | * @param string $label |
||
| 238 | * @return $this |
||
| 239 | */ |
||
| 240 | 17 | public function setLabel($label) |
|
| 245 | |||
| 246 | /** |
||
| 247 | * @param string $message |
||
| 248 | * @return $this |
||
| 249 | */ |
||
| 250 | 5 | public function setCustomErrorMessage($message) |
|
| 255 | |||
| 256 | /** |
||
| 257 | * @return bool |
||
| 258 | */ |
||
| 259 | 4 | public function hasCustomErrorMessage() |
|
| 263 | |||
| 264 | /** |
||
| 265 | * @return string |
||
| 266 | */ |
||
| 267 | 2 | public function getCustomErrorMessage() |
|
| 271 | |||
| 272 | /** |
||
| 273 | * @return FieldRendererInterface |
||
| 274 | */ |
||
| 275 | 28 | public function getRenderer() |
|
| 279 | |||
| 280 | /** |
||
| 281 | * @param FieldRendererInterface $renderer |
||
| 282 | * @return $this |
||
| 283 | */ |
||
| 284 | 32 | public function setRenderer(FieldRendererInterface $renderer) |
|
| 289 | |||
| 290 | /** |
||
| 291 | * If a field is required then it must have a value |
||
| 292 | * We add a not empty validator |
||
| 293 | * |
||
| 294 | * @return boolean |
||
| 295 | */ |
||
| 296 | 33 | public function isRequired() |
|
| 300 | |||
| 301 | /** |
||
| 302 | * @param boolean $required |
||
| 303 | * @return FieldAbstract |
||
| 304 | */ |
||
| 305 | 16 | public function setRequired($required) |
|
| 311 | |||
| 312 | 16 | private function addNotEmptyValidator() |
|
| 317 | |||
| 318 | 2 | private function removeNotEmptyValidator() |
|
| 319 | {
|
||
| 320 | 2 | $this->validatorCollection->rewind(); |
|
| 321 | 2 | while ($this->validatorCollection->valid()) {
|
|
| 322 | 2 | $validator = $this->validatorCollection->current(); |
|
| 323 | 2 | $validator instanceof NotEmpty |
|
| 324 | 2 | ? $this->validatorCollection->offsetUnset($this->validatorCollection->key()) |
|
| 325 | 1 | : null; |
|
| 326 | 2 | $this->validatorCollection->next(); |
|
| 327 | } |
||
| 328 | 2 | } |
|
| 329 | |||
| 330 | /** |
||
| 331 | * @param FormInterface $form |
||
| 332 | * @param $triggerValue |
||
| 333 | * @return $this |
||
| 334 | */ |
||
| 335 | 3 | public function addDynamicForm(FormInterface $form, $triggerValue) |
|
| 336 | {
|
||
| 337 | 3 | $this->dynamicFormCollection[$triggerValue] = $form; |
|
| 338 | 3 | return $this; |
|
| 339 | } |
||
| 340 | |||
| 341 | /** |
||
| 342 | * @return bool |
||
| 343 | */ |
||
| 344 | 33 | public function hasDynamicForms() |
|
| 348 | |||
| 349 | /** |
||
| 350 | * @return FormInterface[] |
||
| 351 | * @throws Exception |
||
| 352 | */ |
||
| 353 | 4 | public function getDynamicForms() |
|
| 360 | } |