Complex classes like HtmlForm 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 HtmlForm, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class HtmlForm extends HtmlSemCollection { |
||
21 | |||
22 | use FieldsTrait; |
||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $_fields; |
||
27 | |||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $_validationParams; |
||
32 | |||
33 | public function __construct($identifier, $elements=array()) { |
||
41 | |||
42 | /** |
||
43 | * @param string $title |
||
44 | * @param number $niveau |
||
45 | * @param string $dividing |
||
46 | * @return HtmlHeader |
||
47 | */ |
||
48 | public function addHeader($title, $niveau=1, $dividing=true) { |
||
54 | |||
55 | /** |
||
56 | * @param string $caption |
||
57 | * @return \Ajax\semantic\html\collections\form\HtmlForm |
||
58 | */ |
||
59 | public function addDivider($caption=NULL){ |
||
62 | |||
63 | public function addFields($fields=NULL, $label=NULL) { |
||
86 | |||
87 | public function addItem($item) { |
||
94 | |||
95 | public function getField($index) { |
||
103 | |||
104 | /** |
||
105 | * automatically divide fields to be equal width |
||
106 | * @return \Ajax\semantic\html\collections\form\HtmlForm |
||
107 | */ |
||
108 | public function setEqualWidth() { |
||
111 | |||
112 | /** |
||
113 | * Adds a field (alias for addItem) |
||
114 | * @param HtmlFormField $field |
||
115 | * @return \Ajax\common\html\HtmlDoubleElement |
||
116 | */ |
||
117 | public function addField($field) { |
||
120 | |||
121 | public function addFieldRule($index,$type,$prompt=NULL,$value=NULL){ |
||
128 | |||
129 | public function addFieldRules($index,$rules){ |
||
136 | |||
137 | /** |
||
138 | * |
||
139 | * @param string $identifier |
||
140 | * @param string $content |
||
141 | * @param string $header |
||
142 | * @param string $icon |
||
143 | * @param string $type |
||
144 | * @return \Ajax\semantic\html\collections\HtmlMessage |
||
145 | */ |
||
146 | public function addMessage($identifier, $content, $header=NULL, $icon=NULL, $type=NULL) { |
||
156 | |||
157 | private function addCompoValidation($js,$compo,$field){ |
||
168 | |||
169 | public function run(JsUtils $js) { |
||
198 | |||
199 | public function setLoading() { |
||
202 | |||
203 | public function addErrorMessage(){ |
||
206 | |||
207 | public function jsState($state) { |
||
210 | |||
211 | public function setValidationParams(array $_validationParams) { |
||
215 | |||
216 | public function submitOn($event,$identifier,$url,$responseElement){ |
||
224 | |||
225 | /** |
||
226 | * Callback on each valid field |
||
227 | * @param string $jsCode |
||
228 | * @return \Ajax\semantic\html\collections\form\HtmlForm |
||
229 | */ |
||
230 | public function onValid($jsCode){ |
||
234 | |||
235 | /** |
||
236 | * Callback if a form is all valid |
||
237 | * @param string $jsCode can use event and fields parameters |
||
238 | * @return HtmlForm |
||
239 | */ |
||
240 | public function onSuccess($jsCode){ |
||
244 | |||
245 | } |