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 |
||
23 | abstract class AbstractFormBuilder |
||
24 | { |
||
25 | |||
26 | /** @var string */ |
||
27 | protected $identifier = ''; |
||
28 | |||
29 | /** @var array */ |
||
30 | protected $formAttributes = []; |
||
31 | |||
32 | /** @var AbstractType[] */ |
||
33 | protected $fields = []; |
||
34 | |||
35 | /** @var Entity|null */ |
||
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 string|null */ |
||
51 | private $confirmValue = null; |
||
52 | |||
53 | /** |
||
54 | * AbstractFormBuilder constructor |
||
55 | * @param Entity $entity |
||
56 | * @codeCoverageIgnore |
||
57 | */ |
||
58 | public function __construct(Entity $entity = null) |
||
66 | |||
67 | /** |
||
68 | * Get field object |
||
69 | * |
||
70 | * @param string $name |
||
71 | * |
||
72 | * @return AbstractType |
||
73 | * |
||
74 | * @throws InvalidFormElementException |
||
75 | */ |
||
76 | public function getField(string $name) |
||
88 | |||
89 | /** |
||
90 | * @param AbstractType $field |
||
91 | * |
||
92 | * @return self |
||
93 | */ |
||
94 | public function setField(AbstractType $field) |
||
99 | |||
100 | /** |
||
101 | * @return string |
||
102 | */ |
||
103 | public function getIdentifier() |
||
107 | |||
108 | /** |
||
109 | * @param array $attributes |
||
110 | */ |
||
111 | public function setFormAttributes(array $attributes) |
||
115 | |||
116 | /** |
||
117 | * @return string |
||
118 | */ |
||
119 | public function getFormOpen() |
||
145 | |||
146 | /** |
||
147 | * @return string |
||
148 | */ |
||
149 | public function getFormClose() |
||
153 | |||
154 | /** |
||
155 | * @param string $prefix |
||
156 | * @param string $suffix |
||
157 | */ |
||
158 | public function setFormErrorContainer(string $prefix, string $suffix) |
||
163 | |||
164 | /** |
||
165 | * @param string $prefix |
||
166 | * @param string $suffix |
||
167 | */ |
||
168 | public function setFormErrorItemContainer(string $prefix, string $suffix) |
||
173 | |||
174 | /** |
||
175 | * @return array |
||
176 | */ |
||
177 | public function getData() |
||
205 | |||
206 | /** |
||
207 | * @param array $data |
||
208 | */ |
||
209 | public function setData(array $data) |
||
221 | |||
222 | /** |
||
223 | * @return bool |
||
224 | */ |
||
225 | public function isValid() :bool |
||
242 | |||
243 | /** |
||
244 | * Add a form field |
||
245 | * |
||
246 | * @param array $definition |
||
247 | * |
||
248 | * @throws InvalidArgumentException |
||
249 | * @throws ServiceNotFoundException |
||
250 | * @throws FormInvalidException |
||
251 | */ |
||
252 | public function add(array $definition) |
||
298 | |||
299 | /** |
||
300 | * @param AbstractType $typeClass |
||
301 | * @param array $definition |
||
302 | * |
||
303 | * @return bool |
||
304 | * |
||
305 | * @throws FormInvalidException |
||
306 | */ |
||
307 | private function _addValidators(AbstractType &$typeClass, array $definition) |
||
341 | |||
342 | /** |
||
343 | * @return mixed |
||
344 | */ |
||
345 | abstract protected function create(); |
||
346 | |||
347 | } |