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 HtmlForm 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 HtmlForm, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class HtmlForm extends HtmlSemCollection { |
||
21 | |||
22 | use FieldsTrait; |
||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $_fields; |
||
27 | |||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $_validationParams; |
||
32 | |||
33 | public function __construct($identifier, $elements=array()) { |
||
41 | |||
42 | /** |
||
43 | * @param string $title |
||
44 | * @param number $niveau |
||
45 | * @param string $dividing |
||
46 | * @return HtmlHeader |
||
47 | */ |
||
48 | public function addHeader($title, $niveau=1, $dividing=true) { |
||
54 | |||
55 | /** |
||
56 | * @param string $caption |
||
57 | * @return \Ajax\semantic\html\collections\form\HtmlForm |
||
58 | */ |
||
59 | public function addDivider($caption=NULL){ |
||
62 | |||
63 | public function addFields($fields=NULL, $label=NULL) { |
||
64 | if (isset($fields)) { |
||
65 | if (!$fields instanceof HtmlFormFields) { |
||
66 | View Code Duplication | if (\is_array($fields) === false) { |
|
|
|||
67 | $fields=\func_get_args(); |
||
68 | $end=\end($fields); |
||
69 | if (\is_string($end)) { |
||
70 | $label=$end; |
||
71 | \array_pop($fields); |
||
72 | } else |
||
73 | $label=NULL; |
||
74 | } |
||
75 | $this->_fields=\array_merge($this->_fields, $fields); |
||
76 | $fields=new HtmlFormFields("fields-" . $this->identifier . "-" . $this->count(), $fields); |
||
77 | } |
||
78 | if (isset($label)) |
||
79 | $fields=new HtmlFormField("", $fields, $label); |
||
80 | } else { |
||
81 | $fields=new HtmlFormFields("fields-" . $this->identifier . "-" . $this->count()); |
||
82 | } |
||
83 | $this->addItem($fields); |
||
84 | return $fields; |
||
85 | } |
||
86 | |||
87 | public function addItem($item) { |
||
94 | |||
95 | public function getField($index) { |
||
103 | |||
104 | /** |
||
105 | * automatically divide fields to be equal width |
||
106 | * @return \Ajax\semantic\html\collections\form\HtmlForm |
||
107 | */ |
||
108 | public function setEqualWidth() { |
||
111 | |||
112 | /** |
||
113 | * Adds a field (alias for addItem) |
||
114 | * @param HtmlFormField $field |
||
115 | * @return \Ajax\common\html\HtmlDoubleElement |
||
116 | */ |
||
117 | public function addField($field) { |
||
120 | |||
121 | /** |
||
122 | * |
||
123 | * @param string $identifier |
||
124 | * @param string $content |
||
125 | * @param string $header |
||
126 | * @param string $icon |
||
127 | * @param string $type |
||
128 | * @return \Ajax\semantic\html\collections\HtmlMessage |
||
129 | */ |
||
130 | public function addMessage($identifier, $content, $header=NULL, $icon=NULL, $type=NULL) { |
||
140 | |||
141 | private function addCompoValidation($js,$compo,$field){ |
||
152 | |||
153 | public function run(JsUtils $js) { |
||
184 | |||
185 | public function setLoading() { |
||
188 | |||
189 | public function addErrorMessage(){ |
||
192 | |||
193 | public function jsState($state) { |
||
196 | |||
197 | public function setValidationParams(array $_validationParams) { |
||
201 | |||
202 | public function submitOn($event,$identifier,$url,$responseElement){ |
||
210 | |||
211 | public function submitOnClick($identifier,$url,$responseElement){ |
||
214 | |||
215 | public function addSubmit($identifier,$value,$CssStyle=NULL,$url=NULL,$responseElement=NULL){ |
||
221 | |||
222 | /** |
||
223 | * Callback on each valid field |
||
224 | * @param string $jsCode |
||
225 | * @return \Ajax\semantic\html\collections\form\HtmlForm |
||
226 | */ |
||
227 | public function onValid($jsCode){ |
||
231 | |||
232 | /** |
||
233 | * Callback if a form is all valid |
||
234 | * @param string $jsCode can use event and fields parameters |
||
235 | * @return HtmlForm |
||
236 | */ |
||
237 | public function onSuccess($jsCode){ |
||
241 | |||
242 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.