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 CFPHPFormularios 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 CFPHPFormularios, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
53 | class CFPHPFormularios |
||
54 | { |
||
55 | /* |
||
56 | * Form configuration. It don't need description |
||
57 | */ |
||
58 | public $method = 'post'; // post / get (CAUTION!!: Case sensitive) |
||
59 | public $action = ''; |
||
60 | public $name = ''; |
||
61 | public $on_submit = ''; |
||
62 | public $id = ''; |
||
63 | public $class = ''; |
||
64 | |||
65 | /** |
||
66 | * Logging control |
||
67 | * @logErrors boolean If is true, $errors will store errors |
||
68 | * @errors string Contain al errors |
||
69 | */ |
||
70 | public $logErrors = true; |
||
71 | public $errors = ''; |
||
72 | |||
73 | /* |
||
74 | * Other configurations about form |
||
75 | */ |
||
76 | |||
77 | /** |
||
78 | * @show_labels boolean |
||
79 | * @tutorial If is true, it show label text of some inputs before input tag |
||
80 | */ |
||
81 | public $show_labels = true; |
||
82 | |||
83 | /** IN DEVELOPMENT |
||
84 | * @self_show boolean |
||
85 | * @tutorial if is true, it show the input when function addX is called |
||
86 | */ |
||
87 | //public $self_show = false; |
||
88 | |||
89 | |||
90 | /** IN DEVELOPMENT |
||
91 | * @only_show boolean |
||
92 | * @tutorial If is true, it don't save the configuration to input (so showForm() and validateData() don't work) |
||
93 | */ |
||
94 | //public $only_show = false; |
||
95 | |||
96 | |||
97 | /* |
||
98 | * Content |
||
99 | * It can be set manually or automatically trhough functions |
||
100 | */ |
||
101 | public $content = array(); |
||
102 | |||
103 | /* |
||
104 | * Start functions |
||
105 | */ |
||
106 | |||
107 | /** |
||
108 | * @name showArray |
||
109 | * @tutorial This function show array result, it can be copied and you can replace all generator functions to this array |
||
110 | * @example $fg->showArray(); |
||
111 | */ |
||
112 | public function showArray($array = false) { |
||
115 | |||
116 | |||
117 | /** |
||
118 | * @name showForm |
||
119 | * @tutorial This function generate the form |
||
120 | * @example $fg->showForm(); |
||
121 | */ |
||
122 | public function showForm() { |
||
170 | |||
171 | |||
172 | /* |
||
173 | * The following "showX" functions are autoloaded and show the different parts of form |
||
174 | */ |
||
175 | |||
176 | /** |
||
177 | * @name showInput |
||
178 | * @tutorial This function show <input> tag with parameters specifics |
||
179 | * @param array $data All data to show in the input |
||
180 | * @return string |
||
181 | */ |
||
182 | private function showInput($data) { |
||
205 | |||
206 | private function showRadio($data, $values, $selected) { |
||
239 | |||
240 | /** |
||
241 | * @name showSelect |
||
242 | * @tutorial This function show <select> tag with all values |
||
243 | * @param array $data All data to show in the input |
||
244 | * @param array $values Values to show in select |
||
245 | * @param string $selected Specify selected option |
||
246 | * @return string |
||
247 | */ |
||
248 | private function showSelect($data, $values, $selected = null) { |
||
277 | |||
278 | /** |
||
279 | * @name showTextArea |
||
280 | * @tutorial This function show <textarea> tag with all values |
||
281 | * @param array $data All data to show in the input |
||
282 | * @return string |
||
283 | */ |
||
284 | private function showTextArea($text, $data) { |
||
300 | |||
301 | /** |
||
302 | * @name showStartForm This function return the start part of the form |
||
303 | * @return string |
||
304 | */ |
||
305 | private function showStartForm() { |
||
316 | |||
317 | /** |
||
318 | * @name showEndForm This show the end of the form |
||
319 | * @return string |
||
320 | */ |
||
321 | private function showEndForm() { |
||
324 | |||
325 | |||
326 | /* |
||
327 | * VALIDATORS |
||
328 | */ |
||
329 | /** |
||
330 | * @name validate |
||
331 | * @tutorial this function validate items sends trhough form. It DON'T FILTER |
||
332 | * Basically simulate HTML5 trhoguh PHP for full compatibility |
||
333 | * @param boolean $error_list If is true, it generate in $this->error_list a list with data required |
||
334 | */ |
||
335 | public function validate() { |
||
376 | |||
377 | /** |
||
378 | * @name validateInput |
||
379 | * @tutorial This function test if an input (text, password, checkbox, ...) is valid depending assigned values |
||
380 | * If you need other that is not invented, you can use "pattern" for example |
||
381 | * It checks: |
||
382 | * - required |
||
383 | * - date |
||
384 | * - min |
||
385 | * - max |
||
386 | * - number |
||
387 | * @param array $data Contains all information about input |
||
388 | * @return boolean If return true, its okay |
||
389 | */ |
||
390 | private function validateInput($data) { |
||
478 | |||
479 | /** |
||
480 | * @name validateArray |
||
481 | * This functions validate an Array, is a complement to validateRadio, select... |
||
482 | * @param array/string $values List of values to validate |
||
483 | * @param array/string $value/s selecteds by user |
||
484 | */ |
||
485 | private function validateArray($values, $value, $data = array()) { |
||
506 | |||
507 | |||
508 | /** |
||
509 | * @name validateRadio |
||
510 | * @tutorial This function test if an radio is valid depending assigned values |
||
511 | * @param array $data Contains all information about input |
||
512 | * @return boolean If return true, its okay |
||
513 | */ |
||
514 | private function validateRadio($params) { |
||
539 | |||
540 | /** |
||
541 | * @name validateSelect |
||
542 | * @tutorial This function test if an select is valid depending assigned values |
||
543 | * @param array $data Contains all information about input |
||
544 | * @return boolean If return true, its okay |
||
545 | */ |
||
546 | private function validateSelect($param) { |
||
563 | |||
564 | |||
565 | /** |
||
566 | * @name getData This function get value from POST/GET trhough ID ($key) |
||
567 | * If you use other system to load GET and POST, you have to edit this |
||
568 | * @param string $key Object Index |
||
569 | * @param string $default Default item if not data |
||
570 | * @return string/null Return the value, if don't exist, return null |
||
571 | */ |
||
572 | private function getData($key, $default = null) { |
||
576 | |||
577 | /* |
||
578 | * Array constructors |
||
579 | * This functions will be called to self construct array easy |
||
580 | */ |
||
581 | |||
582 | /** |
||
583 | * @name separator |
||
584 | * @tutorial This function add separation (<hr/>) between parts |
||
585 | */ |
||
586 | public function addSeparator() { |
||
589 | |||
590 | /** |
||
591 | * @name addInput |
||
592 | * @tutorial This class generate a generic INPUT |
||
593 | * @param string $label If is not empty, it show a text "label" with the text |
||
594 | * @param string $type Specify input type. It will be validated once use validate(); |
||
595 | * Types that are current supported to validate: |
||
596 | * text, password, hidden, file |
||
597 | * Its supported use ALL type supported (http://www.w3schools.com/tags/att_input_type.asp), |
||
598 | * Some type input you can declare directly via functions (afther this function are more) |
||
599 | * @param string $name Element name |
||
600 | * @param string $value Element value |
||
601 | * @param string $required Element required? |
||
602 | * @param array $attributes Optional atributes not listened (in array). |
||
603 | * EX: You like use "date", then you can put array('min' => '1979-12-31', 'max' => '2000-01-02') |
||
604 | * All parameters currently working: min, max (date and number), pattern, |
||
605 | * |
||
606 | * @example $fr->addInput('text', 'Username', '', true, 'Type here your username', array('class'=>'text red', id => 'text1'), array()) |
||
607 | */ |
||
608 | public function addInput($label = '', $type = 'text', $name = '', $value = '', $required = false, $placeholder = '', $attributes = array()) { |
||
631 | |||
632 | /** |
||
633 | * @name addNumber This function add input "number" and "date" with min, max and step (if you like) |
||
634 | * @param string $label If is not empty, it show a text "label" with the text |
||
635 | * @param string $name Element name |
||
636 | * @param string $value Default value |
||
637 | * @param boolen $range If is true, then show "range" instead of text with number |
||
638 | * @param boolen $required Is required? |
||
639 | * @param number $min Minium value. Empty nothing |
||
640 | * @param number $max Max value. Empty nothing |
||
641 | * @param number $step Step (multiply). Empty nothing (ex: step 2: -2, 0, 2, 4 ...) |
||
642 | * @param string $placeholder Default text to show if box is empty |
||
643 | * @param unknown $attributes Additional attributes |
||
644 | */ |
||
645 | View Code Duplication | public function addNumber($label = '', $name = '', $value = '', $range = false, $required = false, $min = '', $max = '', $step = '', $placeholder = '', $attributes = array()) { |
|
670 | |||
671 | /** |
||
672 | * @name addDate This function add input "number" with min, max and step (if you like) |
||
673 | * @param string $label If is not empty, it show a text "label" with the text |
||
674 | * @param string $name Element name |
||
675 | * @param string $value Default value |
||
676 | * @param string $required Is required? |
||
677 | * @param string $min Minium value. Empty nothing |
||
678 | * @param string $max Max value. Empty nothing |
||
679 | * @param string $step Step (multiply). Empty nothing (ex: step 2: -2, 0, 2, 4 ...) |
||
680 | * @param string $placeholder Default text to show if box is empty |
||
681 | * @param unknown $attributes Additional attributes |
||
682 | */ |
||
683 | View Code Duplication | public function addDate($label = '', $name = '', $value = '', $required = false, $min = '', $max = '', $step = '', $placeholder = '', $attributes = array()) { |
|
707 | |||
708 | |||
709 | /** |
||
710 | * @name addCheck |
||
711 | * @tutorial Use this function to add a checkBox |
||
712 | * @param string $label If is not empty, it show a text "label" with the text |
||
713 | * @param string $name Check name |
||
714 | * @param string $value Check value |
||
715 | * @param boolean $required Specify if check is required |
||
716 | * @param array $attributes Optional atributes not listened (in array). |
||
717 | */ |
||
718 | View Code Duplication | public function addCheck($label = '', $name = '', $value = '', $required = false, $attributes = array()) { |
|
735 | |||
736 | |||
737 | /** |
||
738 | * @name addRadio |
||
739 | * @tutorial Use this function to add a radio button |
||
740 | * @param string $label If is not empty, it show a text "label" with the text |
||
741 | * @param string $name Radio button name |
||
742 | * @param array $values Contain all radio with values and text to show |
||
743 | * EX: array('val1'=>'text1', 'val2'=>'text2'); |
||
744 | * @param string $selected Specify wich option (ID) will be checked by default |
||
745 | * @param boolean $required Specify if radio is required |
||
746 | * @param array $attributes Optional atributes not listened (in array). This will be applicated to all radioButton |
||
747 | */ |
||
748 | public function addRadio($label = '', $name = '', $values = array(), $selected = null, $required = false, $attributes = array()) { |
||
769 | |||
770 | |||
771 | /** |
||
772 | * @name addSelect |
||
773 | * @tutorial Use this function to add a radio button |
||
774 | * @param string $label Text to show before input |
||
775 | * @param string $name Select name |
||
776 | * @param array $values Array('value'=>'text to show') |
||
777 | * @param boolean $required Specify if select is required |
||
778 | * @param string $id ID Specify ID to manipulate via javascript or CSS |
||
779 | * @param array $attributes Optional atributes not listened (in array). |
||
780 | */ |
||
781 | public function addSelect($label = '', $name = '', $values = '', $selected = null, $required = false, $multiple = false, $attributes = array()) { |
||
802 | |||
803 | /** |
||
804 | * @name addSubmit |
||
805 | * @tutorial Use this function to add a submit button |
||
806 | * @param string $name Select name (optional, if leav blank it will not send in POST / GET) |
||
807 | * @param string $value Text to show in button and ID |
||
808 | * @param array $attributes Optional atributes not listened (in array). |
||
809 | */ |
||
810 | View Code Duplication | public function addSubmit($name = '', $value = '', $attributes = array()) { |
|
825 | |||
826 | /** |
||
827 | * @name addButton |
||
828 | * @tutorial Use this function to add a button with "onclick" action (or id/class to call via jquery) |
||
829 | * @param string $value Name to show in button and ID |
||
830 | * @param string $onClick Action to load when button is clicked |
||
831 | * @param array $attributes Optional atributes not listened (in array). |
||
832 | */ |
||
833 | View Code Duplication | public function addButton($value = '', $onClick = '', $attributes = array()) { |
|
846 | |||
847 | |||
848 | /** |
||
849 | * @name addButton |
||
850 | * @tutorial Use this function to add a button with "onclick" action (or id/class to call via jquery) |
||
851 | * @param string $value Name to show in button and ID |
||
852 | * @param string $onClick Action to load when button is clicked |
||
853 | * @param array $attributes Optional atributes not listened (in array). |
||
854 | */ |
||
855 | View Code Duplication | public function addTextArea($label = '', $name = '', $value = '', $required = false, $attributes = array()) { |
|
874 | |||
875 | /* |
||
876 | * OTHER |
||
877 | */ |
||
878 | /** |
||
879 | * @name log |
||
880 | * @tutorial Save a log if is enabled logging |
||
881 | * @param boolean $save If is true, save the log |
||
882 | * @param string $message Message to log |
||
883 | */ |
||
884 | private function log($message, $data = array()) { |
||
904 | } |
||
905 | |||
936 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.