Passed
Pull Request — master (#1112)
by Iman
04:03
created

FormConfigGenerator::generateFormConfig()   C

Complexity

Conditions 14
Paths 34

Size

Total Lines 68
Code Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 50
nc 34
nop 2
dl 0
loc 68
rs 5.7471
c 0
b 0
f 0

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
namespace crocodicstudio\crudbooster\Modules\ModuleGenerator\ControllerGenerator;
4
5
use crocodicstudio\crudbooster\helpers\DbInspector;
6
use crocodicstudio\crudbooster\Modules\ModuleGenerator\FileManipulator;
7
use Illuminate\Support\Facades\Schema;
8
use CB;
9
10
class FormConfigGenerator
11
{
12
    /**
13
     * @param $table
14
     * @param $coloms
15
     * @return array
16
     */
17
    static function generateFormConfig($table, $coloms)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
18
    {
19
        $formArrayString = [];
20
        foreach ($coloms as $i => $colName) {
21
            //$attribute = [];
22
            $input = [
23
                'label' => self::getLabel($colName),
24
                'name' => $colName,
25
                'type' => '',
26
                'options' => '',
27
                'required' => true,
28
                'validation' => '',
29
                'help' => '',
30
                'placeholder' => '',
31
            ];
32
33
            if (FieldDetector::isExceptional($colName)) {
34
                continue;
35
            }
36
37
            $typeData = DbInspector::getFieldTypes($table, $colName);
38
39
            list($input['type'], $input['validation'], $input['options']) = self::parseFieldType($typeData);
40
41
            if (FieldDetector::isForeignKey($colName)) {
42
                list($input['type'], $input['options']) = self::handleForeignKey($colName);
43
            }
44
45
            if (substr($colName, 0, 3) == 'is_') {
46
                $input['type'] = 'radio_dataenum';
47
                $label = ucwords(substr($colName, 3));
48
                $input['options'] = [
49
                    'enum' => ['In '.$label, $label],
50
                    'value' => [0, 1],
51
                ];
52
            }
53
54
            if (FieldDetector::isPassword($colName)) {
55
                $input['type'] = 'password';
56
                $input['validation'] = 'min:5|max:32|required';
57
                $input['help'] = cbTrans("text_default_help_password");
58
            }elseif (FieldDetector::isImage($colName)) {
59
                $input['type'] = 'upload';
60
                $input['validation'] = 'required|image';
61
                $input['help'] = cbTrans('text_default_help_upload');
62
            }elseif (FieldDetector::isGeographical($colName)) {
63
                $input['type'] = 'hidden';
64
                $input['validation'] = 'required|numeric';
65
            }elseif (FieldDetector::isPhone($colName)) {
66
                $input['type'] = 'number';
67
                $input['validation'] = 'required|numeric';
68
                $input['placeholder'] = cbTrans('text_default_help_number');
69
            }elseif (FieldDetector::isEmail($colName)) {
70
                $input['type'] = 'email';
71
                $input['validation'] = 'require|email|unique:'.$table;
72
                $input['placeholder'] = cbTrans('text_default_help_email');
73
            }elseif ($input['type'] == 'text' && FieldDetector::isNameField($colName)) {
74
                $input['validation'] = 'required|string|min:3|max:70';
75
                $input['placeholder'] = cbTrans('text_default_help_text');
76
            }elseif ($input['type'] == 'text' && FieldDetector::isUrlField($colName)) {
77
                $input['validation'] = 'required|url';
78
                $input['placeholder'] = cbTrans('text_default_help_url');
79
            }
80
81
            $formArrayString[] = FileManipulator::stringify($input, "            ");
82
        }
83
84
        return $formArrayString;
85
    }
86
87
    /**
88
     * @param $c
89
     * @return mixed|string
90
     */
91
    private static function getLabel($c)
92
    {
93
        $label = str_replace("id_", "", $c);
94
        $label = ucwords(str_replace("_", " ", $label));
95
96
        return $label;
97
    }
98
99
    /**
100
     * @param $typeData
101
     *
102
     * @return array
103
     */
104
    private static function parseFieldType($typeData)
105
    {
106
        $typeData = array_get([
107
            'longtext' => 'text',
108
            'integer' => 'int',
109
            'timestamp' => 'datetime',
110
        ], $typeData, $typeData);
111
112
113
        $default = ["text", "min:1|max:255", []];
114
        return array_get([
115
            'text' => ['textarea', "string|min:5", []],
116
            'date' => ['date', "date", ['php_format' => 'M, d Y', 'datepicker_format' => 'M, dd YYYY',]],
117
            'datetime' => ['datetime', "date_format:Y-m-d H:i:s", ['php_format' => 'M, d Y H:i',]],
118
            'time' => ['time', 'date_format:H:i:s', []],
119
            'double' => ['money', "integer|min:0", []],
120
            'int' => ['number', 'integer|min:0', []],
121
        ], $typeData, $default);
0 ignored issues
show
Bug introduced by
It seems like $typeData can also be of type array<string,string>; however, parameter $key of array_get() 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

121
        ], /** @scrutinizer ignore-type */ $typeData, $default);
Loading history...
122
    }
123
124
    /**
125
     * @param $field
126
     * @return array
127
     */
128
    private static function handleForeignKey($field)
129
    {
130
        $jointable = str_replace(['id_', '_id'], '', $field);
131
        if (!Schema::hasTable($jointable)) {
132
            return ['', ''];
133
        }
134
        $options = [
135
            'table' => $jointable,
136
            'field_label' => DbInspector::colName(DbInspector::getTableCols($jointable)),
137
            'field_value' => DbInspector::findPk($jointable),
138
        ];
139
140
        return ['select2_datatable', $options];
141
    }
142
}