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 |
||
29 | */ |
||
30 | protected $errorList = []; |
||
31 | |||
32 | /** |
||
33 | * @var array|null |
||
34 | */ |
||
35 | protected $data = null; |
||
36 | |||
37 | /** |
||
38 | * @var bool |
||
39 | */ |
||
40 | protected $isSubmitted = false; |
||
41 | |||
42 | /** |
||
43 | * @var Element\BaseElement[] |
||
44 | */ |
||
45 | protected $elements = []; |
||
46 | |||
47 | /** |
||
48 | * Default form attributes |
||
49 | * |
||
50 | * @var array |
||
51 | */ |
||
52 | protected $attributes = [ |
||
53 | 'method' => 'post', |
||
54 | ]; |
||
55 | |||
56 | |||
57 | /** |
||
58 | * @param $name |
||
59 | * @return $this |
||
60 | */ |
||
61 | 9 | public function setName($name) { |
|
66 | |||
67 | |||
68 | /** |
||
69 | * @param FormData $data |
||
70 | * @return $this |
||
71 | */ |
||
72 | 19 | public function handle(FormData $data) { |
|
73 | 19 | $this->cleanValidationFlag(); |
|
74 | 19 | foreach ($this->getElements() as $element) { |
|
75 | 17 | $element->handle($data); |
|
76 | } |
||
77 | |||
78 | 19 | $this->data = $data->getData(); |
|
79 | 19 | $this->isSubmitted = false; |
|
80 | 19 | if ($data->isMethod($this->getMethod()) and $data->has($this->getUid())) { |
|
81 | 17 | $this->isSubmitted = true; |
|
82 | } |
||
83 | |||
84 | 19 | return $this; |
|
85 | } |
||
86 | |||
87 | |||
88 | /** |
||
89 | * @return array |
||
90 | * @throws \Exception |
||
91 | */ |
||
92 | 2 | public function getData() { |
|
99 | |||
100 | |||
101 | /** |
||
102 | * @param array|\Iterator $data |
||
103 | * @throws \Exception |
||
104 | * @deprecated |
||
105 | */ |
||
106 | public function setData($data) { |
||
140 | |||
141 | |||
142 | /** |
||
143 | * @return string |
||
144 | */ |
||
145 | 23 | public function getMethod() { |
|
152 | |||
153 | |||
154 | /** |
||
155 | * @param string $method |
||
156 | * @return $this |
||
157 | */ |
||
158 | 3 | public function setMethod($method) { |
|
163 | |||
164 | |||
165 | /** |
||
166 | * |
||
167 | */ |
||
168 | 29 | protected function cleanValidationFlag() { |
|
172 | |||
173 | |||
174 | /** |
||
175 | * Check if form is submitted and all elements are valid |
||
176 | * |
||
177 | * @return boolean |
||
178 | */ |
||
179 | 13 | public function isValid() { |
|
198 | |||
199 | |||
200 | /** |
||
201 | * @return array |
||
202 | */ |
||
203 | 2 | public function getErrors() { |
|
206 | |||
207 | |||
208 | /** |
||
209 | * @param string $error |
||
210 | * @return $this |
||
211 | */ |
||
212 | 1 | protected function addError($error) { |
|
220 | |||
221 | |||
222 | /** |
||
223 | * Check if form is submitted |
||
224 | * |
||
225 | * @return bool |
||
226 | */ |
||
227 | 16 | public function isSubmitted() { |
|
230 | |||
231 | |||
232 | /** |
||
233 | * Return unique id of form |
||
234 | * |
||
235 | * @return string |
||
236 | */ |
||
237 | 23 | public function getUid() { |
|
244 | |||
245 | |||
246 | /** |
||
247 | * @return Element\BaseElement[] |
||
248 | */ |
||
249 | 22 | public function getElements() { |
|
252 | |||
253 | |||
254 | /** |
||
255 | * @param string $name |
||
256 | * @return Element\BaseElement |
||
257 | * @throws \InvalidArgumentException |
||
258 | */ |
||
259 | 4 | public function getElement($name) { |
|
265 | |||
266 | |||
267 | /** |
||
268 | * @param string $name |
||
269 | * @param string|null $text |
||
270 | * @return \Fiv\Form\Element\Input |
||
271 | */ |
||
272 | 13 | public function input($name, $text = null) { |
|
279 | |||
280 | |||
281 | /** |
||
282 | * @param string $name |
||
283 | * @param string|null $text |
||
284 | * @return \Fiv\Form\Element\Input |
||
285 | */ |
||
286 | public function password($name, $text = null) { |
||
293 | |||
294 | |||
295 | /** |
||
296 | * @param string $name |
||
297 | * @param null $text |
||
298 | * @return Select |
||
299 | */ |
||
300 | public function select($name, $text = null) { |
||
307 | |||
308 | |||
309 | /** |
||
310 | * @param string $name |
||
311 | * @param string $text |
||
312 | * @return RadioList |
||
313 | */ |
||
314 | public function radioList($name, $text = null) { |
||
321 | |||
322 | |||
323 | /** |
||
324 | * @param string $name |
||
325 | * @param null $text |
||
326 | * @return TextArea |
||
327 | */ |
||
328 | 5 | public function textarea($name, $text = null) { |
|
335 | |||
336 | |||
337 | /** |
||
338 | * ``` |
||
339 | * $form->hidden('key', md5($this-user->id . HASH_A); |
||
340 | * ``` |
||
341 | * @param string $name |
||
342 | * @param null $value |
||
343 | * @return \Fiv\Form\Element\Input |
||
344 | */ |
||
345 | 3 | public function hidden($name, $value = null) { |
|
353 | |||
354 | |||
355 | /** |
||
356 | * ``` |
||
357 | * $form->submit('register', 'зареєструватись'); |
||
358 | * ``` |
||
359 | * @param string $name |
||
360 | * @param null $value |
||
361 | * @return Submit |
||
362 | */ |
||
363 | 1 | public function submit($name, $value = null) { |
|
370 | |||
371 | |||
372 | /** |
||
373 | * ``` |
||
374 | * $form->checkbox('subscribe', 'Підписка на новини'); |
||
375 | * ``` |
||
376 | * @param string $name |
||
377 | * @param string|null $label |
||
378 | * @return Checkbox |
||
379 | */ |
||
380 | 2 | public function checkbox($name, $label = null) { |
|
387 | |||
388 | |||
389 | /** |
||
390 | * @param string $name |
||
391 | * @param null $text |
||
392 | * @return CheckboxList |
||
393 | */ |
||
394 | public function checkboxList($name, $text = null) { |
||
401 | |||
402 | |||
403 | /** |
||
404 | * Attach element to this form. Overwrite element with same name |
||
405 | * |
||
406 | * @param Element\BaseElement $element |
||
407 | * @return $this |
||
408 | */ |
||
409 | 2 | public function setElement(Element\BaseElement $element) { |
|
414 | |||
415 | |||
416 | /** |
||
417 | * @param Element\BaseElement $element |
||
418 | * @return $this |
||
419 | * @throws \Exception |
||
420 | */ |
||
421 | 25 | public function addElement(Element\BaseElement $element) { |
|
430 | |||
431 | |||
432 | /** |
||
433 | * Render full form |
||
434 | * |
||
435 | * @return string |
||
436 | */ |
||
437 | 2 | public function render() { |
|
440 | |||
441 | |||
442 | /** |
||
443 | * You can easy rewrite this method for custom design of your forms |
||
444 | * |
||
445 | * @return string |
||
446 | */ |
||
447 | 2 | protected function renderElements() { |
|
464 | |||
465 | |||
466 | /** |
||
467 | * @return string |
||
468 | */ |
||
469 | 3 | public function renderStart() { |
|
495 | |||
496 | |||
497 | /** |
||
498 | * @return string |
||
499 | */ |
||
500 | 3 | public function renderEnd() { |
|
503 | } |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: