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() |
||
101 | |||
102 | /** |
||
103 | * @return string |
||
104 | */ |
||
105 | public function getFormClose() |
||
109 | |||
110 | /** |
||
111 | * @param string $prefix |
||
112 | * @param string $suffix |
||
113 | * @codeCoverageIgnore |
||
114 | */ |
||
115 | public function setFormErrorContainer(string $prefix, string $suffix) |
||
120 | |||
121 | /** |
||
122 | * @param string $prefix |
||
123 | * @param string $suffix |
||
124 | * @codeCoverageIgnore |
||
125 | */ |
||
126 | public function setFormErrorItemContainer(string $prefix, string $suffix) |
||
131 | |||
132 | /** |
||
133 | * @return array |
||
134 | * @codeCoverageIgnore |
||
135 | */ |
||
136 | public function getData() |
||
160 | |||
161 | /** |
||
162 | * @param array $data |
||
163 | * @codeCoverageIgnore |
||
164 | */ |
||
165 | public function setData(array $data) |
||
177 | |||
178 | /** |
||
179 | * @return bool |
||
180 | * @codeCoverageIgnore |
||
181 | */ |
||
182 | public function isValid() :bool |
||
203 | |||
204 | /** |
||
205 | * @param array $definition |
||
206 | * @throws InvalidArgumentException |
||
207 | * @codeCoverageIgnore |
||
208 | */ |
||
209 | protected function add(array $definition) |
||
255 | |||
256 | /** |
||
257 | * @param AbstractType $typeClass |
||
258 | * @param array $definition |
||
259 | * @throws FormInvalidException |
||
260 | * @return boolean |
||
261 | * @codeCoverageIgnore |
||
262 | */ |
||
263 | private function addValidators(AbstractType &$typeClass, array $definition) |
||
297 | |||
298 | /** |
||
299 | * @return mixed |
||
300 | */ |
||
301 | abstract protected function create(); |
||
302 | |||
303 | } |