ModelForm::create()   F
last analyzed

Complexity

Conditions 33
Paths 174

Size

Total Lines 72
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 51
c 4
b 0
f 0
dl 0
loc 72
rs 3.55
cc 33
nc 174
nop 2

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
/**
3
 * KumbiaPHP web & app Framework
4
 *
5
 * LICENSE
6
 *
7
 * This source file is subject to the new BSD license that is bundled
8
 * with this package in the file LICENSE.
9
 *
10
 * @category   KumbiaPHP
11
 * @package    Helpers
12
 *
13
 * @copyright  Copyright (c) 2005 - 2024 KumbiaPHP Team (http://www.kumbiaphp.com)
14
 * @license    https://github.com/KumbiaPHP/KumbiaPHP/blob/master/LICENSE   New BSD License
15
 */
16
17
/**
18
 * Helper para crear Formularios de un modelo automáticamente.
19
 *
20
 * @category   KumbiaPHP
21
 * @package    Helpers
22
 */
23
class ModelForm
24
{
25
    /**
26
     * Generate a form from model automatically
27
     * -
28
     * Genera un form de un modelo (objeto) automáticamente.
29
     *
30
     */
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'), '/');
0 ignored issues
show
Bug introduced by
It seems like Router::get('route') can also be of type array; however, parameter $string of ltrim() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

35
            $action = ltrim(/** @scrutinizer ignore-type */ Router::get('route'), '/');
Loading history...
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
    }
104
}
105