Total Complexity | 99 |
Total Lines | 671 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like XoopsForm 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.
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 XoopsForm, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class XoopsForm |
||
32 | { |
||
33 | /** |
||
34 | * *#@+ |
||
35 | * |
||
36 | * @access private |
||
37 | */ |
||
38 | /** |
||
39 | * "action" attribute for the html form |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | public $_action; |
||
44 | |||
45 | /** |
||
46 | * "method" attribute for the form. |
||
47 | * |
||
48 | * @var string |
||
49 | */ |
||
50 | public $_method; |
||
51 | |||
52 | /** |
||
53 | * "name" attribute of the form |
||
54 | * |
||
55 | * @var string |
||
56 | */ |
||
57 | public $_name; |
||
58 | |||
59 | /** |
||
60 | * title for the form |
||
61 | * |
||
62 | * @var string |
||
63 | */ |
||
64 | public $_title; |
||
65 | |||
66 | /** |
||
67 | * summary for the form (WGAC2 Requirement) |
||
68 | * |
||
69 | * @var string |
||
70 | */ |
||
71 | public $_summary = ''; |
||
72 | |||
73 | /** |
||
74 | * array of {@link XoopsFormElement} objects |
||
75 | * |
||
76 | * @var array |
||
77 | */ |
||
78 | public $_elements = array(); |
||
79 | |||
80 | /** |
||
81 | * HTML classes for the <form> tag |
||
82 | * |
||
83 | * @var array |
||
84 | */ |
||
85 | public $_class = array(); |
||
86 | |||
87 | /** |
||
88 | * extra information for the <form> tag |
||
89 | * |
||
90 | * @var array |
||
91 | */ |
||
92 | public $_extra = array(); |
||
93 | |||
94 | /** |
||
95 | * required elements |
||
96 | * |
||
97 | * @var array |
||
98 | */ |
||
99 | public $_required = array(); |
||
100 | |||
101 | /** |
||
102 | * additional serialised object checksum (ERM Analysis - Requirement) |
||
103 | * @deprecated |
||
104 | * @access private |
||
105 | */ |
||
106 | public $_objid = 'da39a3ee5e6b4b0d3255bfef95601890afd80709'; |
||
107 | |||
108 | /** |
||
109 | * *#@- |
||
110 | */ |
||
111 | |||
112 | /** |
||
113 | * constructor |
||
114 | * |
||
115 | * @param string $title title of the form |
||
116 | * @param string $name "name" attribute for the <form> tag |
||
117 | * @param string $action "action" attribute for the <form> tag |
||
118 | * @param string $method "method" attribute for the <form> tag |
||
119 | * @param bool $addtoken whether to add a security token to the form |
||
120 | * @param string $summary |
||
121 | */ |
||
122 | public function __construct($title, $name, $action, $method = 'post', $addtoken = false, $summary = '') |
||
131 | } |
||
132 | } |
||
133 | /** |
||
134 | * PHP 4 style constructor compatibility shim |
||
135 | * @deprecated all callers should be using parent::__construct() |
||
136 | */ |
||
137 | public function XoopsForm() |
||
142 | } |
||
143 | /** |
||
144 | * *#@+ |
||
145 | * retrieves object serialisation/identification id (sha1 used) |
||
146 | * |
||
147 | * each object has serialisation<br> |
||
148 | * - legal requirement of enterprise relational management (ERM) |
||
149 | * |
||
150 | * @deprecated |
||
151 | * @access public |
||
152 | * @param $object |
||
153 | * @param string $hashinfo |
||
154 | * @return string |
||
155 | */ |
||
156 | public function getObjectID($object, $hashinfo = 'sha1') |
||
157 | { |
||
158 | if (!is_object($object)) { |
||
159 | $object = $this; |
||
160 | } |
||
161 | |||
162 | switch ($hashinfo) { |
||
163 | case 'md5': |
||
164 | |||
165 | if (!isset($var['name'])) { |
||
166 | $var['name'] = ''; |
||
167 | } |
||
168 | $var['name'] = md5(get_class($object)); |
||
169 | |||
170 | foreach (get_object_vars($object) as $key => $value) { |
||
171 | if ($key !== '_objid') { |
||
172 | if (!isset($var['value'])) { |
||
173 | $var['value'] = ''; |
||
174 | } |
||
175 | $var['value'] = $this->getArrayID($value, $key, $var['value'], $hashinfo); |
||
176 | } |
||
177 | } |
||
178 | |||
179 | foreach (get_class_methods($object) as $key => $value) { |
||
180 | if (!isset($var['func'])) { |
||
181 | $var['func'] = ''; |
||
182 | } |
||
183 | $var['func'] = $this->getArrayID($value, $key, $var['func'], $hashinfo); |
||
184 | } |
||
185 | |||
186 | if (!isset($this->_objid)) { |
||
187 | $this->_objid = ''; |
||
188 | } |
||
189 | $this->_objid = md5($var['name'] . ':' . $var['func'] . ':' . $var['value']); |
||
190 | |||
191 | return $this->_objid; |
||
192 | break; |
||
193 | |||
194 | default: |
||
195 | |||
196 | if (!isset($var['name'])) { |
||
197 | $var['name'] = ''; |
||
198 | } |
||
199 | $var['name'] = sha1(get_class($object)); |
||
200 | |||
201 | foreach (get_object_vars($object) as $key => $value) { |
||
202 | if ($key !== '_objid') { |
||
203 | if (!isset($var['value'])) { |
||
204 | $var['value'] = ''; |
||
205 | } |
||
206 | $var['value'] = $this->getArrayID($value, $key, $var['value'], $hashinfo); |
||
207 | } |
||
208 | } |
||
209 | |||
210 | foreach (get_class_methods($object) as $key => $value) { |
||
211 | if (!isset($var['func'])) { |
||
212 | $var['func'] = ''; |
||
213 | } |
||
214 | $var['func'] = $this->getArrayID($value, $key, $var['func'], $hashinfo); |
||
215 | } |
||
216 | |||
217 | if (!isset($this->_objid)) { |
||
218 | $this->_objid = ''; |
||
219 | } |
||
220 | $this->_objid = sha1($var['name'] . ':' . $var['func'] . ':' . $var['value']); |
||
221 | |||
222 | return $this->_objid; |
||
223 | |||
224 | } |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * @param $value |
||
229 | * @param $key |
||
230 | * @param $ret |
||
231 | * @param string $hashinfo |
||
232 | * |
||
233 | * @return string |
||
234 | */ |
||
235 | public function getArrayID($value, $key, $ret, $hashinfo = 'sha1') |
||
236 | { |
||
237 | switch ($hashinfo) { |
||
238 | case 'md5': |
||
239 | if (!isset($ret)) { |
||
240 | $ret = ''; |
||
241 | } |
||
242 | if (is_array($value)) { |
||
243 | foreach ($value as $keyb => $valueb) { |
||
244 | $ret = md5($ret . ':' . $this->getArrayID($valueb, $keyb, $ret, $hashinfo)); |
||
245 | } |
||
246 | } else { |
||
247 | $ret = md5($ret . ':' . $key . ':' . $value); |
||
248 | } |
||
249 | |||
250 | return $ret; |
||
251 | break; |
||
252 | default: |
||
253 | if (!isset($ret)) { |
||
254 | $ret = ''; |
||
255 | } |
||
256 | if (is_array($value)) { |
||
257 | foreach ($value as $keyb => $valueb) { |
||
258 | $ret = sha1($ret . ':' . $this->getArrayID($valueb, $keyb, $ret, $hashinfo)); |
||
259 | } |
||
260 | } else { |
||
261 | $ret = sha1($ret . ':' . $key . ':' . $value); |
||
262 | } |
||
263 | |||
264 | return $ret; |
||
265 | break; |
||
266 | } |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * return the summary of the form |
||
271 | * |
||
272 | * @param bool $encode To sanitizer the text? |
||
273 | * @return string |
||
274 | */ |
||
275 | public function getSummary($encode = false) |
||
276 | { |
||
277 | return $encode ? htmlspecialchars($this->_summary, ENT_QUOTES) : $this->_summary; |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | * return the title of the form |
||
282 | * |
||
283 | * @param bool $encode To sanitizer the text? |
||
284 | * @return string |
||
285 | */ |
||
286 | public function getTitle($encode = false) |
||
287 | { |
||
288 | return $encode ? htmlspecialchars($this->_title, ENT_QUOTES) : $this->_title; |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * get the "name" attribute for the <form> tag |
||
293 | * |
||
294 | * Deprecated, to be refactored |
||
295 | * |
||
296 | * @param bool $encode To sanitizer the text? |
||
297 | * @return string |
||
298 | */ |
||
299 | public function getName($encode = true) |
||
300 | { |
||
301 | return $encode ? htmlspecialchars($this->_name, ENT_QUOTES) : $this->_name; |
||
302 | } |
||
303 | |||
304 | /** |
||
305 | * get the "action" attribute for the <form> tag |
||
306 | * |
||
307 | * @param bool $encode To sanitizer the text? |
||
308 | * @return string |
||
309 | */ |
||
310 | public function getAction($encode = true) |
||
311 | { |
||
312 | // Convert & to & for backward compatibility |
||
313 | return $encode ? htmlspecialchars(str_replace('&', '&', $this->_action), ENT_QUOTES) : $this->_action; |
||
314 | } |
||
315 | |||
316 | /** |
||
317 | * get the "method" attribute for the <form> tag |
||
318 | * |
||
319 | * @return string |
||
320 | */ |
||
321 | public function getMethod() |
||
322 | { |
||
323 | return (strtolower($this->_method) === 'get') ? 'get' : 'post'; |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * Add an element to the form |
||
328 | * |
||
329 | * @param string|XoopsFormElement $formElement reference to a {@link XoopsFormElement} |
||
330 | * @param bool $required is this a "required" element? |
||
331 | * |
||
332 | */ |
||
333 | public function addElement($formElement, $required = false) |
||
334 | { |
||
335 | if (is_string($formElement)) { |
||
336 | $this->_elements[] = $formElement; |
||
337 | } elseif (is_subclass_of($formElement, 'XoopsFormElement')) { |
||
338 | $this->_elements[] = &$formElement; |
||
339 | if (!$formElement->isContainer()) { |
||
340 | if ($required) { |
||
341 | $formElement->_required = true; |
||
342 | $this->_required[] = &$formElement; |
||
343 | } |
||
344 | } else { |
||
345 | $required_elements = &$formElement->getRequired(); |
||
346 | $count = count($required_elements); |
||
347 | for ($i = 0; $i < $count; ++$i) { |
||
348 | $this->_required[] = &$required_elements[$i]; |
||
349 | } |
||
350 | } |
||
351 | } |
||
352 | } |
||
353 | |||
354 | /** |
||
355 | * get an array of forms elements |
||
356 | * |
||
357 | * @param bool $recurse get elements recursively? |
||
358 | * |
||
359 | * @return XoopsFormElement[] array of {@link XoopsFormElement}s |
||
360 | */ |
||
361 | public function &getElements($recurse = false) |
||
362 | { |
||
363 | if (!$recurse) { |
||
364 | return $this->_elements; |
||
365 | } else { |
||
366 | $ret = array(); |
||
367 | $count = count($this->_elements); |
||
368 | for ($i = 0; $i < $count; ++$i) { |
||
369 | if (is_object($this->_elements[$i])) { |
||
370 | if (!$this->_elements[$i]->isContainer()) { |
||
371 | $ret[] = &$this->_elements[$i]; |
||
372 | } else { |
||
373 | $elements = &$this->_elements[$i]->getElements(true); |
||
374 | $count2 = count($elements); |
||
375 | for ($j = 0; $j < $count2; ++$j) { |
||
376 | $ret[] = &$elements[$j]; |
||
377 | } |
||
378 | unset($elements); |
||
379 | } |
||
380 | } |
||
381 | } |
||
382 | |||
383 | return $ret; |
||
384 | } |
||
385 | } |
||
386 | |||
387 | /** |
||
388 | * get an array of "name" attributes of form elements |
||
389 | * |
||
390 | * @return array array of form element names |
||
391 | */ |
||
392 | public function getElementNames() |
||
393 | { |
||
394 | $ret = array(); |
||
395 | $elements = &$this->getElements(true); |
||
396 | $count = count($elements); |
||
397 | for ($i = 0; $i < $count; ++$i) { |
||
398 | $ret[] = $elements[$i]->getName(); |
||
399 | } |
||
400 | |||
401 | return $ret; |
||
402 | } |
||
403 | |||
404 | /** |
||
405 | * get a reference to a {@link XoopsFormElement} object by its "name" |
||
406 | * |
||
407 | * @param string $name "name" attribute assigned to a {@link XoopsFormElement} |
||
408 | * @return object reference to a {@link XoopsFormElement}, false if not found |
||
409 | */ |
||
410 | public function &getElementByName($name) |
||
411 | { |
||
412 | $elements =& $this->getElements(true); |
||
413 | $count = count($elements); |
||
414 | for ($i = 0; $i < $count; ++$i) { |
||
415 | if ($name == $elements[$i]->getName(false)) { |
||
416 | return $elements[$i]; |
||
417 | } |
||
418 | } |
||
419 | $elt = null; |
||
420 | |||
421 | return $elt; |
||
422 | } |
||
423 | |||
424 | /** |
||
425 | * Sets the "value" attribute of a form element |
||
426 | * |
||
427 | * @param string $name the "name" attribute of a form element |
||
428 | * @param string $value the "value" attribute of a form element |
||
429 | */ |
||
430 | public function setElementValue($name, $value) |
||
431 | { |
||
432 | $ele = &$this->getElementByName($name); |
||
433 | if (is_object($ele) && method_exists($ele, 'setValue')) { |
||
434 | $ele->setValue($value); |
||
435 | } |
||
436 | } |
||
437 | |||
438 | /** |
||
439 | * Sets the "value" attribute of form elements in a batch |
||
440 | * |
||
441 | * @param array $values array of name/value pairs to be assigned to form elements |
||
442 | */ |
||
443 | public function setElementValues($values) |
||
444 | { |
||
445 | if (is_array($values) && !empty($values)) { |
||
446 | // will not use getElementByName() for performance.. |
||
447 | $elements = &$this->getElements(true); |
||
448 | $count = count($elements); |
||
449 | for ($i = 0; $i < $count; ++$i) { |
||
450 | $name = $elements[$i]->getName(false); |
||
451 | if ($name && isset($values[$name]) && method_exists($elements[$i], 'setValue')) { |
||
452 | $elements[$i]->setValue($values[$name]); |
||
453 | } |
||
454 | } |
||
455 | } |
||
456 | } |
||
457 | |||
458 | /** |
||
459 | * Gets the "value" attribute of a form element |
||
460 | * |
||
461 | * @param string $name the "name" attribute of a form element |
||
462 | * @param bool $encode To sanitizer the text? |
||
463 | * @return string the "value" attribute assigned to a form element, null if not set |
||
464 | */ |
||
465 | public function getElementValue($name, $encode = false) |
||
473 | } |
||
474 | |||
475 | /** |
||
476 | * gets the "value" attribute of all form elements |
||
477 | * |
||
478 | * @param bool $encode To sanitizer the text? |
||
479 | * @return array array of name/value pairs assigned to form elements |
||
480 | */ |
||
481 | public function getElementValues($encode = false) |
||
482 | { |
||
483 | // will not use getElementByName() for performance.. |
||
484 | $elements = &$this->getElements(true); |
||
485 | $count = count($elements); |
||
486 | $values = array(); |
||
487 | for ($i = 0; $i < $count; ++$i) { |
||
488 | $name = $elements[$i]->getName(false); |
||
489 | if ($name && method_exists($elements[$i], 'getValue')) { |
||
490 | $values[$name] = &$elements[$i]->getValue($encode); |
||
491 | } |
||
492 | } |
||
493 | |||
494 | return $values; |
||
495 | } |
||
496 | |||
497 | /** |
||
498 | * set the "class" attribute for the <form> tag |
||
499 | * |
||
500 | * @param string $class |
||
501 | */ |
||
502 | public function setClass($class) |
||
503 | { |
||
504 | $class = trim($class); |
||
505 | if (!empty($class)) { |
||
506 | $this->_class[] = $class; |
||
507 | } |
||
508 | } |
||
509 | |||
510 | /** |
||
511 | * set the extra attributes for the <form> tag |
||
512 | * |
||
513 | * @param string $extra extra attributes for the <form> tag |
||
514 | */ |
||
515 | public function setExtra($extra) |
||
516 | { |
||
517 | if (!empty($extra)) { |
||
518 | $this->_extra[] = $extra; |
||
519 | } |
||
520 | } |
||
521 | |||
522 | /** |
||
523 | * set the summary tag for the <form> tag |
||
524 | * |
||
525 | * @param string $summary |
||
526 | */ |
||
527 | public function setSummary($summary) |
||
528 | { |
||
529 | if (!empty($summary)) { |
||
530 | $this->summary = strip_tags($summary); |
||
531 | } |
||
532 | } |
||
533 | |||
534 | /** |
||
535 | * get the "class" attribute for the <form> tag |
||
536 | * |
||
537 | * @return string "class" attribute value |
||
538 | */ |
||
539 | public function &getClass() |
||
540 | { |
||
541 | if (empty($this->_class)) { |
||
542 | return false; |
||
543 | } |
||
544 | $classes = array(); |
||
545 | foreach ($this->_class as $class) { |
||
546 | $classes[] = htmlspecialchars($class, ENT_QUOTES); |
||
547 | } |
||
548 | |||
549 | return implode(' ', $classes); |
||
550 | } |
||
551 | |||
552 | /** |
||
553 | * get the extra attributes for the <form> tag |
||
554 | * |
||
555 | * @return string |
||
556 | */ |
||
557 | public function &getExtra() |
||
558 | { |
||
559 | $extra = empty($this->_extra) ? '' : ' ' . implode(' ', $this->_extra); |
||
560 | |||
561 | return $extra; |
||
562 | } |
||
563 | |||
564 | /** |
||
565 | * make an element "required" |
||
566 | * |
||
567 | * @param XoopsFormElement $formElement reference to a {@link XoopsFormElement} |
||
568 | */ |
||
569 | public function setRequired(XoopsFormElement $formElement) |
||
572 | } |
||
573 | |||
574 | /** |
||
575 | * get an array of "required" form elements |
||
576 | * |
||
577 | * @return array array of {@link XoopsFormElement}s |
||
578 | */ |
||
579 | public function &getRequired() |
||
580 | { |
||
581 | return $this->_required; |
||
582 | } |
||
583 | |||
584 | /** |
||
585 | * insert a break in the form |
||
586 | * |
||
587 | * This method is abstract. It must be overwritten in the child classes. |
||
588 | * |
||
589 | * @param string $extra extra information for the break |
||
590 | * @abstract |
||
591 | */ |
||
592 | public function insertBreak($extra = null) |
||
593 | { |
||
594 | } |
||
595 | |||
596 | /** |
||
597 | * returns renderered form |
||
598 | * |
||
599 | * This method is abstract. It must be overwritten in the child classes. |
||
600 | * |
||
601 | * @abstract |
||
602 | */ |
||
603 | public function render() |
||
604 | { |
||
605 | } |
||
606 | |||
607 | /** |
||
608 | * displays rendered form |
||
609 | */ |
||
610 | public function display() |
||
611 | { |
||
612 | echo $this->render(); |
||
613 | } |
||
614 | |||
615 | /** |
||
616 | * Renders the Javascript function needed for client-side for validation |
||
617 | * |
||
618 | * Form elements that have been declared "required" and not set will prevent the form from being |
||
619 | * submitted. Additionally, each element class may provide its own "renderValidationJS" method |
||
620 | * that is supposed to return custom validation code for the element. |
||
621 | * |
||
622 | * The element validation code can assume that the JS "myform" variable points to the form, and must |
||
623 | * execute <i>return false</i> if validation fails. |
||
624 | * |
||
625 | * A basic element validation method may contain something like this: |
||
626 | * <code> |
||
627 | * function renderValidationJS() { |
||
628 | * $name = $this->getName(); |
||
629 | * return "if (myform.{$name}.value != 'valid') { " . |
||
630 | * "myform.{$name}.focus(); window.alert( '$name is invalid' ); return false;" . |
||
631 | * " }"; |
||
632 | * } |
||
633 | * </code> |
||
634 | * |
||
635 | * @param boolean $withtags Include the < javascript > tags in the returned string |
||
636 | * |
||
637 | * @return string |
||
638 | */ |
||
639 | public function renderValidationJS($withtags = true) |
||
659 | } |
||
660 | |||
661 | /** |
||
662 | * assign to smarty form template instead of displaying directly |
||
663 | * |
||
664 | * @param XoopsTpl $tpl reference to a {@link Smarty} object object |
||
665 | * @see Smarty |
||
666 | */ |
||
667 | public function assign(XoopsTpl $tpl) |
||
668 | { |
||
669 | $i = -1; |
||
670 | $elements = array(); |
||
671 | if (count($this->getRequired()) > 0) { |
||
672 | $this->_elements[] = "<tr class='foot'><td colspan='2'>* = " . _REQUIRED . '</td></tr>'; |
||
673 | } |
||
674 | foreach ($this->getElements() as $ele) { |
||
675 | ++$i; |
||
676 | if (is_string($ele)) { |
||
677 | $elements[$i]['body'] = $ele; |
||
678 | continue; |
||
679 | } |
||
705 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.