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 | * @return array |
||
70 | * @throws \Exception |
||
71 | */ |
||
72 | 1 | public function getData() { |
|
79 | |||
80 | |||
81 | /** |
||
82 | * @param array|\Iterator $data |
||
83 | * @throws \Exception |
||
84 | */ |
||
85 | 17 | public function setData($data) { |
|
117 | |||
118 | |||
119 | /** |
||
120 | * @return string |
||
121 | */ |
||
122 | 4 | public function getMethod() { |
|
129 | |||
130 | |||
131 | /** |
||
132 | * @param string $method |
||
133 | * @return $this |
||
134 | */ |
||
135 | 2 | public function setMethod($method) { |
|
140 | |||
141 | |||
142 | /** |
||
143 | * |
||
144 | */ |
||
145 | 26 | protected function cleanValidationFlag() { |
|
149 | |||
150 | |||
151 | /** |
||
152 | * Check if form is submitted and all elements are valid |
||
153 | * |
||
154 | * @return boolean |
||
155 | */ |
||
156 | 12 | public function isValid() { |
|
175 | |||
176 | |||
177 | /** |
||
178 | * @return array |
||
179 | */ |
||
180 | 2 | public function getErrors() { |
|
183 | |||
184 | |||
185 | /** |
||
186 | * @param string $error |
||
187 | * @return $this |
||
188 | */ |
||
189 | 1 | protected function addError($error) { |
|
197 | |||
198 | |||
199 | /** |
||
200 | * Check if form is submitted |
||
201 | * |
||
202 | * @return bool |
||
203 | */ |
||
204 | 14 | public function isSubmitted() { |
|
207 | |||
208 | |||
209 | /** |
||
210 | * Return unique id of form |
||
211 | * |
||
212 | * @return string |
||
213 | */ |
||
214 | 20 | public function getUid() { |
|
221 | |||
222 | |||
223 | /** |
||
224 | * @return Element\BaseElement[] |
||
225 | */ |
||
226 | 6 | public function getElements() { |
|
229 | |||
230 | |||
231 | /** |
||
232 | * @param string $name |
||
233 | * @return Element\BaseElement |
||
234 | * @throws \InvalidArgumentException |
||
235 | */ |
||
236 | 2 | public function getElement($name) { |
|
242 | |||
243 | |||
244 | /** |
||
245 | * @param string $name |
||
246 | * @param string|null $text |
||
247 | * @return \Fiv\Form\Element\Input |
||
248 | */ |
||
249 | 12 | public function input($name, $text = null) { |
|
256 | |||
257 | |||
258 | /** |
||
259 | * @param string $name |
||
260 | * @param string|null $text |
||
261 | * @return \Fiv\Form\Element\Input |
||
262 | */ |
||
263 | public function password($name, $text = null) { |
||
270 | |||
271 | |||
272 | /** |
||
273 | * @param string $name |
||
274 | * @param null $text |
||
275 | * @return Select |
||
276 | */ |
||
277 | public function select($name, $text = null) { |
||
284 | |||
285 | |||
286 | /** |
||
287 | * @param string $name |
||
288 | * @param string $text |
||
289 | * @return RadioList |
||
290 | */ |
||
291 | public function radioList($name, $text = null) { |
||
298 | |||
299 | |||
300 | /** |
||
301 | * @param string $name |
||
302 | * @param null $text |
||
303 | * @return TextArea |
||
304 | */ |
||
305 | 5 | public function textarea($name, $text = null) { |
|
312 | |||
313 | |||
314 | /** |
||
315 | * ``` |
||
316 | * $form->hidden('key', md5($this-user->id . HASH_A); |
||
317 | * ``` |
||
318 | * @param string $name |
||
319 | * @param null $value |
||
320 | * @return \Fiv\Form\Element\Input |
||
321 | */ |
||
322 | 3 | public function hidden($name, $value = null) { |
|
330 | |||
331 | |||
332 | /** |
||
333 | * ``` |
||
334 | * $form->submit('register', 'зареєструватись'); |
||
335 | * ``` |
||
336 | * @param string $name |
||
337 | * @param null $value |
||
338 | * @return Submit |
||
339 | */ |
||
340 | 1 | public function submit($name, $value = null) { |
|
347 | |||
348 | |||
349 | /** |
||
350 | * ``` |
||
351 | * $form->checkbox('subscribe', 'Підписка на новини'); |
||
352 | * ``` |
||
353 | * @param string $name |
||
354 | * @param string|null $label |
||
355 | * @return Checkbox |
||
356 | */ |
||
357 | 2 | public function checkbox($name, $label = null) { |
|
364 | |||
365 | |||
366 | /** |
||
367 | * @param string $name |
||
368 | * @param null $text |
||
369 | * @return CheckboxList |
||
370 | */ |
||
371 | public function checkboxList($name, $text = null) { |
||
378 | |||
379 | |||
380 | /** |
||
381 | * Attach element to this form. Overwrite element with same name |
||
382 | * |
||
383 | * @param Element\BaseElement $element |
||
384 | * @return $this |
||
385 | */ |
||
386 | 2 | public function setElement(Element\BaseElement $element) { |
|
391 | |||
392 | |||
393 | /** |
||
394 | * @param Element\BaseElement $element |
||
395 | * @return $this |
||
396 | * @throws \Exception |
||
397 | */ |
||
398 | 23 | public function addElement(Element\BaseElement $element) { |
|
407 | |||
408 | |||
409 | /** |
||
410 | * Render full form |
||
411 | * |
||
412 | * @return string |
||
413 | */ |
||
414 | 2 | public function render() { |
|
417 | |||
418 | |||
419 | /** |
||
420 | * You can easy rewrite this method for custom design of your forms |
||
421 | * |
||
422 | * @return string |
||
423 | */ |
||
424 | 2 | protected function renderElements() { |
|
441 | |||
442 | |||
443 | /** |
||
444 | * @return string |
||
445 | */ |
||
446 | 3 | public function renderStart() { |
|
472 | |||
473 | |||
474 | /** |
||
475 | * @return string |
||
476 | */ |
||
477 | 3 | public function renderEnd() { |
|
480 | } |
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: