Complex classes like AbstractFormBuilder 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 AbstractFormBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | abstract class AbstractFormBuilder |
||
| 21 | { |
||
| 22 | |||
| 23 | /** @var string */ |
||
| 24 | protected $identifier = ''; |
||
| 25 | |||
| 26 | /** @var array */ |
||
| 27 | protected $formAttributes = []; |
||
| 28 | |||
| 29 | /** @var AbstractType[] */ |
||
| 30 | protected $fields = []; |
||
| 31 | |||
| 32 | /** @var Entity */ |
||
| 33 | protected $entity = null; |
||
| 34 | |||
| 35 | /** @var string */ |
||
| 36 | protected $formErrorContainerPrefix = ''; |
||
| 37 | |||
| 38 | /** @var string */ |
||
| 39 | protected $formErrorContainerSuffix = ''; |
||
| 40 | |||
| 41 | /** @var string */ |
||
| 42 | protected $formErrorItemContainerPrefix = ''; |
||
| 43 | |||
| 44 | /** @var string */ |
||
| 45 | protected $formErrorItemContainerSuffix = ''; |
||
| 46 | |||
| 47 | /** @var */ |
||
| 48 | private $confirmValue = null; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * AbstractFormBuilder constructor |
||
| 52 | * @param Entity $entity |
||
| 53 | * @codeCoverageIgnore |
||
| 54 | */ |
||
| 55 | public function __construct(Entity $entity = null) |
||
| 56 | { |
||
| 57 | if ($entity !== null) { |
||
| 58 | $this->create(); |
||
| 59 | $this->setData($entity->getDataAsArray()); |
||
| 60 | } |
||
| 61 | |||
| 62 | $this->create(); |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @param string $name |
||
| 67 | * @return AbstractType |
||
| 68 | * @throws InvalidFormElementException |
||
| 69 | * @codeCoverageIgnore |
||
| 70 | */ |
||
| 71 | public function getField(string $name) |
||
| 72 | { |
||
| 73 | if (empty($this->fields[$name])) { |
||
| 74 | throw new InvalidFormElementException('No field with name \'' . $name . '\' found'); |
||
| 75 | } |
||
| 76 | return $this->fields[$name]; |
||
| 77 | } |
||
| 78 | |||
| 79 | public function setField(AbstractType $field) |
||
| 80 | { |
||
| 81 | $this->fields[$field->getName()] = $field; |
||
| 82 | return $this; |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @return string |
||
| 87 | */ |
||
| 88 | public function getIdentifier() |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param array $attributes |
||
| 95 | */ |
||
| 96 | public function setFormAttributes(array $attributes) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @return string |
||
| 103 | */ |
||
| 104 | public function getFormOpen() |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @return string |
||
| 116 | */ |
||
| 117 | public function getFormClose() |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param string $prefix |
||
| 124 | * @param string $suffix |
||
| 125 | * @codeCoverageIgnore |
||
| 126 | */ |
||
| 127 | public function setFormErrorContainer(string $prefix, string $suffix) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @param string $prefix |
||
| 135 | * @param string $suffix |
||
| 136 | * @codeCoverageIgnore |
||
| 137 | */ |
||
| 138 | public function setFormErrorItemContainer(string $prefix, string $suffix) |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @return array |
||
| 146 | * @codeCoverageIgnore |
||
| 147 | */ |
||
| 148 | public function getData() |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @param array $data |
||
| 175 | * @codeCoverageIgnore |
||
| 176 | */ |
||
| 177 | public function setData(array $data) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @return bool |
||
| 192 | * @codeCoverageIgnore |
||
| 193 | */ |
||
| 194 | public function isValid() :bool |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param array $definition |
||
| 214 | * @throws InvalidArgumentException |
||
| 215 | * @codeCoverageIgnore |
||
| 216 | */ |
||
| 217 | public function add(array $definition) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @param AbstractType $typeClass |
||
| 266 | * @param array $definition |
||
| 267 | * @throws FormInvalidException |
||
| 268 | * @return boolean |
||
| 269 | * @codeCoverageIgnore |
||
| 270 | */ |
||
| 271 | private function _addValidators(AbstractType &$typeClass, array $definition) |
||
| 272 | { |
||
| 273 | if (!empty($definition['validator'])) { |
||
| 274 | |||
| 275 | if ($definition['validator'] === 'none') { |
||
| 276 | return true; |
||
| 277 | } |
||
| 278 | |||
| 279 | $validatorChain = new ValidatorChain($typeClass); |
||
| 280 | |||
| 281 | foreach ($definition['validator'] as $validator) { |
||
| 282 | |||
| 283 | if (!class_exists($validator)) { |
||
| 284 | throw new FormInvalidException('Validator "' . $validator . '" doesn\'t exists'); |
||
| 285 | } |
||
| 286 | |||
| 287 | $validatorChain->add(new $validator($typeClass)); |
||
| 288 | |||
| 289 | } |
||
| 290 | |||
| 291 | $typeClass->setValidatorChain($validatorChain); |
||
| 292 | |||
| 293 | } else { |
||
| 294 | |||
| 295 | $validator = '\Faulancer\Form\Validator\Base\\' . ucfirst($typeClass->getType()); |
||
| 296 | |||
| 297 | if (empty($definition['validator']) && class_exists($validator)) { |
||
| 298 | $typeClass->setDefaultValidator(new $validator($typeClass)); |
||
| 299 | } |
||
| 300 | |||
| 301 | } |
||
| 302 | |||
| 303 | return true; |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @return mixed |
||
| 308 | */ |
||
| 309 | abstract protected function create(); |
||
| 310 | |||
| 311 | } |