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 |
||
| 18 | abstract class FieldAbstract implements FieldInterface |
||
| 19 | {
|
||
| 20 | |||
| 21 | /** @var FormInterface[] $dynamicFormCollection */ |
||
| 22 | private $dynamicFormCollection; |
||
| 23 | |||
| 24 | /** @var FilterCollection $filterCollection */ |
||
| 25 | private $filterCollection; |
||
| 26 | |||
| 27 | /** @var ValidatorCollection $validatorCollection */ |
||
| 28 | private $validatorCollection; |
||
| 29 | |||
| 30 | /** @var ValidatorCollection $validatorCollection */ |
||
| 31 | private $transformer; |
||
| 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(): string; |
||
| 54 | |||
| 55 | abstract public function init(); |
||
| 56 | |||
| 57 | 53 | public function __construct($name, $value = null) |
|
| 58 | {
|
||
| 59 | 53 | $this->required = false; |
|
| 60 | 53 | $this->dynamicFormCollection = []; |
|
| 61 | 53 | $this->filterCollection = new FilterCollection(); |
|
| 62 | 53 | $this->validatorCollection = new ValidatorCollection(); |
|
| 63 | 53 | $this->renderer = new TextRender(); |
|
| 64 | 53 | $this->setName($name); |
|
| 65 | 53 | $this->addFilter(new FilterAdapterZf(new ToNull())); |
|
| 66 | 53 | $value === null ? null : $this->setValue($value); |
|
| 67 | 53 | $this->init(); |
|
| 68 | 53 | } |
|
| 69 | |||
| 70 | /** |
||
| 71 | * @return string |
||
| 72 | */ |
||
| 73 | 41 | public function getName(): string |
|
| 74 | {
|
||
| 75 | 41 | return $this->getAttribute('name');
|
|
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @param string $name |
||
| 80 | */ |
||
| 81 | 53 | public function setName(string $name): void |
|
| 85 | |||
| 86 | /** |
||
| 87 | * @return string |
||
| 88 | */ |
||
| 89 | 31 | public function getId(): ?string |
|
| 93 | |||
| 94 | /** |
||
| 95 | * @param string $id |
||
| 96 | */ |
||
| 97 | 8 | public function setId(string $id): void |
|
| 98 | {
|
||
| 99 | 8 | $this->setAttribute('id', $id);
|
|
| 100 | 8 | } |
|
| 101 | |||
| 102 | /** |
||
| 103 | * @return string |
||
| 104 | */ |
||
| 105 | 1 | public function getClass(): string |
|
| 106 | {
|
||
| 107 | 1 | return $this->getAttribute('class') ?: 'form-control';
|
|
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @param string $class |
||
| 112 | * @return FieldAbstract |
||
| 113 | */ |
||
| 114 | 8 | public function setClass(string $class): void |
|
| 115 | {
|
||
| 116 | 8 | $this->setAttribute('class', $class);
|
|
| 117 | 8 | } |
|
| 118 | |||
| 119 | /** |
||
| 120 | * @return mixed |
||
| 121 | */ |
||
| 122 | 40 | public function getValue() |
|
| 123 | {
|
||
| 124 | 40 | return $this->getAttribute('value');
|
|
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @param string $value |
||
| 129 | */ |
||
| 130 | 33 | public function setValue($value): void |
|
| 131 | {
|
||
| 132 | 33 | $this->setAttribute('value', $value);
|
|
| 133 | 33 | $this->filterValue(); |
|
| 134 | 33 | } |
|
| 135 | |||
| 136 | /** |
||
| 137 | * @param ValidatorInterface $validator |
||
| 138 | */ |
||
| 139 | 20 | public function addValidator(ValidatorInterface $validator): void |
|
| 140 | {
|
||
| 141 | 20 | $this->validatorCollection->append($validator); |
|
| 142 | 20 | } |
|
| 143 | |||
| 144 | /** |
||
| 145 | * @return ValidatorCollection |
||
| 146 | */ |
||
| 147 | 2 | public function getValidators(): ValidatorCollection |
|
| 151 | |||
| 152 | /** |
||
| 153 | * @param FilterInterface $filter |
||
| 154 | */ |
||
| 155 | 53 | public function addFilter(FilterInterface $filter): void |
|
| 156 | {
|
||
| 157 | 53 | $this->filterCollection->append($filter); |
|
| 158 | 53 | } |
|
| 159 | |||
| 160 | /** |
||
| 161 | * @param FilterInterface $transformer |
||
| 162 | */ |
||
| 163 | 2 | public function setTransformer(TransformerInterface $transformer): void |
|
| 164 | {
|
||
| 165 | 2 | $this->transformer = $transformer; |
|
|
|
|||
| 166 | 2 | } |
|
| 167 | |||
| 168 | /** |
||
| 169 | * @return TransformerInterface |
||
| 170 | */ |
||
| 171 | 2 | public function getTransformer(): TransformerInterface |
|
| 172 | {
|
||
| 173 | 2 | return $this->transformer; |
|
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @return bool |
||
| 178 | */ |
||
| 179 | 8 | public function hasTransformer(): bool |
|
| 180 | {
|
||
| 181 | 8 | return $this->transformer instanceof TransformerInterface; |
|
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @return FilterCollection |
||
| 186 | */ |
||
| 187 | 1 | public function getFilters(): FilterCollection |
|
| 188 | {
|
||
| 189 | 1 | return $this->filterCollection; |
|
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Runs the checkForErrors method for each field, which adds to errorMessages if invalid |
||
| 194 | * |
||
| 195 | * @return bool |
||
| 196 | * @throws Exception If validation of $value is impossible |
||
| 197 | */ |
||
| 198 | 33 | public function isValid(): bool |
|
| 212 | |||
| 213 | /** |
||
| 214 | * @param ValidatorInterface $validator |
||
| 215 | */ |
||
| 216 | 17 | private function checkForErrors(ValidatorInterface $validator): void |
|
| 217 | {
|
||
| 218 | 17 | $value = $this->getValue(); |
|
| 219 | |||
| 220 | 17 | if ((!$validator->isValid($value)) && $this->isRequired()) {
|
|
| 221 | 13 | $this->errorMessages = array_merge($this->errorMessages, $validator->getMessages()); |
|
| 222 | } |
||
| 223 | 17 | } |
|
| 224 | |||
| 225 | /** |
||
| 226 | * @throws Exception |
||
| 227 | */ |
||
| 228 | 33 | private function filterValue(): void |
|
| 229 | {
|
||
| 230 | 33 | $value = $this->getAttribute('value');
|
|
| 231 | 33 | $this->filterCollection->rewind(); |
|
| 232 | |||
| 233 | 33 | while ($this->filterCollection->valid()) {
|
|
| 234 | 33 | $value = $this->filterCollection->current()->filter($value); |
|
| 235 | 33 | $this->filterCollection->next(); |
|
| 236 | } |
||
| 237 | |||
| 238 | 33 | $this->filterCollection->rewind(); |
|
| 239 | 33 | $this->setAttribute('value', $value);
|
|
| 240 | 33 | } |
|
| 241 | |||
| 242 | /** |
||
| 243 | * @return array |
||
| 244 | */ |
||
| 245 | 10 | public function getMessages(): array |
|
| 246 | {
|
||
| 247 | 10 | return array_values($this->errorMessages); |
|
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @return string |
||
| 252 | */ |
||
| 253 | 30 | public function getLabel(): ?string |
|
| 254 | {
|
||
| 255 | 30 | return $this->label; |
|
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @param string $label |
||
| 260 | */ |
||
| 261 | 19 | public function setLabel(string $label): void |
|
| 262 | {
|
||
| 263 | 19 | $this->label = $label; |
|
| 264 | 19 | } |
|
| 265 | |||
| 266 | /** |
||
| 267 | * @param string $message |
||
| 268 | */ |
||
| 269 | 5 | public function setCustomErrorMessage(string $message): void |
|
| 270 | {
|
||
| 271 | 5 | $this->customErrorMessage = $message; |
|
| 272 | 5 | } |
|
| 273 | |||
| 274 | /** |
||
| 275 | * @return bool |
||
| 276 | */ |
||
| 277 | 4 | public function hasCustomErrorMessage(): bool |
|
| 278 | {
|
||
| 279 | 4 | return $this->customErrorMessage != null; |
|
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * @return string |
||
| 284 | */ |
||
| 285 | 2 | public function getCustomErrorMessage(): string |
|
| 289 | |||
| 290 | /** |
||
| 291 | * @return FieldRendererInterface |
||
| 292 | */ |
||
| 293 | 31 | public function getRenderer(): FieldRendererInterface |
|
| 294 | {
|
||
| 295 | 31 | return $this->renderer; |
|
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param FieldRendererInterface $renderer |
||
| 300 | */ |
||
| 301 | 35 | public function setRenderer(FieldRendererInterface $renderer): void |
|
| 302 | {
|
||
| 303 | 35 | $this->renderer = $renderer; |
|
| 304 | 35 | } |
|
| 305 | |||
| 306 | /** |
||
| 307 | * If a field is required then it must have a value |
||
| 308 | * We add a not empty validator |
||
| 309 | * |
||
| 310 | * @return boolean |
||
| 311 | */ |
||
| 312 | 37 | public function isRequired(): bool |
|
| 313 | {
|
||
| 314 | 37 | return $this->required; |
|
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @param boolean $required |
||
| 319 | */ |
||
| 320 | 17 | public function setRequired(bool $required): void |
|
| 321 | {
|
||
| 322 | 17 | $required ? $this->addNotEmptyValidator() : $this->removeNotEmptyValidator(); |
|
| 323 | 17 | $this->required = $required; |
|
| 324 | 17 | } |
|
| 325 | |||
| 326 | /** |
||
| 327 | * adds not empty validator |
||
| 328 | */ |
||
| 329 | 17 | private function addNotEmptyValidator(): void |
|
| 330 | {
|
||
| 331 | 17 | $notEmpty = new NotEmpty(); |
|
| 332 | 17 | $this->addValidator($notEmpty); |
|
| 333 | 17 | } |
|
| 334 | |||
| 335 | /** |
||
| 336 | * removes not empty validator |
||
| 337 | */ |
||
| 338 | 2 | private function removeNotEmptyValidator(): void |
|
| 339 | {
|
||
| 340 | 2 | $this->validatorCollection->rewind(); |
|
| 341 | |||
| 342 | 2 | while ($this->validatorCollection->valid()) {
|
|
| 343 | 2 | $validator = $this->validatorCollection->current(); |
|
| 344 | 2 | $validator instanceof NotEmpty |
|
| 345 | 2 | ? $this->validatorCollection->offsetUnset($this->validatorCollection->key()) |
|
| 346 | 1 | : null; |
|
| 347 | 2 | $this->validatorCollection->next(); |
|
| 348 | } |
||
| 349 | 2 | } |
|
| 350 | |||
| 351 | /** |
||
| 352 | * @param FormInterface $form |
||
| 353 | * @param string $triggerValue |
||
| 354 | */ |
||
| 355 | 3 | public function addDynamicForm(FormInterface $form, string $triggerValue): void |
|
| 359 | |||
| 360 | /** |
||
| 361 | * @return bool |
||
| 362 | */ |
||
| 363 | 39 | public function hasDynamicForms(): bool |
|
| 364 | {
|
||
| 365 | 39 | return count($this->dynamicFormCollection) > 0; |
|
| 366 | } |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @return FormInterface[] |
||
| 370 | * @throws Exception |
||
| 371 | */ |
||
| 372 | 4 | public function getDynamicForms(): array |
|
| 373 | {
|
||
| 374 | 4 | if (!$this->hasDynamicForms()) {
|
|
| 375 | 1 | throw new Exception('No dynamic form for this value - Did you check hasDynamicForm() ?');
|
|
| 376 | } |
||
| 377 | 3 | return $this->dynamicFormCollection; |
|
| 378 | } |
||
| 379 | } |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..