Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like FormValidator 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 FormValidator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class FormValidator extends HTML_QuickForm |
||
9 | { |
||
10 | const LAYOUT_HORIZONTAL = 'horizontal'; |
||
11 | const LAYOUT_INLINE = 'inline'; |
||
12 | const LAYOUT_BOX = 'box'; |
||
13 | const LAYOUT_BOX_NO_LABEL = 'box-no-label'; |
||
14 | |||
15 | public $with_progress_bar = false; |
||
16 | private $layout; |
||
17 | |||
18 | /** |
||
19 | * Constructor |
||
20 | * @param string $name Name of the form |
||
21 | * @param string $method (optional Method ('post' (default) or 'get') |
||
22 | * @param string $action (optional Action (default is $PHP_SELF) |
||
23 | * @param string $target (optional Form's target defaults to '_self' |
||
24 | * @param mixed $attributes (optional) Extra attributes for <form> tag |
||
25 | * @param string $layout |
||
26 | * @param bool $trackSubmit (optional) Whether to track if the form was |
||
27 | * submitted by adding a special hidden field (default = true) |
||
28 | */ |
||
29 | public function __construct( |
||
110 | |||
111 | /** |
||
112 | * @return string |
||
113 | */ |
||
114 | public function getFormTemplate() |
||
123 | |||
124 | /** |
||
125 | * @return string |
||
126 | */ |
||
127 | public function getDefaultElementTemplate() |
||
154 | |||
155 | /** |
||
156 | * @return string |
||
157 | */ |
||
158 | public function getLayout() |
||
162 | |||
163 | /** |
||
164 | * @param string $layout |
||
165 | */ |
||
166 | public function setLayout($layout) |
||
170 | |||
171 | /** |
||
172 | * Adds a text field to the form. |
||
173 | * A trim-filter is attached to the field. |
||
174 | * @param string $label The label for the form-element |
||
175 | * @param string $name The element name |
||
176 | * @param bool $required (optional) Is the form-element required (default=true) |
||
177 | * @param array $attributes (optional) List of attributes for the form-element |
||
178 | */ |
||
179 | public function addText($name, $label, $required = true, $attributes = array()) |
||
187 | |||
188 | /** |
||
189 | * The "date_range_picker" element creates 2 hidden fields |
||
190 | * "elementName" + "_start" and "elementName" + "_end" |
||
191 | * For example if the name is "range", you will have 2 new fields |
||
192 | * when executing $form->getSubmitValues() |
||
193 | * "range_start" and "range_end" |
||
194 | * |
||
195 | * @param string $name |
||
196 | * @param string $label |
||
197 | * @param bool $required |
||
198 | * @param array $attributes |
||
199 | */ |
||
200 | View Code Duplication | public function addDateRangePicker($name, $label, $required = true, $attributes = array()) |
|
210 | |||
211 | /** |
||
212 | * @param string $name |
||
213 | * @param string $label |
||
214 | * @param array $attributes |
||
215 | * |
||
216 | * @return mixed |
||
217 | */ |
||
218 | public function addDatePicker($name, $label, $attributes = []) |
||
222 | |||
223 | /** |
||
224 | * @param string $name |
||
225 | * @param string $label |
||
226 | * @param array $attributes |
||
227 | * |
||
228 | * @return mixed |
||
229 | */ |
||
230 | public function addSelectLanguage($name, $label, $options = [], $attributes = []) |
||
234 | |||
235 | /** |
||
236 | * @param $name |
||
237 | * @param $label |
||
238 | * @param array $options |
||
239 | * @param array $attributes |
||
240 | * @throws |
||
241 | */ |
||
242 | public function addSelectAjax($name, $label, $options = [], $attributes = []) |
||
255 | |||
256 | /** |
||
257 | * @param string $name |
||
258 | * @param string $label |
||
259 | * @param array $attributes |
||
260 | * |
||
261 | * @return mixed |
||
262 | */ |
||
263 | public function addDateTimePicker($name, $label, $attributes = []) |
||
267 | |||
268 | /** |
||
269 | * @param string $name |
||
270 | * @param string $value |
||
271 | */ |
||
272 | public function addHidden($name, $value) |
||
276 | |||
277 | /** |
||
278 | * @param string $name |
||
279 | * @param string $label |
||
280 | * @param array $attributes |
||
281 | * |
||
282 | * @return HTML_QuickForm_textarea |
||
283 | */ |
||
284 | public function addTextarea($name, $label, $attributes = array()) |
||
288 | |||
289 | /** |
||
290 | * @param string $name |
||
291 | * @param string $label |
||
292 | * @param string $icon font-awesome |
||
293 | * @param string $style default|primary|success|info|warning|danger|link |
||
294 | * @param string $size large|default|small|extra-small |
||
295 | * @param string $class Example plus is transformed to icon fa fa-plus |
||
296 | * @param array $attributes |
||
297 | * |
||
298 | * @return HTML_QuickForm_button |
||
299 | */ |
||
300 | public function addButton( |
||
334 | |||
335 | /** |
||
336 | * Returns a button with the primary color and a check mark |
||
337 | * @param string $label Text appearing on the button |
||
338 | * @param string $name Element name (for form treatment purposes) |
||
339 | * @param bool $createElement Whether to use the create or add method |
||
340 | * |
||
341 | * @return HTML_QuickForm_button |
||
342 | */ |
||
343 | public function addButtonSave($label, $name = 'submit', $createElement = false) |
||
356 | |||
357 | /** |
||
358 | * Returns a cancel button |
||
359 | * @param string $label Text appearing on the button |
||
360 | * @param string $name Element name (for form treatment purposes) |
||
361 | * @param bool $createElement Whether to use the create or add method |
||
362 | * |
||
363 | * @return HTML_QuickForm_button |
||
364 | */ |
||
365 | public function addButtonCancel($label, $name = 'submit', $createElement = false) |
||
378 | |||
379 | /** |
||
380 | * Returns a button with the primary color and a "plus" icon |
||
381 | * @param string $label Text appearing on the button |
||
382 | * @param string $name Element name (for form treatment purposes) |
||
383 | * @param bool $createElement Whether to use the create or add method |
||
384 | * @param array $attributes Additional attributes |
||
385 | * |
||
386 | * @return HTML_QuickForm_button |
||
387 | */ |
||
388 | public function addButtonCreate($label, $name = 'submit', $createElement = false, $attributes = array()) |
||
401 | |||
402 | /** |
||
403 | * Returns a button with the primary color and a pencil icon |
||
404 | * @param string $label Text appearing on the button |
||
405 | * @param string $name Element name (for form treatment purposes) |
||
406 | * @param bool $createElement Whether to use the create or add method |
||
407 | * @return HTML_QuickForm_button |
||
408 | */ |
||
409 | public function addButtonUpdate($label, $name = 'submit', $createElement = false) |
||
422 | |||
423 | /** |
||
424 | * Returns a button with the danger color and a trash icon |
||
425 | * @param string $label Text appearing on the button |
||
426 | * @param string $name Element name (for form treatment purposes) |
||
427 | * @param bool $createElement Whether to use the create or add method |
||
428 | * |
||
429 | * @return HTML_QuickForm_button |
||
430 | */ |
||
431 | public function addButtonDelete($label, $name = 'submit', $createElement = false) |
||
444 | |||
445 | /** |
||
446 | * Returns a button with the primary color and a paper-plane icon |
||
447 | * @param string $label Text appearing on the button |
||
448 | * @param string $name Element name (for form treatment purposes) |
||
449 | * @param bool $createElement Whether to use the create or add method |
||
450 | * |
||
451 | * @return HTML_QuickForm_button |
||
452 | */ |
||
453 | public function addButtonSend($label, $name = 'submit', $createElement = false, $attributes = array()) |
||
466 | |||
467 | /** |
||
468 | * Returns a button with the default (grey?) color and a magnifier icon |
||
469 | * @param string $label Text appearing on the button |
||
470 | * @param string $name Element name (for form treatment purposes) |
||
471 | * |
||
472 | * @return HTML_QuickForm_button |
||
473 | */ |
||
474 | public function addButtonSearch($label = null, $name = 'submit') |
||
482 | |||
483 | /** |
||
484 | * Returns a button with the primary color and a right-pointing arrow icon |
||
485 | * @param string $label Text appearing on the button |
||
486 | * @param string $name Element name (for form treatment purposes) |
||
487 | * @param array $attributes Additional attributes |
||
488 | * @return HTML_QuickForm_button |
||
489 | */ |
||
490 | public function addButtonNext($label, $name = 'submit', $attributes = array()) |
||
494 | |||
495 | /** |
||
496 | * Returns a button with the primary color and a check mark icon |
||
497 | * @param string $label Text appearing on the button |
||
498 | * @param string $name Element name (for form treatment purposes) |
||
499 | * @param bool $createElement Whether to use the create or add method |
||
500 | * @return HTML_QuickForm_button |
||
501 | */ |
||
502 | public function addButtonImport($label, $name = 'submit', $createElement = false) |
||
515 | |||
516 | /** |
||
517 | * Returns a button with the primary color and a check-mark icon |
||
518 | * @param string $label Text appearing on the button |
||
519 | * @param string $name Element name (for form treatment purposes) |
||
520 | * @param bool $createElement Whether to use the create or add method |
||
521 | * @return HTML_QuickForm_button |
||
522 | */ |
||
523 | public function addButtonExport($label, $name = 'submit', $createElement = false) |
||
536 | |||
537 | /** |
||
538 | * Shortcut to filter button |
||
539 | * @param string $label Text appearing on the button |
||
540 | * @param string $name Element name (for form treatment purposes) |
||
541 | * @param bool $createElement Whether to use the create or add method |
||
542 | * @return HTML_QuickForm_button |
||
543 | */ |
||
544 | public function addButtonFilter($label, $name = 'submit', $createElement = false) |
||
557 | |||
558 | /** |
||
559 | * Shortcut to reset button |
||
560 | * @param string $label Text appearing on the button |
||
561 | * @param string $name Element name (for form treatment purposes) |
||
562 | * @param bool $createElement Whether to use the create or add method |
||
563 | * @return HTML_QuickForm_button |
||
564 | */ |
||
565 | public function addButtonReset($label, $name = 'reset', $createElement = false) |
||
597 | |||
598 | /** |
||
599 | * Returns a button with the primary color and an upload icon |
||
600 | * @param string $label Text appearing on the button |
||
601 | * @param string $name Element name (for form treatment purposes) |
||
602 | * @param bool $createElement Whether to use the create or add method |
||
603 | * |
||
604 | * @return HTML_QuickForm_button |
||
605 | */ |
||
606 | public function addButtonUpload($label, $name = 'submit', $createElement = false) |
||
619 | |||
620 | /** |
||
621 | * Returns a button with the primary color and a download icon |
||
622 | * @param string $label Text appearing on the button |
||
623 | * @param string $name Element name (for form treatment purposes) |
||
624 | * @param bool $createElement Whether to use the create or add method |
||
625 | * |
||
626 | * @return HTML_QuickForm_button |
||
627 | */ |
||
628 | public function addButtonDownload($label, $name = 'submit', $createElement = false) |
||
641 | |||
642 | /** |
||
643 | * Returns a button with the primary color and a magnifier icon |
||
644 | * @param string $label Text appearing on the button |
||
645 | * @param string $name Element name (for form treatment purposes) |
||
646 | * @param bool $createElement Whether to use the create or add method |
||
647 | * |
||
648 | * @return HTML_QuickForm_button |
||
649 | */ |
||
650 | View Code Duplication | public function addButtonPreview($label, $name = 'submit', $createElement = false) |
|
663 | |||
664 | /** |
||
665 | * Returns a button with the primary color and a copy (double sheet) icon |
||
666 | * @param string $label Text appearing on the button |
||
667 | * @param string $name Element name (for form treatment purposes) |
||
668 | * @param bool $createElement Whether to use the create or add method |
||
669 | * |
||
670 | * @return HTML_QuickForm_button |
||
671 | */ |
||
672 | View Code Duplication | public function addButtonCopy($label, $name = 'submit', $createElement = false) |
|
685 | |||
686 | /** |
||
687 | * @param string $name |
||
688 | * @param string $label |
||
689 | * @param string $text |
||
690 | * @param array $attributes |
||
691 | * |
||
692 | * @return HTML_QuickForm_checkbox |
||
693 | */ |
||
694 | public function addCheckBox($name, $label, $text = '', $attributes = array()) |
||
698 | |||
699 | /** |
||
700 | * @param string $name |
||
701 | * @param string $label |
||
702 | * @param array $options |
||
703 | * @param array $attributes |
||
704 | * |
||
705 | * @return HTML_QuickForm_group |
||
706 | */ |
||
707 | View Code Duplication | public function addCheckBoxGroup($name, $label, $options = array(), $attributes = array()) |
|
717 | |||
718 | /** |
||
719 | * @param string $name |
||
720 | * @param string $label |
||
721 | * @param array $options |
||
722 | * @param array $attributes |
||
723 | * |
||
724 | * @return HTML_QuickForm_radio |
||
725 | */ |
||
726 | View Code Duplication | public function addRadio($name, $label, $options = array(), $attributes = array()) |
|
735 | |||
736 | /** |
||
737 | * @param string $name |
||
738 | * @param string $label |
||
739 | * @param array $options |
||
740 | * @param array $attributes |
||
741 | * |
||
742 | * @return HTML_QuickForm_select |
||
743 | */ |
||
744 | public function addSelect($name, $label, $options = array(), $attributes = array()) |
||
748 | |||
749 | /** |
||
750 | * @param $name |
||
751 | * @param $label |
||
752 | * @param $collection |
||
753 | * @param array $attributes |
||
754 | * @param bool $addNoneOption |
||
755 | * @param string $textCallable set a function getStringValue() by default __toString() |
||
756 | * |
||
757 | * @return HTML_QuickForm_element |
||
758 | */ |
||
759 | public function addSelectFromCollection( |
||
784 | |||
785 | /** |
||
786 | * @param string $label |
||
787 | * @param string $text |
||
788 | * |
||
789 | * @return HTML_QuickForm_label |
||
790 | */ |
||
791 | public function addLabel($label, $text) |
||
795 | |||
796 | /** |
||
797 | * @param string $text |
||
798 | */ |
||
799 | public function addHeader($text) |
||
803 | |||
804 | /** |
||
805 | * @param string $name |
||
806 | * @param string $label |
||
807 | * @param array $attributes |
||
808 | * @throws Exception if the file doesn't have an id |
||
809 | */ |
||
810 | public function addFile($name, $label, $attributes = array()) |
||
838 | |||
839 | /** |
||
840 | * @param string $snippet |
||
841 | */ |
||
842 | public function addHtml($snippet) |
||
846 | |||
847 | /** |
||
848 | * Adds a HTML-editor to the form |
||
849 | * @param string $name |
||
850 | * @param string $label The label for the form-element |
||
851 | * @param bool $required (optional) Is the form-element required (default=true) |
||
852 | * @param bool $fullPage (optional) When it is true, the editor loads completed html code for a full page. |
||
853 | * @param array $config (optional) Configuration settings for the online editor. |
||
854 | * @param bool $style |
||
855 | */ |
||
856 | public function addHtmlEditor($name, $label, $required = true, $fullPage = false, $config = array(), $style = false) |
||
880 | |||
881 | /** |
||
882 | * Adds a Google Maps Geolocalization field to the form |
||
883 | * |
||
884 | * @param $name |
||
885 | * @param $label |
||
886 | */ |
||
887 | public function addGeoLocationMapField($name, $label) |
||
1033 | |||
1034 | /** |
||
1035 | * @param string $name |
||
1036 | * @param string $label |
||
1037 | * |
||
1038 | * @return mixed |
||
1039 | */ |
||
1040 | public function addButtonAdvancedSettings($name, $label = '') |
||
1046 | |||
1047 | /** |
||
1048 | * Adds a progress loading image to the form. |
||
1049 | * |
||
1050 | */ |
||
1051 | public function addProgress($delay = 2, $label = '') |
||
1062 | |||
1063 | /** |
||
1064 | * This function has been created for avoiding changes directly within QuickForm class. |
||
1065 | * When we use it, the element is threated as 'required' to be dealt during validation. |
||
1066 | * @param array $element The array of elements |
||
|
|||
1067 | * @param string $message The message displayed |
||
1068 | */ |
||
1069 | public function add_multiple_required_rule($elements, $message) |
||
1074 | |||
1075 | /** |
||
1076 | * Displays the form. |
||
1077 | * If an element in the form didn't validate, an error message is showed |
||
1078 | * asking the user to complete the form. |
||
1079 | */ |
||
1080 | public function display() |
||
1084 | |||
1085 | /** |
||
1086 | * Returns the HTML code of the form. |
||
1087 | * @return string $return_value HTML code of the form |
||
1088 | */ |
||
1089 | public function returnForm() |
||
1119 | |||
1120 | /** |
||
1121 | * Returns the HTML code of the form. |
||
1122 | * If an element in the form didn't validate, an error message is showed |
||
1123 | * asking the user to complete the form. |
||
1124 | * |
||
1125 | * @return string $return_value HTML code of the form |
||
1126 | * |
||
1127 | * @author Patrick Cool <[email protected]>, Ghent University, august 2006 |
||
1128 | * @author Julio Montoya |
||
1129 | * @deprecated use returnForm() |
||
1130 | */ |
||
1131 | public function return_form() |
||
1135 | |||
1136 | /** |
||
1137 | * Create a form validator based on an array of form data: |
||
1138 | * |
||
1139 | * array( |
||
1140 | * 'name' => 'zombie_report_parameters', //optional |
||
1141 | * 'method' => 'GET', //optional |
||
1142 | * 'items' => array( |
||
1143 | * array( |
||
1144 | * 'name' => 'ceiling', |
||
1145 | * 'label' => 'Ceiling', //optional |
||
1146 | * 'type' => 'date', |
||
1147 | * 'default' => date() //optional |
||
1148 | * ), |
||
1149 | * array( |
||
1150 | * 'name' => 'active_only', |
||
1151 | * 'label' => 'ActiveOnly', |
||
1152 | * 'type' => 'checkbox', |
||
1153 | * 'default' => true |
||
1154 | * ), |
||
1155 | * array( |
||
1156 | * 'name' => 'submit_button', |
||
1157 | * 'type' => 'style_submit_button', |
||
1158 | * 'value' => get_lang('Search'), |
||
1159 | * 'attributes' => array('class' => 'search') |
||
1160 | * ) |
||
1161 | * ) |
||
1162 | * ); |
||
1163 | * |
||
1164 | * @param array $form_data |
||
1165 | * @deprecated use normal FormValidator construct |
||
1166 | * |
||
1167 | * @return FormValidator |
||
1168 | */ |
||
1169 | public static function create($form_data) |
||
1220 | |||
1221 | /** |
||
1222 | * @return HTML_QuickForm_Renderer_Default |
||
1223 | */ |
||
1224 | public static function getDefaultRenderer() |
||
1230 | |||
1231 | /** |
||
1232 | * Adds a input of type url to the form. |
||
1233 | * @param type $name The label for the form-element |
||
1234 | * @param type $label The element name |
||
1235 | * @param type $required Optional. Is the form-element required (default=true) |
||
1236 | * @param type $attributes Optional. List of attributes for the form-element |
||
1237 | */ |
||
1238 | View Code Duplication | public function addUrl($name, $label, $required = true, $attributes = array()) |
|
1248 | |||
1249 | /** |
||
1250 | * Adds a text field for letters to the form. |
||
1251 | * A trim-filter is attached to the field. |
||
1252 | * @param string $name The element name |
||
1253 | * @param string $label The label for the form-element |
||
1254 | * @param bool $required Optional. Is the form-element required (default=true) |
||
1255 | * @param array $attributes Optional. List of attributes for the form-element |
||
1256 | */ |
||
1257 | View Code Duplication | public function addTextLettersOnly( |
|
1294 | |||
1295 | /** |
||
1296 | * Adds a text field for alphanumeric characters to the form. |
||
1297 | * A trim-filter is attached to the field. |
||
1298 | * @param string $name The element name |
||
1299 | * @param string $label The label for the form-element |
||
1300 | * @param bool $required Optional. Is the form-element required (default=true) |
||
1301 | * @param array $attributes Optional. List of attributes for the form-element |
||
1302 | */ |
||
1303 | View Code Duplication | public function addTextAlphanumeric( |
|
1340 | |||
1341 | /** |
||
1342 | * @param $name |
||
1343 | * @param $label |
||
1344 | * @param bool $required |
||
1345 | * @param array $attributes |
||
1346 | * @param bool $allowNegative |
||
1347 | * @param null $minValue |
||
1348 | * @param null $maxValue |
||
1349 | */ |
||
1350 | public function addFloat( |
||
1419 | |||
1420 | /** |
||
1421 | * Adds a text field for letters and spaces to the form. |
||
1422 | * A trim-filter is attached to the field. |
||
1423 | * @param string $name The element name |
||
1424 | * @param string $label The label for the form-element |
||
1425 | * @param bool $required Optional. Is the form-element required (default=true) |
||
1426 | * @param array $attributes Optional. List of attributes for the form-element |
||
1427 | */ |
||
1428 | View Code Duplication | public function addTextLettersAndSpaces( |
|
1465 | |||
1466 | /** |
||
1467 | * Adds a text field for alphanumeric and spaces characters to the form. |
||
1468 | * A trim-filter is attached to the field. |
||
1469 | * @param string $name The element name |
||
1470 | * @param string $label The label for the form-element |
||
1471 | * @param bool $required Optional. Is the form-element required (default=true) |
||
1472 | * @param array $attributes Optional. List of attributes for the form-element |
||
1473 | */ |
||
1474 | View Code Duplication | public function addTextAlphanumericAndSpaces( |
|
1511 | |||
1512 | /** |
||
1513 | * @param string $url |
||
1514 | */ |
||
1515 | public function addMultipleUpload($url) |
||
1544 | |||
1545 | /** |
||
1546 | * |
||
1547 | * @param string $url page that will handle the upload |
||
1548 | * @param string $inputName |
||
1549 | */ |
||
1550 | private function addMultipleUploadJavascript($url, $inputName) |
||
1659 | } |
||
1660 | |||
1705 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.
Consider the following example. The parameter
$ireland
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was changed, but the annotation was not.