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 |
||
18 | class Form extends Element\Html { |
||
19 | |||
20 | /** |
||
21 | * @var null|string |
||
22 | */ |
||
23 | protected $uid = null; |
||
24 | |||
25 | /** |
||
26 | * @var boolean|null |
||
27 | */ |
||
28 | protected $validationResult = null; |
||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $errorList = []; |
||
34 | |||
35 | /** |
||
36 | * @var bool |
||
37 | */ |
||
38 | protected $isSubmitted = false; |
||
39 | |||
40 | /** |
||
41 | * @var DataElementInterface[] |
||
42 | */ |
||
43 | protected $elements = []; |
||
44 | |||
45 | /** |
||
46 | * Default form attributes |
||
47 | * |
||
48 | * @var array |
||
49 | */ |
||
50 | protected $attributes = [ |
||
51 | 'method' => 'post', |
||
52 | ]; |
||
53 | |||
54 | |||
55 | /** |
||
56 | * @param $name |
||
57 | * @return $this |
||
58 | */ |
||
59 | 14 | public function setName($name) { |
|
64 | |||
65 | |||
66 | /** |
||
67 | * @param FormData $data |
||
68 | * @return $this |
||
69 | */ |
||
70 | 27 | public function handle(FormData $data) { |
|
84 | |||
85 | |||
86 | /** |
||
87 | * @return string |
||
88 | */ |
||
89 | 31 | public function getMethod() { |
|
96 | |||
97 | |||
98 | /** |
||
99 | * @param string $method |
||
100 | * @return $this |
||
101 | */ |
||
102 | 3 | public function setMethod($method) { |
|
107 | |||
108 | |||
109 | /** |
||
110 | * |
||
111 | */ |
||
112 | 37 | protected function cleanValidationFlag() { |
|
116 | |||
117 | |||
118 | /** |
||
119 | * Check if form is submitted and all elements are valid |
||
120 | * |
||
121 | * @return boolean |
||
122 | */ |
||
123 | 14 | public function isValid() { |
|
150 | |||
151 | |||
152 | /** |
||
153 | * @return array |
||
154 | */ |
||
155 | 2 | public function getErrors() { |
|
158 | |||
159 | |||
160 | /** |
||
161 | * @param string $error |
||
162 | * @return $this |
||
163 | */ |
||
164 | 7 | protected function addError($error) { |
|
172 | |||
173 | |||
174 | /** |
||
175 | * Check if form is submitted |
||
176 | * |
||
177 | * @return bool |
||
178 | */ |
||
179 | 17 | public function isSubmitted() { |
|
182 | |||
183 | |||
184 | /** |
||
185 | * Return unique id of form |
||
186 | * |
||
187 | * @return string |
||
188 | */ |
||
189 | 31 | public function getUid() { |
|
196 | |||
197 | |||
198 | /** |
||
199 | * @return DataElementInterface[] |
||
200 | */ |
||
201 | 32 | public function getElements() { |
|
204 | |||
205 | |||
206 | /** |
||
207 | * @param string $name |
||
208 | * @return DataElementInterface |
||
209 | * @throws \InvalidArgumentException |
||
210 | */ |
||
211 | 4 | public function getElement($name) { |
|
217 | |||
218 | |||
219 | /** |
||
220 | * Attach element to this form. Overwrite element with same name |
||
221 | * |
||
222 | * @param DataElementInterface $element |
||
223 | * @return $this |
||
224 | */ |
||
225 | 2 | public function setElement(DataElementInterface $element) { |
|
230 | |||
231 | |||
232 | /** |
||
233 | * @param DataElementInterface $element |
||
234 | * @return $this |
||
235 | * @throws \Exception |
||
236 | */ |
||
237 | 33 | public function addElement(DataElementInterface $element) { |
|
246 | |||
247 | |||
248 | /** |
||
249 | * @param string $name |
||
250 | * @param string|null $text |
||
251 | * @return \Fiv\Form\Element\Input |
||
252 | */ |
||
253 | 12 | public function input($name, $text = null) { |
|
260 | |||
261 | |||
262 | /** |
||
263 | * @param string $name |
||
264 | * @param string|null $text |
||
265 | * @return \Fiv\Form\Element\Input |
||
266 | */ |
||
267 | public function password($name, $text = null) { |
||
274 | |||
275 | |||
276 | /** |
||
277 | * @param string $name |
||
278 | * @param null $text |
||
279 | * @return Select |
||
280 | */ |
||
281 | public function select($name, $text = null) { |
||
288 | |||
289 | |||
290 | /** |
||
291 | * @param string $name |
||
292 | * @param string $text |
||
293 | * @return RadioList |
||
294 | */ |
||
295 | public function radioList($name, $text = null) { |
||
302 | |||
303 | |||
304 | /** |
||
305 | * @param string $name |
||
306 | * @param null $text |
||
307 | * @return TextArea |
||
308 | */ |
||
309 | 5 | public function textarea($name, $text = null) { |
|
316 | |||
317 | |||
318 | /** |
||
319 | * ``` |
||
320 | * $form->hidden('key', md5($this-user->id . HASH_A); |
||
321 | * ``` |
||
322 | * @param string $name |
||
323 | * @param null $value |
||
324 | * @return \Fiv\Form\Element\Input |
||
325 | */ |
||
326 | 3 | public function hidden($name, $value = null) { |
|
334 | |||
335 | |||
336 | /** |
||
337 | * ``` |
||
338 | * $form->submit('register', 'зареєструватись'); |
||
339 | * ``` |
||
340 | * @param string $name |
||
341 | * @param null $value |
||
342 | * @return Submit |
||
343 | */ |
||
344 | 3 | public function submit($name, $value = null) { |
|
351 | |||
352 | |||
353 | /** |
||
354 | * ``` |
||
355 | * $form->checkbox('subscribe', 'Підписка на новини'); |
||
356 | * ``` |
||
357 | * @param string $name |
||
358 | * @param string|null $label |
||
359 | * @return Checkbox |
||
360 | */ |
||
361 | public function checkbox($name, $label = null) { |
||
367 | |||
368 | |||
369 | /** |
||
370 | * @param string $name |
||
371 | * @param null $text |
||
372 | * @return CheckboxList |
||
373 | */ |
||
374 | public function checkboxList($name, $text = null) { |
||
381 | |||
382 | |||
383 | /** |
||
384 | * Render full form |
||
385 | * |
||
386 | * @return string |
||
387 | */ |
||
388 | 2 | public function render() { |
|
391 | |||
392 | |||
393 | /** |
||
394 | * You can easy rewrite this method for custom design of your forms |
||
395 | * |
||
396 | * @return string |
||
397 | */ |
||
398 | 2 | protected function renderElements() { |
|
415 | |||
416 | |||
417 | /** |
||
418 | * @return string |
||
419 | */ |
||
420 | 3 | public function renderStart() { |
|
446 | |||
447 | |||
448 | /** |
||
449 | * @return string |
||
450 | */ |
||
451 | 3 | public function renderEnd() { |
|
454 | } |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: