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 | ||
| 26 | abstract class AbstractFormBuilder | ||
| 27 | { | ||
| 28 | |||
| 29 | /** @var array */ | ||
| 30 | protected $formAttributes = []; | ||
| 31 | |||
| 32 | /** @var AbstractType[] */ | ||
| 33 | protected $fields = []; | ||
| 34 | |||
| 35 | /** @var Entity */ | ||
| 36 | protected $entity = null; | ||
| 37 | |||
| 38 | /** @var string */ | ||
| 39 | protected $formErrorContainerPrefix = ''; | ||
| 40 | |||
| 41 | /** @var string */ | ||
| 42 | protected $formErrorContainerSuffix = ''; | ||
| 43 | |||
| 44 | /** @var string */ | ||
| 45 | protected $formErrorItemContainerPrefix = ''; | ||
| 46 | |||
| 47 | /** @var string */ | ||
| 48 | protected $formErrorItemContainerSuffix = ''; | ||
| 49 | |||
| 50 | /** @var */ | ||
| 51 | private $confirmValue = null; | ||
| 52 | |||
| 53 | /** | ||
| 54 | * AbstractFormBuilder constructor | ||
| 55 | * @param Entity $entity | ||
| 56 | * @codeCoverageIgnore | ||
| 57 | */ | ||
| 58 | public function __construct(Entity $entity = null) | ||
| 67 | |||
| 68 | /** | ||
| 69 | * @param string $name | ||
| 70 | * @return AbstractType | ||
| 71 | * @throws InvalidFormElementException | ||
| 72 | * @codeCoverageIgnore | ||
| 73 | */ | ||
| 74 | public function getField(string $name) | ||
| 81 | |||
| 82 | /** | ||
| 83 | * @param array $attributes | ||
| 84 | */ | ||
| 85 | public function setFormAttributes(array $attributes) | ||
| 89 | |||
| 90 | /** | ||
| 91 | * @return string | ||
| 92 | */ | ||
| 93 | public function getFormOpen() | ||
| 102 | |||
| 103 | /** | ||
| 104 | * @return string | ||
| 105 | */ | ||
| 106 | public function getFormClose() | ||
| 110 | |||
| 111 | /** | ||
| 112 | * @param string $prefix | ||
| 113 | * @param string $suffix | ||
| 114 | * @codeCoverageIgnore | ||
| 115 | */ | ||
| 116 | public function setFormErrorContainer(string $prefix, string $suffix) | ||
| 121 | |||
| 122 | /** | ||
| 123 | * @param string $prefix | ||
| 124 | * @param string $suffix | ||
| 125 | * @codeCoverageIgnore | ||
| 126 | */ | ||
| 127 | public function setFormErrorItemContainer(string $prefix, string $suffix) | ||
| 132 | |||
| 133 | /** | ||
| 134 | * @return array | ||
| 135 | * @codeCoverageIgnore | ||
| 136 | */ | ||
| 137 | public function getData() | ||
| 161 | |||
| 162 | /** | ||
| 163 | * @param array $data | ||
| 164 | * @codeCoverageIgnore | ||
| 165 | */ | ||
| 166 | public function setData(array $data) | ||
| 178 | |||
| 179 | /** | ||
| 180 | * @return bool | ||
| 181 | * @codeCoverageIgnore | ||
| 182 | */ | ||
| 183 | public function isValid() :bool | ||
| 204 | |||
| 205 | /** | ||
| 206 | * @param array $definition | ||
| 207 | * @throws InvalidArgumentException | ||
| 208 | * @codeCoverageIgnore | ||
| 209 | */ | ||
| 210 | protected function add(array $definition) | ||
| 256 | |||
| 257 | /** | ||
| 258 | * @param AbstractType $typeClass | ||
| 259 | * @param array $definition | ||
| 260 | * @throws FormInvalidException | ||
| 261 | * @return boolean | ||
| 262 | * @codeCoverageIgnore | ||
| 263 | */ | ||
| 264 | private function addValidators(AbstractType &$typeClass, array $definition) | ||
| 298 | |||
| 299 | /** | ||
| 300 | * @return mixed | ||
| 301 | */ | ||
| 302 | abstract protected function create(); | ||
| 303 | |||
| 304 | } |