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 | 8 | 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 | 16 | 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 | 24 | protected function cleanValidationFlag() { |
|
149 | |||
150 | |||
151 | /** |
||
152 | * Check if form is submitted and all elements are valid |
||
153 | * |
||
154 | * @return boolean |
||
155 | */ |
||
156 | 11 | 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 | 13 | public function isSubmitted() { |
|
207 | |||
208 | |||
209 | /** |
||
210 | * Return unique id of form |
||
211 | * |
||
212 | * @return string |
||
213 | */ |
||
214 | 19 | public function getUid() { |
|
221 | |||
222 | |||
223 | /** |
||
224 | * @return Element\BaseElement[] |
||
225 | */ |
||
226 | 5 | public function getElements() { |
|
229 | |||
230 | |||
231 | /** |
||
232 | * @param string $name |
||
233 | * @param string|null $text |
||
234 | * @return \Fiv\Form\Element\Input |
||
235 | */ |
||
236 | 10 | public function input($name, $text = null) { |
|
243 | |||
244 | |||
245 | /** |
||
246 | * @param string $name |
||
247 | * @param string|null $text |
||
248 | * @return \Fiv\Form\Element\Input |
||
249 | */ |
||
250 | public function password($name, $text = null) { |
||
257 | |||
258 | |||
259 | /** |
||
260 | * @param string $name |
||
261 | * @param null $text |
||
262 | * @return Select |
||
263 | */ |
||
264 | public function select($name, $text = null) { |
||
271 | |||
272 | |||
273 | /** |
||
274 | * @param string $name |
||
275 | * @param string $text |
||
276 | * @return RadioList |
||
277 | */ |
||
278 | public function radioList($name, $text = null) { |
||
285 | |||
286 | |||
287 | /** |
||
288 | * @param string $name |
||
289 | * @param null $text |
||
290 | * @return TextArea |
||
291 | */ |
||
292 | 4 | public function textarea($name, $text = null) { |
|
299 | |||
300 | |||
301 | /** |
||
302 | * ``` |
||
303 | * $form->hidden('key', md5($this-user->id . HASH_A); |
||
304 | * ``` |
||
305 | * @param string $name |
||
306 | * @param null $value |
||
307 | * @return \Fiv\Form\Element\Input |
||
308 | */ |
||
309 | 3 | public function hidden($name, $value = null) { |
|
317 | |||
318 | |||
319 | /** |
||
320 | * ``` |
||
321 | * $form->submit('register', 'зареєструватись'); |
||
322 | * ``` |
||
323 | * @param string $name |
||
324 | * @param null $value |
||
325 | * @return Submit |
||
326 | */ |
||
327 | 1 | public function submit($name, $value = null) { |
|
334 | |||
335 | |||
336 | /** |
||
337 | * ``` |
||
338 | * $form->checkbox('subscribe', 'Підписка на новини'); |
||
339 | * ``` |
||
340 | * @param string $name |
||
341 | * @param string|null $label |
||
342 | * @return Checkbox |
||
343 | */ |
||
344 | 2 | public function checkbox($name, $label = null) { |
|
351 | |||
352 | |||
353 | /** |
||
354 | * @param string $name |
||
355 | * @param null $text |
||
356 | * @return CheckboxList |
||
357 | */ |
||
358 | public function checkboxList($name, $text = null) { |
||
365 | |||
366 | |||
367 | /** |
||
368 | * Attach element to this form. Overwrite element with same name |
||
369 | * |
||
370 | * @param Element\BaseElement $element |
||
371 | * @return $this |
||
372 | */ |
||
373 | 2 | public function setElement(Element\BaseElement $element) { |
|
378 | |||
379 | |||
380 | /** |
||
381 | * @param Element\BaseElement $element |
||
382 | * @return $this |
||
383 | * @throws \Exception |
||
384 | */ |
||
385 | 21 | public function addElement(Element\BaseElement $element) { |
|
394 | |||
395 | |||
396 | /** |
||
397 | * Render full form |
||
398 | * |
||
399 | * @return string |
||
400 | */ |
||
401 | 2 | public function render() { |
|
404 | |||
405 | |||
406 | /** |
||
407 | * You can easy rewrite this method for custom design of your forms |
||
408 | * |
||
409 | * @return string |
||
410 | */ |
||
411 | 2 | protected function renderElements() { |
|
428 | |||
429 | |||
430 | /** |
||
431 | * @return string |
||
432 | */ |
||
433 | 3 | public function renderStart() { |
|
459 | |||
460 | |||
461 | /** |
||
462 | * @return string |
||
463 | */ |
||
464 | 3 | public function renderEnd() { |
|
467 | } |
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: