Conditions | 33 |
Paths | 174 |
Total Lines | 72 |
Code Lines | 51 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 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 |
||
31 | public static function create(object $model, string $action = ''): void |
||
32 | { |
||
33 | $model_name = $model::class; |
||
34 | if (!$action) { |
||
35 | $action = ltrim(Router::get('route'), '/'); |
||
|
|||
36 | } |
||
37 | // separar para diferentes ORM u otros formatos json, ini, xml, array,... |
||
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 class=\"required\">$alias" , PHP_EOL; |
||
53 | $required = ' required'; |
||
54 | } else { |
||
55 | echo "<label>$alias" , PHP_EOL; |
||
56 | $required = ''; |
||
57 | } |
||
58 | |||
59 | switch ($tipo) { |
||
60 | case 'tinyint': case 'smallint': case 'mediumint': |
||
61 | case 'integer': case 'int': case 'bigint': |
||
62 | case 'float': case 'double': case 'precision': |
||
63 | case 'real': case 'decimal': case 'numeric': |
||
64 | case 'year': case 'day': case 'int unsigned': // Números |
||
65 | |||
66 | if (str_ends_with($field, '_id')) { |
||
67 | echo Form::dbSelect($model_name.'.'.$field, null, null, 'Seleccione', $required, $model->$field); |
||
68 | break; |
||
69 | } |
||
70 | |||
71 | echo "<input id=\"$formId\" type=\"number\" name=\"$formName\" value=\"{$model->$field}\"$required>" , PHP_EOL; |
||
72 | break; |
||
73 | |||
74 | case 'date': |
||
75 | echo "<input id=\"$formId\" type=\"date\" name=\"$formName\" value=\"{$model->$field}\"$required>" , PHP_EOL; |
||
76 | break; |
||
77 | |||
78 | case 'datetime': case 'timestamp': |
||
79 | echo "<input id=\"$formId\" type=\"datetime-local\" name=\"$formName\" value=\"{$model->$field}\"$required>" , PHP_EOL; |
||
80 | break; |
||
81 | |||
82 | case 'enum': case 'set': case 'bool': |
||
83 | $enumList = explode(',', str_replace("'", '', substr($model->_data_type[$field], 5, (strlen($model->_data_type[$field]) - 6)))); |
||
84 | echo "<select id=\"$formId\" class=\"select\" name=\"$formName\" >", PHP_EOL; |
||
85 | foreach ($enumList as $value) { |
||
86 | echo "<option value=\"{$value}\">$value</option>", PHP_EOL; |
||
87 | } |
||
88 | echo '</select>', PHP_EOL; |
||
89 | break; |
||
90 | |||
91 | case 'text': case 'mediumtext': case 'longtext': // Usar textarea |
||
92 | case 'blob': case 'mediumblob': case 'longblob': |
||
93 | echo "<textarea id=\"$formId\" name=\"$formName\"$required>{$model->$field}</textarea>" , PHP_EOL; |
||
94 | break; |
||
95 | |||
96 | default: //text,tinytext,varchar, char,etc se comprobara su tamaño |
||
97 | echo "<input id=\"$formId\" type=\"text\" name=\"$formName\" value=\"{$model->$field}\"$required>" , PHP_EOL; |
||
98 | } |
||
99 | echo '</label>'; |
||
100 | } |
||
101 | echo '<input type="submit">' , PHP_EOL; |
||
102 | echo '</form>' , PHP_EOL; |
||
103 | } |
||
105 |