Complex classes like Form 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 Form, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class Form extends Element\Html { |
||
16 | |||
17 | /** |
||
18 | * @var null|string |
||
19 | */ |
||
20 | protected $uid = null; |
||
21 | |||
22 | /** |
||
23 | * @var boolean|null |
||
24 | */ |
||
25 | protected $validationResult = null; |
||
26 | |||
27 | /** |
||
28 | * @var array|null |
||
29 | */ |
||
30 | protected $data = null; |
||
31 | |||
32 | /** |
||
33 | * @var Element\BaseElement[] |
||
34 | */ |
||
35 | protected $elements = []; |
||
36 | |||
37 | /** |
||
38 | * Default form attributes |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | protected $attributes = [ |
||
43 | 'method' => 'post', |
||
44 | ]; |
||
45 | |||
46 | |||
47 | /** |
||
48 | * @param $name |
||
49 | * @return $this |
||
50 | */ |
||
51 | 6 | public function setName($name) { |
|
56 | |||
57 | |||
58 | /** |
||
59 | * @return array |
||
60 | * @throws \Exception |
||
61 | */ |
||
62 | 11 | public function getData() { |
|
69 | |||
70 | |||
71 | /** |
||
72 | * @param array|\Iterator $data |
||
73 | * @throws \Exception |
||
74 | */ |
||
75 | 11 | public function setData($data) { |
|
105 | |||
106 | |||
107 | /** |
||
108 | * @return string |
||
109 | */ |
||
110 | 2 | public function getMethod() { |
|
111 | 2 | if (!empty($this->attributes['method'])) { |
|
112 | 2 | return strtolower($this->attributes['method']); |
|
113 | } |
||
114 | |||
115 | 1 | return null; |
|
116 | } |
||
117 | |||
118 | |||
119 | /** |
||
120 | * @param string $method |
||
121 | * @return $this |
||
122 | */ |
||
123 | 2 | public function setMethod($method) { |
|
128 | |||
129 | |||
130 | /** |
||
131 | * |
||
132 | */ |
||
133 | 17 | protected function cleanValidationFlag() { |
|
136 | |||
137 | |||
138 | /** |
||
139 | * Check if form is submitted and all elements are valid |
||
140 | * |
||
141 | * @return boolean |
||
142 | */ |
||
143 | 9 | public function isValid() { |
|
144 | 9 | if ($this->validationResult !== null) { |
|
145 | 1 | return $this->validationResult; |
|
146 | } |
||
147 | |||
148 | 9 | if (!$this->isSubmitted()) { |
|
149 | 1 | return false; |
|
150 | } |
||
151 | |||
152 | 8 | $this->validationResult = true; |
|
153 | 8 | foreach ($this->elements as $element) { |
|
154 | 8 | if (!$element->validate()) { |
|
155 | 8 | $this->validationResult = false; |
|
156 | } |
||
157 | } |
||
158 | |||
159 | 8 | return $this->validationResult; |
|
160 | } |
||
161 | |||
162 | |||
163 | /** |
||
164 | * Check if form is submitted |
||
165 | * |
||
166 | * @return bool |
||
167 | */ |
||
168 | 11 | public function isSubmitted() { |
|
173 | |||
174 | |||
175 | /** |
||
176 | * Return unique id of form |
||
177 | * |
||
178 | * @return string |
||
179 | */ |
||
180 | 12 | public function getUid() { |
|
187 | |||
188 | |||
189 | /** |
||
190 | * @return Element\BaseElement[] |
||
191 | */ |
||
192 | 4 | public function getElements() { |
|
195 | |||
196 | |||
197 | /** |
||
198 | * @param string $name |
||
199 | * @param string|null $text |
||
200 | * @return \Fiv\Form\Element\Input |
||
201 | */ |
||
202 | 7 | public function input($name, $text = null) { |
|
209 | |||
210 | |||
211 | /** |
||
212 | * @param string $name |
||
213 | * @param string|null $text |
||
214 | * @return \Fiv\Form\Element\Input |
||
215 | */ |
||
216 | public function password($name, $text = null) { |
||
217 | $input = new Element\Password(); |
||
218 | $input->setName($name); |
||
219 | $input->setText($text); |
||
220 | $this->addElement($input); |
||
221 | return $input; |
||
222 | } |
||
223 | |||
224 | |||
225 | /** |
||
226 | * @param string $name |
||
227 | * @param null $text |
||
228 | * @return Select |
||
229 | */ |
||
230 | public function select($name, $text = null) { |
||
231 | $select = new Select(); |
||
232 | $select->setName($name); |
||
233 | $select->setText($text); |
||
234 | $this->addElement($select); |
||
235 | return $select; |
||
236 | } |
||
237 | |||
238 | |||
239 | /** |
||
240 | * @param $name |
||
241 | * @param string $text |
||
242 | * @return RadioList |
||
243 | */ |
||
244 | public function radioList($name, $text = null) { |
||
245 | $radio = new RadioList(); |
||
246 | $radio->setName($name); |
||
247 | $radio->setText($text); |
||
248 | $this->addElement($radio); |
||
249 | return $radio; |
||
250 | } |
||
251 | |||
252 | |||
253 | /** |
||
254 | * @param string $name |
||
255 | * @param null $text |
||
256 | * @return TextArea |
||
257 | */ |
||
258 | 3 | public function textarea($name, $text = null) { |
|
265 | |||
266 | |||
267 | /** |
||
268 | * ``` |
||
269 | * $form->hidden('key', md5($this-user->id . HASH_A); |
||
270 | * ``` |
||
271 | * @param string $name |
||
272 | * @param null $value |
||
273 | * @return \Fiv\Form\Element\Input |
||
274 | */ |
||
275 | 2 | public function hidden($name, $value = null) { |
|
283 | |||
284 | |||
285 | /** |
||
286 | * ``` |
||
287 | * $form->submit('register', 'зареєструватись'); |
||
288 | * ``` |
||
289 | * @param string $name |
||
290 | * @param null $value |
||
291 | * @return Submit |
||
292 | */ |
||
293 | 1 | public function submit($name, $value = null) { |
|
300 | |||
301 | |||
302 | /** |
||
303 | * ``` |
||
304 | * $form->checkbox('subscribe', 'Підписка на новини'); |
||
305 | * ``` |
||
306 | * @param string $name |
||
307 | * @param string|null $label |
||
308 | * @return Checkbox |
||
309 | */ |
||
310 | public function checkbox($name, $label = null) { |
||
311 | $checkbox = new Checkbox(); |
||
312 | $checkbox->setName($name); |
||
313 | $checkbox->setLabel($label); |
||
314 | $this->addElement($checkbox); |
||
315 | return $checkbox; |
||
316 | } |
||
317 | |||
318 | |||
319 | /** |
||
320 | * @param string $name |
||
321 | * @param null $text |
||
322 | * @return CheckboxList |
||
323 | */ |
||
324 | public function checkboxList($name, $text = null) { |
||
325 | $checkbox = new CheckboxList(); |
||
326 | $checkbox->setName($name); |
||
327 | $checkbox->setText($text); |
||
328 | $this->addElement($checkbox); |
||
329 | return $checkbox; |
||
330 | } |
||
331 | |||
332 | |||
333 | /** |
||
334 | * Attach element to this form. Overwrite element with same name |
||
335 | * |
||
336 | * @param Element\BaseElement $element |
||
337 | * @return $this |
||
338 | */ |
||
339 | 2 | public function setElement(Element\BaseElement $element) { |
|
344 | |||
345 | |||
346 | /** |
||
347 | * @param Element\BaseElement $element |
||
348 | * @return $this |
||
349 | * @throws \Exception |
||
350 | */ |
||
351 | 14 | public function addElement(Element\BaseElement $element) { |
|
360 | |||
361 | |||
362 | /** |
||
363 | * Render full form |
||
364 | * |
||
365 | * @return string |
||
366 | */ |
||
367 | 1 | public function render() { |
|
370 | |||
371 | |||
372 | /** |
||
373 | * You can easy rewrite this method for custom design of your forms |
||
374 | * |
||
375 | * @return string |
||
376 | */ |
||
377 | 1 | protected function renderElements() { |
|
378 | 1 | $formHtml = '<dl>'; |
|
379 | |||
380 | 1 | foreach ($this->elements as $element) { |
|
381 | $formHtml .= |
||
382 | '<dt>' . $element->getText() . '</dt>' . |
||
383 | '<dd>' . $element->render() . '</dd>'; |
||
384 | } |
||
385 | |||
386 | 1 | $formHtml .= '</dl>'; |
|
387 | 1 | return $formHtml; |
|
388 | } |
||
389 | |||
390 | |||
391 | /** |
||
392 | * @return string |
||
393 | */ |
||
394 | 1 | public function renderStart() { |
|
409 | |||
410 | |||
411 | /** |
||
412 | * @return string |
||
413 | */ |
||
414 | 1 | public function renderEnd() { |
|
417 | |||
418 | } |