Conditions | 33 |
Paths | 186 |
Total Lines | 70 |
Code Lines | 50 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
32 | public static function create($model, $action = '') |
||
33 | { |
||
34 | $model_name = get_class($model); |
||
35 | if (!$action) { |
||
36 | $action = ltrim(Router::get('route'), '/'); |
||
37 | } |
||
38 | |||
39 | echo '<form action="', PUBLIC_PATH.$action, '" method="post" id="', $model_name, '" class="scaffold">' , PHP_EOL; |
||
40 | $pk = $model->primary_key[0]; |
||
41 | echo '<input id="', $model_name, '_', $pk, '" name="', $model_name, '[', $pk, ']" class="id" value="', $model->$pk , '" type="hidden">' , PHP_EOL; |
||
42 | |||
43 | $fields = array_diff($model->fields, $model->_at, $model->_in, $model->primary_key); |
||
44 | |||
45 | foreach ($fields as $field) { |
||
46 | $tipo = trim(preg_replace('/(\(.*\))/', '', $model->_data_type[$field])); //TODO: recoger tamaño y otros valores |
||
47 | $alias = $model->get_alias($field); |
||
48 | $formId = $model_name.'_'.$field; |
||
49 | $formName = $model_name.'['.$field.']'; |
||
50 | |||
51 | if (in_array($field, $model->not_null)) { |
||
52 | echo "<label for=\"$formId\" class=\"required\">$alias *</label>" , PHP_EOL; |
||
53 | } else { |
||
54 | echo "<label for=\"$formId\">$alias</label>" , PHP_EOL; |
||
55 | } |
||
56 | |||
57 | switch ($tipo) { |
||
58 | case 'tinyint': case 'smallint': case 'mediumint': |
||
59 | case 'integer': case 'int': case 'bigint': |
||
60 | case 'float': case 'double': case 'precision': |
||
61 | case 'real': case 'decimal': case 'numeric': |
||
62 | case 'year': case 'day': case 'int unsigned': // Números |
||
63 | |||
64 | if (strripos($field, '_id', -3)) { |
||
65 | echo Form::dbSelect($model_name.'.'.$field, null, null, 'Seleccione', null, $model->$field); |
||
66 | break; |
||
67 | } else { |
||
68 | echo "<input id=\"$formId\" type=\"number\" name=\"$formName\" value=\"{$model->$field}\">" , PHP_EOL; |
||
69 | break; |
||
70 | } |
||
71 | |||
72 | // no break |
||
73 | case 'date': // Usar el js de datetime |
||
74 | echo "<input id=\"$formId\" type=\"date\" name=\"$formName\" value=\"{$model->$field}\">" , PHP_EOL; |
||
75 | break; |
||
76 | |||
77 | case 'datetime': case 'timestamp': |
||
78 | echo "<input id=\"$formId\" type=\"datetime\" name=\"$formName\" value=\"{$model->$field}\">" , PHP_EOL; |
||
79 | break; |
||
80 | |||
81 | case 'enum': case 'set': case 'bool': |
||
82 | $enumList = explode(',', str_replace("'", '', substr($model->_data_type[$field], 5, (strlen($model->_data_type[$field]) - 6)))); |
||
83 | echo "<select id=\"$formId\" class=\"select\" name=\"$formName\" >", PHP_EOL; |
||
84 | foreach ($enumList as $value) { |
||
85 | echo "<option value=\"{$value}\">$value</option>", PHP_EOL; |
||
86 | } |
||
87 | echo '</select>', PHP_EOL; |
||
88 | break; |
||
89 | |||
90 | case 'text': case 'mediumtext': case 'longtext': // Usar textarea |
||
91 | case 'blob': case 'mediumblob': case 'longblob': |
||
92 | echo "<textarea id=\"$formId\" name=\"$formName\">{$model->$field}</textarea>" , PHP_EOL; |
||
93 | break; |
||
94 | |||
95 | default: //text,tinytext,varchar, char,etc se comprobara su tamaño |
||
96 | echo "<input id=\"$formId\" type=\"text\" name=\"$formName\" value=\"{$model->$field}\">" , PHP_EOL; |
||
97 | } |
||
98 | } |
||
99 | echo '<input type="submit" value="Enviar" />' , PHP_EOL; |
||
100 | echo '</form>' , PHP_EOL; |
||
101 | } |
||
102 | } |
||
103 |