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|null |
||
29 | */ |
||
30 | protected $data = null; |
||
31 | |||
32 | /** |
||
33 | * @var Element\Base[] |
||
34 | */ |
||
35 | protected $elements = []; |
||
36 | |||
37 | /** |
||
38 | * Default form attributes |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | protected $attributes = [ |
||
43 | 'method' => 'post', |
||
44 | ]; |
||
45 | |||
46 | |||
47 | /** |
||
48 | * @param $name |
||
49 | * @return $this |
||
50 | */ |
||
51 | 4 | public function setName($name) { |
|
56 | |||
57 | |||
58 | /** |
||
59 | * @return array |
||
60 | * @throws \Exception |
||
61 | */ |
||
62 | 9 | public function getData() { |
|
69 | |||
70 | |||
71 | /** |
||
72 | * @param array|\Iterator $data |
||
73 | * @throws \Exception |
||
74 | */ |
||
75 | 9 | public function setData($data) { |
|
105 | |||
106 | |||
107 | /** |
||
108 | * @return string |
||
109 | */ |
||
110 | 2 | public function getMethod() { |
|
117 | |||
118 | |||
119 | /** |
||
120 | * @param string $method |
||
121 | * @return $this |
||
122 | */ |
||
123 | 2 | public function setMethod($method) { |
|
128 | |||
129 | |||
130 | /** |
||
131 | * |
||
132 | */ |
||
133 | 13 | protected function cleanValidationFlag() { |
|
136 | |||
137 | |||
138 | /** |
||
139 | * Check if form is submitted and all elements are valid |
||
140 | * |
||
141 | * @return bool|null |
||
142 | */ |
||
143 | 7 | public function isValid() { |
|
161 | |||
162 | |||
163 | /** |
||
164 | * Check if form is submitted |
||
165 | * |
||
166 | * @return bool |
||
167 | */ |
||
168 | 9 | public function isSubmitted() { |
|
173 | |||
174 | |||
175 | /** |
||
176 | * Return unique id of form |
||
177 | * |
||
178 | * @return string |
||
179 | */ |
||
180 | 10 | public function getUid() { |
|
187 | |||
188 | |||
189 | /** |
||
190 | * @return \Fiv\Form\Element\Base[] |
||
191 | */ |
||
192 | 1 | public function getElements() { |
|
195 | |||
196 | |||
197 | /** |
||
198 | * @param string $name |
||
199 | * @param string|null $text |
||
200 | * @return \Fiv\Form\Element\Input |
||
201 | */ |
||
202 | 6 | public function input($name, $text = null) { |
|
209 | |||
210 | |||
211 | /** |
||
212 | * @param string $name |
||
213 | * @param string|null $text |
||
214 | * @return \Fiv\Form\Element\Input |
||
215 | */ |
||
216 | public function password($name, $text = null) { |
||
223 | |||
224 | |||
225 | /** |
||
226 | * @param $name |
||
227 | * @param null $text |
||
228 | * @return Select |
||
229 | */ |
||
230 | public function select($name, $text = null) { |
||
237 | |||
238 | |||
239 | /** |
||
240 | * @param $name |
||
241 | * @param string $text |
||
242 | * @return RadioList |
||
243 | */ |
||
244 | public function radioList($name, $text = null) { |
||
251 | |||
252 | |||
253 | /** |
||
254 | * @param $name |
||
255 | * @param null $text |
||
256 | * @return TextArea |
||
257 | */ |
||
258 | 3 | public function textarea($name, $text = null) { |
|
265 | |||
266 | |||
267 | /** |
||
268 | * ``` |
||
269 | * $form->hidden('key', md5($this-user->id . HASH_A); |
||
270 | * ``` |
||
271 | * @param $name |
||
272 | * @param null $value |
||
273 | * @return \Fiv\Form\Element\Input |
||
274 | */ |
||
275 | 2 | public function hidden($name, $value = null) { |
|
283 | |||
284 | |||
285 | /** |
||
286 | * ``` |
||
287 | * $form->submit('register', 'зареєструватись'); |
||
288 | * ``` |
||
289 | * @param $name |
||
290 | * @param null $value |
||
291 | * @return Submit |
||
292 | */ |
||
293 | 1 | public function submit($name, $value = null) { |
|
300 | |||
301 | |||
302 | /** |
||
303 | * ``` |
||
304 | * $form->checkbox('subscribe', 'Підписка на новини'); |
||
305 | * ``` |
||
306 | * @param string $name |
||
307 | * @param string|null $label |
||
308 | * @return Checkbox |
||
309 | */ |
||
310 | public function checkbox($name, $label = null) { |
||
317 | |||
318 | |||
319 | /** |
||
320 | * @param string $name |
||
321 | * @param null $text |
||
322 | * @return CheckboxList |
||
323 | */ |
||
324 | public function checkboxList($name, $text = null) { |
||
331 | |||
332 | |||
333 | /** |
||
334 | * Connect element to block and to form |
||
335 | * |
||
336 | * @param Element\Base $element |
||
337 | * @return $this |
||
338 | */ |
||
339 | 12 | protected function setElement(\Fiv\Form\Element\Base $element) { |
|
344 | |||
345 | |||
346 | /** |
||
347 | * Render full form |
||
348 | * |
||
349 | * @return string |
||
350 | */ |
||
351 | 1 | public function render() { |
|
354 | |||
355 | |||
356 | /** |
||
357 | * You can easy rewrite this method for custom design of your forms |
||
358 | * |
||
359 | * @return string |
||
360 | */ |
||
361 | 1 | protected function renderElements() { |
|
373 | |||
374 | |||
375 | /** |
||
376 | * @return string |
||
377 | */ |
||
378 | 1 | public function renderStart() { |
|
393 | |||
394 | |||
395 | /** |
||
396 | * @return string |
||
397 | */ |
||
398 | 1 | public function renderEnd() { |
|
401 | |||
402 | } |
This check looks for type mismatches where the missing type is
false
. This is usually indicative of an error condtion.Consider the follow example
This function either returns a new
DateTime
object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returnedfalse
before passing on the value to another function or method that may not be able to handle afalse
.