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 string */ |
||
30 | protected $identifier = ''; |
||
31 | |||
32 | /** @var array */ |
||
33 | protected $formAttributes = []; |
||
34 | |||
35 | /** @var AbstractType[] */ |
||
36 | protected $fields = []; |
||
37 | |||
38 | /** @var Entity */ |
||
39 | protected $entity = null; |
||
40 | |||
41 | /** @var string */ |
||
42 | protected $formErrorContainerPrefix = ''; |
||
43 | |||
44 | /** @var string */ |
||
45 | protected $formErrorContainerSuffix = ''; |
||
46 | |||
47 | /** @var string */ |
||
48 | protected $formErrorItemContainerPrefix = ''; |
||
49 | |||
50 | /** @var string */ |
||
51 | protected $formErrorItemContainerSuffix = ''; |
||
52 | |||
53 | /** @var */ |
||
54 | private $confirmValue = null; |
||
55 | |||
56 | /** |
||
57 | * AbstractFormBuilder constructor |
||
58 | * @param Entity $entity |
||
59 | * @codeCoverageIgnore |
||
60 | */ |
||
61 | public function __construct(Entity $entity = null) |
||
70 | |||
71 | /** |
||
72 | * @param string $name |
||
73 | * @return AbstractType |
||
74 | * @throws InvalidFormElementException |
||
75 | * @codeCoverageIgnore |
||
76 | */ |
||
77 | public function getField(string $name) |
||
84 | |||
85 | public function setField(AbstractType $field) |
||
90 | |||
91 | /** |
||
92 | * @return string |
||
93 | */ |
||
94 | public function getIdentifier() |
||
98 | |||
99 | /** |
||
100 | * @param array $attributes |
||
101 | */ |
||
102 | public function setFormAttributes(array $attributes) |
||
106 | |||
107 | /** |
||
108 | * @return string |
||
109 | */ |
||
110 | public function getFormOpen() |
||
119 | |||
120 | /** |
||
121 | * @return string |
||
122 | */ |
||
123 | public function getFormClose() |
||
127 | |||
128 | /** |
||
129 | * @param string $prefix |
||
130 | * @param string $suffix |
||
131 | * @codeCoverageIgnore |
||
132 | */ |
||
133 | public function setFormErrorContainer(string $prefix, string $suffix) |
||
138 | |||
139 | /** |
||
140 | * @param string $prefix |
||
141 | * @param string $suffix |
||
142 | * @codeCoverageIgnore |
||
143 | */ |
||
144 | public function setFormErrorItemContainer(string $prefix, string $suffix) |
||
149 | |||
150 | /** |
||
151 | * @return array |
||
152 | * @codeCoverageIgnore |
||
153 | */ |
||
154 | public function getData() |
||
178 | |||
179 | /** |
||
180 | * @param array $data |
||
181 | * @codeCoverageIgnore |
||
182 | */ |
||
183 | public function setData(array $data) |
||
195 | |||
196 | /** |
||
197 | * @return bool |
||
198 | * @codeCoverageIgnore |
||
199 | */ |
||
200 | public function isValid() :bool |
||
217 | |||
218 | /** |
||
219 | * @param array $definition |
||
220 | * @throws InvalidArgumentException |
||
221 | * @codeCoverageIgnore |
||
222 | */ |
||
223 | protected function add(array $definition) |
||
269 | |||
270 | /** |
||
271 | * @param AbstractType $typeClass |
||
272 | * @param array $definition |
||
273 | * @throws FormInvalidException |
||
274 | * @return boolean |
||
275 | * @codeCoverageIgnore |
||
276 | */ |
||
277 | private function addValidators(AbstractType &$typeClass, array $definition) |
||
311 | |||
312 | /** |
||
313 | * @return mixed |
||
314 | */ |
||
315 | abstract protected function create(); |
||
316 | |||
317 | } |