| Total Complexity | 68 |
| Total Lines | 281 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
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.
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 |
||
| 24 | class Form implements interfaceForm |
||
| 25 | { |
||
| 26 | |||
| 27 | /** |
||
| 28 | * open form |
||
| 29 | * |
||
| 30 | * This method return the form element <form... |
||
| 31 | * |
||
| 32 | * @param array(id, name, class, onsubmit, method, action, files, style) |
||
| 33 | * |
||
| 34 | * @return string |
||
| 35 | */ |
||
| 36 | public static function open($params = array()) |
||
| 37 | { |
||
| 38 | |||
| 39 | if (!is_array($params)) { |
||
| 40 | throw new \InvalidArgumentException("Arguments is not a Array" . print_r($params, true)); |
||
| 41 | } |
||
| 42 | $o = '<form'; |
||
| 43 | $o .= self::param_mix($params); |
||
| 44 | $o .= (isset($params['onsubmit'])) ? " onsubmit='{$params['onsubmit']}'" : ''; |
||
| 45 | $o .= (isset($params['method'])) ? " method='{$params['method']}'" : ' method="get"'; |
||
| 46 | $o .= (isset($params['action'])) ? " action='{$params['action']}'" : ''; |
||
| 47 | $o .= (isset($params['files'])) ? " enctype='multipart/form-data'" : ''; |
||
| 48 | $o .= (isset($params['role'])) ? " role='{$params['role']}'" : ''; |
||
| 49 | $o .= (isset($params['autocomplete'])) ? " autocomplete='{$params['autocomplete']}'" : ''; |
||
| 50 | $o .= '>'; |
||
| 51 | return $o; |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * closed the form |
||
| 56 | * |
||
| 57 | * @return string |
||
| 58 | */ |
||
| 59 | public static function close() |
||
| 60 | { |
||
| 61 | return "</form>\n"; |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * textBox |
||
| 66 | * |
||
| 67 | * This method creates a textarea element |
||
| 68 | * |
||
| 69 | * @param array(id, name, class, onclick, columns, rows, disabled, placeholder, style, value) |
||
| 70 | * |
||
| 71 | * @return string |
||
| 72 | */ |
||
| 73 | public static function textBox($params = array()) |
||
| 74 | { |
||
| 75 | |||
| 76 | if (!is_array($params)) { |
||
| 77 | throw new \InvalidArgumentException("Arguments is not a Array" . print_r($params, true)); |
||
| 78 | } |
||
| 79 | $o = '<textarea'; |
||
| 80 | $o .= self::param_mix($params); |
||
| 81 | $o .= (isset($params['cols'])) ? " cols='{$params['cols']}'" : ''; |
||
| 82 | $o .= (isset($params['rows'])) ? " rows='{$params['rows']}'" : ''; |
||
| 83 | $o .= (isset($params['maxlength'])) ? " maxlength='{$params['maxlength']}'" : ''; |
||
| 84 | $o .= (isset($params['required'])) ? " required='required'" : ''; |
||
| 85 | $o .= '>'; |
||
| 86 | $o .= (isset($params['value'])) ? $params['value'] : ''; |
||
| 87 | $o .= "</textarea>\n"; |
||
| 88 | return $o; |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * input |
||
| 93 | * |
||
| 94 | * This method returns a input text element. |
||
| 95 | * |
||
| 96 | * @param array(id, name, class, onclick, value, length, width, disable,placeholder) |
||
| 97 | * |
||
| 98 | * @return string |
||
| 99 | */ |
||
| 100 | public static function input($params = array()) |
||
| 101 | { |
||
| 102 | |||
| 103 | if (!is_array($params)) { |
||
| 104 | throw new \InvalidArgumentException("Arguments is not a Array" . print_r($params, true)); |
||
| 105 | } |
||
| 106 | |||
| 107 | $o = '<input '; |
||
| 108 | $o .= self::param_mix($params); |
||
| 109 | $o .= (isset($params['onkeypress'])) ? " onkeypress='{$params['onkeypress']}'" : ''; |
||
| 110 | $o .= (isset($params['length'])) ? " maxlength='{$params['length']}'" : ''; |
||
| 111 | $o .= (isset($params['width'])) ? " style='width:{$params['width']}px;'" : ''; |
||
| 112 | $o .= (isset($params['accept'])) ? " accept='{$params['accept']}'" : ''; |
||
| 113 | $o .= (isset($params['maxlength'])) ? " maxlength='{$params['maxlength']}'" : ''; |
||
| 114 | $o .= (isset($params['minlength'])) ? " minlength='{$params['minlength']}'" : ''; |
||
| 115 | $o .= (isset($params['autocomplete'])) ? " autocomplete='{$params['autocomplete']}'" : ''; |
||
| 116 | $o .= (isset($params['autofocus'])) ? " autofocus" : ''; |
||
| 117 | $o .= " />\n"; |
||
| 118 | return $o; |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * select |
||
| 123 | * |
||
| 124 | * This method returns a select html element. |
||
| 125 | * It can be given a param called value which then will be preselected |
||
| 126 | * data has to be array(k=>v) |
||
| 127 | * |
||
| 128 | * @param array(id, name, class, onclick, disabled) |
||
| 129 | * |
||
| 130 | * @return string |
||
| 131 | */ |
||
| 132 | public static function select($params = array()) |
||
| 133 | { |
||
| 134 | if (!is_array($params)) { |
||
| 135 | throw new \InvalidArgumentException("Arguments is not a Array" . print_r($params, true)); |
||
| 136 | } |
||
| 137 | |||
| 138 | $o = "<select"; |
||
| 139 | $o .= self::param_mix($params); |
||
| 140 | $o .= (isset($params['width'])) ? " style='width:{$params['width']}px;'" : ''; |
||
| 141 | $o .= ">\n"; |
||
| 142 | $o .= "<option value=''>Select</option>\n"; |
||
| 143 | if (isset($params['data']) && is_array($params['data'])) { |
||
| 144 | foreach ($params['data'] as $k => $v) { |
||
| 145 | if (isset($params['value']) && $params['value'] == $k) { |
||
| 146 | $o .= "<option value='{$k}' selected='selected'>{$v}</option>\n"; |
||
| 147 | } else { |
||
| 148 | $o .= "<option value='{$k}'>{$v}</option>\n"; |
||
| 149 | } |
||
| 150 | } else { |
||
|
|
|||
| 151 | throw new \InvalidArgumentException("Arguments is not valid " . print_r($params, true)); |
||
| 152 | |||
| 153 | } |
||
| 154 | } |
||
| 155 | $o .= "</select>\n"; |
||
| 156 | return $o; |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * checkboxMulti |
||
| 161 | * |
||
| 162 | * This method returns multiple checkbox elements in order given in an array |
||
| 163 | * For checking of checkbox pass checked |
||
| 164 | * Each checkbox should look like array(0=>array('id'=>'1', 'name'=>'cb[]', 'value'=>'x', 'label'=>'label_text' )) |
||
| 165 | * |
||
| 166 | * @param array(array(id, name, value, class, checked, disabled)) |
||
| 167 | * |
||
| 168 | * @return string |
||
| 169 | */ |
||
| 170 | public static function checkbox($params = array()) |
||
| 171 | { |
||
| 172 | |||
| 173 | if (!is_array($params)) { |
||
| 174 | throw new \InvalidArgumentException("Arguments is not a Array" . print_r($params, true)); |
||
| 175 | } |
||
| 176 | $o = ''; |
||
| 177 | $x = 0; |
||
| 178 | foreach ($params as $k => $v) { |
||
| 179 | $v['id'] = (isset($v['id'])) ? $v['id'] : "cb_id_{$x}_" . rand(1000, 9999); |
||
| 180 | $o .= "<input type='checkbox'"; |
||
| 181 | $o .= self::param_mix($params); |
||
| 182 | $o .= (isset($v['checked'])) ? " checked='checked'" : ''; |
||
| 183 | $o .= (isset($v['disabled'])) ? " disabled='{$v['disabled']}'" : ''; |
||
| 184 | $o .= " />\n"; |
||
| 185 | $o .= (isset($v['label'])) ? "<label for='{$v['id']}'>{$v['label']}</label> " : ''; |
||
| 186 | $x++; |
||
| 187 | } |
||
| 188 | |||
| 189 | return $o; |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * radioMulti |
||
| 194 | * |
||
| 195 | * This method returns radio elements in order given in an array |
||
| 196 | * For selection pass checked |
||
| 197 | * Each radio should look like array(0=>array('id'=>'1', 'name'=>'rd[]', 'value'=>'x', 'label'=>'label_text' )) |
||
| 198 | * |
||
| 199 | * @param array(array(id, name, value, class, checked, disabled, label)) |
||
| 200 | * |
||
| 201 | * @return string |
||
| 202 | */ |
||
| 203 | public static function radio($params = array()) |
||
| 204 | { |
||
| 205 | |||
| 206 | if (!is_array($params)) { |
||
| 207 | throw new \InvalidArgumentException("Arguments is not a Array" . print_r($params, true)); |
||
| 208 | } |
||
| 209 | |||
| 210 | $o = ''; |
||
| 211 | $x = 0; |
||
| 212 | foreach ($params as $k => $v) { |
||
| 213 | $v['id'] = (isset($v['id'])) ? $v['id'] : "rd_id_{$x}_" . rand(1000, 9999); |
||
| 214 | $o .= "<input type='radio'"; |
||
| 215 | $o .= self::param_mix($params); |
||
| 216 | $o .= (isset($v['checked'])) ? " checked='checked'" : ''; |
||
| 217 | $o .= (isset($v['disabled'])) ? " disabled='{$v['disabled']}'" : ''; |
||
| 218 | $o .= " />\n"; |
||
| 219 | $o .= (isset($v['label'])) ? "<label for='{$v['id']}'>{$v['label']}</label> " : ''; |
||
| 220 | $x++; |
||
| 221 | } |
||
| 222 | |||
| 223 | return $o; |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * This method returns a button element given the params for settings |
||
| 228 | * |
||
| 229 | * @param array(id, name, class, onclick, value, disabled) |
||
| 230 | * |
||
| 231 | * @return string |
||
| 232 | */ |
||
| 233 | public static function button($params = array()) |
||
| 234 | { |
||
| 235 | |||
| 236 | if (!is_array($params)) { |
||
| 237 | throw new \InvalidArgumentException("Arguments is not a Array" . print_r($params, true)); |
||
| 238 | } |
||
| 239 | |||
| 240 | $o = "<button type='submit'"; |
||
| 241 | $o .= self::param_mix($params); |
||
| 242 | $o .= (isset($params['onclick'])) ? " onclick='{$params['onclick']}'" : ''; |
||
| 243 | $o .= ">"; |
||
| 244 | $o .= (isset($params['class'])) ? "<i class='fa {$params['iclass']}'></i> " : ''; |
||
| 245 | $o .= "</button>\n"; |
||
| 246 | return $o; |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * This method returns a submit button element given the params for settings |
||
| 251 | * |
||
| 252 | * @param array(id, name, class, onclick, value, disabled) |
||
| 253 | * |
||
| 254 | * @return string |
||
| 255 | */ |
||
| 256 | public static function submit($params = array()) |
||
| 257 | { |
||
| 258 | |||
| 259 | if (!is_array($params)) { |
||
| 260 | throw new \InvalidArgumentException("Arguments is not a Array" . print_r($params, true)); |
||
| 261 | } |
||
| 262 | |||
| 263 | $o = '<input type="submit"'; |
||
| 264 | $o .= self::param_mix($params); |
||
| 265 | $o .= (isset($params['onclick'])) ? " onclick='{$params['onclick']}'" : ''; |
||
| 266 | $o .= " />\n"; |
||
| 267 | return $o; |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * |
||
| 272 | * @since 1.0.6 |
||
| 273 | * |
||
| 274 | * @param array('for' => 'exemplo-title' , 'title' => 'Example Title'); |
||
| 275 | * |
||
| 276 | * @return string |
||
| 277 | */ |
||
| 278 | |||
| 279 | private static function param_mix($params = array()) { |
||
| 280 | |||
| 281 | if (!is_array($params)) { |
||
| 282 | throw new \InvalidArgumentException("Arguments is not a Array" . print_r($params, true)); |
||
| 283 | } |
||
| 284 | $o =''; |
||
| 285 | $o .= (isset($params['id'])) ? " id='{$params['id']}'" : ''; |
||
| 286 | $o .= (isset($params['name'])) ? " name='{$params['name']}'" : ''; |
||
| 287 | $o .= (isset($params['class'])) ? " class='form-input textbox {$params['class']}'" : ''; |
||
| 288 | $o .= (isset($params['onclick'])) ? " onclick='{$params['onclick']}'" : ''; |
||
| 289 | $o .= (isset($params['disabled'])) ? " disabled='{$params['disabled']}'" : ''; |
||
| 290 | $o .= (isset($params['required'])) ? " required='required'" : ''; |
||
| 291 | $o .= (isset($params['type'])) ? " type='{$params['type']}'" : 'type="text"'; |
||
| 292 | return $o; |
||
| 293 | |||
| 294 | } |
||
| 295 | public static function label($params = array()) |
||
| 296 | { |
||
| 297 | $o = "<label"; |
||
| 298 | $o .= (isset($params['for'])) ? " for='{$params['for']}'" : ''; |
||
| 299 | $o .= (isset($params['class'])) ? " class='{$params['class']}'" : ''; |
||
| 300 | $o .= '>'; |
||
| 301 | $o .= (isset($params['title']) ? $params['title'] : " "); |
||
| 302 | $o .= '</label>'; |
||
| 303 | |||
| 304 | return $o; |
||
| 305 | } |
||
| 308 |