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 RequestContext $requestContext |
||
70 | * @return $this |
||
71 | */ |
||
72 | 19 | public function handleRequestContext(RequestContext $requestContext) { |
|
86 | |||
87 | |||
88 | /** |
||
89 | * @return array |
||
90 | * @throws \Exception |
||
91 | */ |
||
92 | 2 | public function getData() { |
|
93 | 2 | if ($this->data === null) { |
|
94 | throw new \Exception('Data does not exist!'); |
||
95 | } |
||
96 | |||
97 | 2 | return $this->data; |
|
98 | } |
||
99 | |||
100 | |||
101 | /** |
||
102 | * @param array|\Iterator $data |
||
103 | * @throws \Exception |
||
104 | * @deprecated |
||
105 | */ |
||
106 | public function setData($data) { |
||
107 | trigger_error('Deprecated', E_USER_DEPRECATED); |
||
108 | |||
109 | if ($data instanceof \Iterator) { |
||
110 | $data = iterator_to_array($data); |
||
111 | } |
||
112 | |||
113 | if (!is_array($data)) { |
||
114 | throw new \Exception('Data should be an array'); |
||
115 | } |
||
116 | |||
117 | |||
118 | $this->cleanValidationFlag(); |
||
119 | |||
120 | $formData = []; |
||
121 | foreach ($this->elements as $element) { |
||
122 | $element->clearValue(); |
||
123 | $name = $element->getName(); |
||
124 | |||
125 | if ($element instanceof Checkbox) { |
||
126 | $data[$name] = array_key_exists($name, $data) ? 1 : 0; |
||
127 | } elseif ($element instanceof CheckboxList) { |
||
128 | $data[$name] = isset($data[$name]) ? $data[$name] : []; |
||
129 | } |
||
130 | |||
131 | if (array_key_exists($name, $data)) { |
||
132 | $element->setValue($data[$name]); |
||
133 | $formData[$name] = $element->getValue(); |
||
134 | } |
||
135 | |||
136 | } |
||
137 | |||
138 | $this->isSubmitted = isset($data[$this->getUid()]); |
||
139 | $this->data = $formData; |
||
140 | } |
||
141 | |||
142 | |||
143 | /** |
||
144 | * @return string |
||
145 | */ |
||
146 | 23 | public function getMethod() { |
|
153 | |||
154 | |||
155 | /** |
||
156 | * @param string $method |
||
157 | * @return $this |
||
158 | */ |
||
159 | 3 | public function setMethod($method) { |
|
164 | |||
165 | |||
166 | /** |
||
167 | * |
||
168 | */ |
||
169 | 29 | protected function cleanValidationFlag() { |
|
173 | |||
174 | |||
175 | /** |
||
176 | * Check if form is submitted and all elements are valid |
||
177 | * |
||
178 | * @return boolean |
||
179 | */ |
||
180 | 13 | public function isValid() { |
|
199 | |||
200 | |||
201 | /** |
||
202 | * @return array |
||
203 | */ |
||
204 | 2 | public function getErrors() { |
|
207 | |||
208 | |||
209 | /** |
||
210 | * @param string $error |
||
211 | * @return $this |
||
212 | */ |
||
213 | 1 | protected function addError($error) { |
|
221 | |||
222 | |||
223 | /** |
||
224 | * Check if form is submitted |
||
225 | * |
||
226 | * @return bool |
||
227 | */ |
||
228 | 16 | public function isSubmitted() { |
|
231 | |||
232 | |||
233 | /** |
||
234 | * Return unique id of form |
||
235 | * |
||
236 | * @return string |
||
237 | */ |
||
238 | 23 | public function getUid() { |
|
245 | |||
246 | |||
247 | /** |
||
248 | * @return Element\BaseElement[] |
||
249 | */ |
||
250 | 22 | public function getElements() { |
|
253 | |||
254 | |||
255 | /** |
||
256 | * @param string $name |
||
257 | * @return Element\BaseElement |
||
258 | * @throws \InvalidArgumentException |
||
259 | */ |
||
260 | 4 | public function getElement($name) { |
|
266 | |||
267 | |||
268 | /** |
||
269 | * @param string $name |
||
270 | * @param string|null $text |
||
271 | * @return \Fiv\Form\Element\Input |
||
272 | */ |
||
273 | 13 | public function input($name, $text = null) { |
|
280 | |||
281 | |||
282 | /** |
||
283 | * @param string $name |
||
284 | * @param string|null $text |
||
285 | * @return \Fiv\Form\Element\Input |
||
286 | */ |
||
287 | public function password($name, $text = null) { |
||
294 | |||
295 | |||
296 | /** |
||
297 | * @param string $name |
||
298 | * @param null $text |
||
299 | * @return Select |
||
300 | */ |
||
301 | public function select($name, $text = null) { |
||
308 | |||
309 | |||
310 | /** |
||
311 | * @param string $name |
||
312 | * @param string $text |
||
313 | * @return RadioList |
||
314 | */ |
||
315 | public function radioList($name, $text = null) { |
||
322 | |||
323 | |||
324 | /** |
||
325 | * @param string $name |
||
326 | * @param null $text |
||
327 | * @return TextArea |
||
328 | */ |
||
329 | 5 | public function textarea($name, $text = null) { |
|
336 | |||
337 | |||
338 | /** |
||
339 | * ``` |
||
340 | * $form->hidden('key', md5($this-user->id . HASH_A); |
||
341 | * ``` |
||
342 | * @param string $name |
||
343 | * @param null $value |
||
344 | * @return \Fiv\Form\Element\Input |
||
345 | */ |
||
346 | 3 | public function hidden($name, $value = null) { |
|
354 | |||
355 | |||
356 | /** |
||
357 | * ``` |
||
358 | * $form->submit('register', 'зареєструватись'); |
||
359 | * ``` |
||
360 | * @param string $name |
||
361 | * @param null $value |
||
362 | * @return Submit |
||
363 | */ |
||
364 | 1 | public function submit($name, $value = null) { |
|
371 | |||
372 | |||
373 | /** |
||
374 | * ``` |
||
375 | * $form->checkbox('subscribe', 'Підписка на новини'); |
||
376 | * ``` |
||
377 | * @param string $name |
||
378 | * @param string|null $label |
||
379 | * @return Checkbox |
||
380 | */ |
||
381 | 2 | public function checkbox($name, $label = null) { |
|
388 | |||
389 | |||
390 | /** |
||
391 | * @param string $name |
||
392 | * @param null $text |
||
393 | * @return CheckboxList |
||
394 | */ |
||
395 | public function checkboxList($name, $text = null) { |
||
402 | |||
403 | |||
404 | /** |
||
405 | * Attach element to this form. Overwrite element with same name |
||
406 | * |
||
407 | * @param Element\BaseElement $element |
||
408 | * @return $this |
||
409 | */ |
||
410 | 2 | public function setElement(Element\BaseElement $element) { |
|
415 | |||
416 | |||
417 | /** |
||
418 | * @param Element\BaseElement $element |
||
419 | * @return $this |
||
420 | * @throws \Exception |
||
421 | */ |
||
422 | 25 | public function addElement(Element\BaseElement $element) { |
|
431 | |||
432 | |||
433 | /** |
||
434 | * Render full form |
||
435 | * |
||
436 | * @return string |
||
437 | */ |
||
438 | 2 | public function render() { |
|
441 | |||
442 | |||
443 | /** |
||
444 | * You can easy rewrite this method for custom design of your forms |
||
445 | * |
||
446 | * @return string |
||
447 | */ |
||
448 | 2 | protected function renderElements() { |
|
465 | |||
466 | |||
467 | /** |
||
468 | * @return string |
||
469 | */ |
||
470 | 3 | public function renderStart() { |
|
496 | |||
497 | |||
498 | /** |
||
499 | * @return string |
||
500 | */ |
||
501 | 3 | public function renderEnd() { |
|
504 | } |
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: