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 |
||
| 22 | abstract class AbstractFormBuilder |
||
| 23 | { |
||
| 24 | |||
| 25 | /** @var string */ |
||
| 26 | protected $identifier = ''; |
||
| 27 | |||
| 28 | /** @var array */ |
||
| 29 | protected $formAttributes = []; |
||
| 30 | |||
| 31 | /** @var AbstractType[] */ |
||
| 32 | protected $fields = []; |
||
| 33 | |||
| 34 | /** @var Entity|null */ |
||
| 35 | protected $entity = null; |
||
| 36 | |||
| 37 | /** @var string */ |
||
| 38 | protected $formErrorContainerPrefix = ''; |
||
| 39 | |||
| 40 | /** @var string */ |
||
| 41 | protected $formErrorContainerSuffix = ''; |
||
| 42 | |||
| 43 | /** @var string */ |
||
| 44 | protected $formErrorItemContainerPrefix = ''; |
||
| 45 | |||
| 46 | /** @var string */ |
||
| 47 | protected $formErrorItemContainerSuffix = ''; |
||
| 48 | |||
| 49 | /** @var string|null */ |
||
| 50 | private $confirmValue = null; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * AbstractFormBuilder constructor |
||
| 54 | * @param Entity $entity |
||
| 55 | * @codeCoverageIgnore |
||
| 56 | */ |
||
| 57 | public function __construct(Entity $entity = null) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Get field object |
||
| 68 | * |
||
| 69 | * @param string $name |
||
| 70 | * |
||
| 71 | * @return AbstractType |
||
| 72 | * |
||
| 73 | * @throws InvalidFormElementException |
||
| 74 | */ |
||
| 75 | public function getField(string $name) |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @param AbstractType $field |
||
| 86 | * |
||
| 87 | * @return self |
||
| 88 | */ |
||
| 89 | public function setField(AbstractType $field) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @return string |
||
| 97 | */ |
||
| 98 | public function getIdentifier() |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @param array $attributes |
||
| 105 | */ |
||
| 106 | public function setFormAttributes(array $attributes) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @return string |
||
| 113 | */ |
||
| 114 | public function getFormOpen() |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @return string |
||
| 143 | */ |
||
| 144 | public function getFormClose() |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @param string $prefix |
||
| 151 | * @param string $suffix |
||
| 152 | */ |
||
| 153 | public function setFormErrorContainer(string $prefix, string $suffix) |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @param string $prefix |
||
| 161 | * @param string $suffix |
||
| 162 | */ |
||
| 163 | public function setFormErrorItemContainer(string $prefix, string $suffix) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * @return array |
||
| 171 | */ |
||
| 172 | public function getData() |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @param array $data |
||
| 199 | */ |
||
| 200 | public function setData(array $data) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @return bool |
||
| 215 | */ |
||
| 216 | public function isValid() :bool |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Add a form field |
||
| 236 | * |
||
| 237 | * @param array $definition |
||
| 238 | * |
||
| 239 | * @throws InvalidArgumentException |
||
| 240 | * @throws ServiceNotFoundException |
||
| 241 | * @throws FormInvalidException |
||
| 242 | */ |
||
| 243 | public function add(array $definition) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param AbstractType $typeClass |
||
| 292 | * @param array $definition |
||
| 293 | * |
||
| 294 | * @return bool |
||
| 295 | * |
||
| 296 | * @throws FormInvalidException |
||
| 297 | */ |
||
| 298 | private function _addValidators(AbstractType &$typeClass, array $definition) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @return mixed |
||
| 335 | */ |
||
| 336 | abstract protected function create(); |
||
| 337 | |||
| 338 | } |