Conditions | 17 |
Paths | 29 |
Total Lines | 85 |
Code Lines | 63 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 1 | 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 |
||
83 | public function save(EntityFieldRequest $request) |
||
84 | { |
||
85 | try { |
||
86 | $data = $request->only($this->formNames); |
||
87 | $data['is_show'] = $data['is_show'] ?? EntityField::SHOW_DISABLE; |
||
88 | $data['is_edit'] = $data['is_edit'] ?? EntityField::EDIT_DISABLE; |
||
89 | $data['is_required'] = $data['is_required'] ?? EntityField::REQUIRED_DISABLE; |
||
90 | $data['is_show_inline'] = $data['is_show_inline'] ?? EntityField::SHOW_NOT_INLINE; |
||
91 | $modifyDB = $request->post('is_modify_db'); |
||
92 | |||
93 | $table = EntityRepository::find($data['entity_id']); |
||
94 | if (!$table) { |
||
95 | return [ |
||
96 | 'code' => 1, |
||
97 | 'msg' => '新增失败:模型不存在', |
||
98 | ]; |
||
99 | } |
||
100 | if ($modifyDB && Schema::hasColumn($table->table_name, $data['name'])) { |
||
101 | return [ |
||
102 | 'code' => 2, |
||
103 | 'msg' => '新增失败:字段已存在', |
||
104 | ]; |
||
105 | } |
||
106 | if (!in_array($data['type'], config('light.db_table_field_type'))) { |
||
107 | return [ |
||
108 | 'code' => 3, |
||
109 | 'msg' => '新增失败:无效字段类型', |
||
110 | ]; |
||
111 | } |
||
112 | // 一个模型只能有一个 inputTags 表单类型 |
||
113 | if (EntityFieldRepository::formTypeBeUnique($data['form_type']) |
||
114 | && EntityFieldRepository::getInputTagsField($data['entity_id'])) { |
||
115 | return [ |
||
116 | 'code' => 4, |
||
117 | 'msg' => '新增失败:一个模型只能有一个标签输入框表单类型', |
||
118 | ]; |
||
119 | } |
||
120 | |||
121 | // inputTags类型表单不需要添加数据库字段 |
||
122 | if (in_array($data['form_type'], ['inputTags'], true)) { |
||
123 | $modifyDB = false; |
||
124 | } |
||
125 | if ($modifyDB) { |
||
126 | Schema::table($table->table_name, function (Blueprint $table) use ($data) { |
||
127 | $m = $data['type']; |
||
128 | $length = intval($data['field_length']); |
||
129 | $total = intval($data['field_total']); |
||
130 | $scale = intval($data['field_scale']); |
||
131 | if (in_array($m, ['char', 'string'])) { |
||
132 | $table->$m($data['name'], $length > 0 ? $length : 255) |
||
133 | ->comment($data['comment']) |
||
134 | ->default(strval($data['default_value'])); |
||
135 | } elseif (Str::contains(Str::lower($m), 'integer')) { |
||
136 | $table->$m($data['name']) |
||
137 | ->comment($data['comment']) |
||
138 | ->default(intval($data['default_value'])); |
||
139 | } elseif (in_array($m, ['float', 'double', 'decimal', 'unsignedDecimal'])) { |
||
140 | if ($total > 0 && $scale > 0 && $total > $scale) { |
||
141 | $table->$m($data['name'], $total, $scale) |
||
142 | ->comment($data['comment']) |
||
143 | ->default(doubleval($data['default_value'])); |
||
144 | } else { |
||
145 | $table->$m($data['name']) |
||
146 | ->comment($data['comment']) |
||
147 | ->default(doubleval($data['default_value'])); |
||
148 | } |
||
149 | } else { |
||
150 | $table->$m($data['name'])->comment($data['comment'])->nullable(); |
||
151 | } |
||
152 | }); |
||
153 | } |
||
154 | |||
155 | unset($data['field_length'], $data['field_total'], $data['field_scale']); |
||
156 | EntityFieldRepository::add($data); |
||
157 | |||
158 | return [ |
||
159 | 'code' => 0, |
||
160 | 'msg' => '新增成功', |
||
161 | 'redirect' => true |
||
162 | ]; |
||
163 | } catch (\Exception $e) { |
||
164 | Log::error($e); |
||
165 | return [ |
||
166 | 'code' => 1, |
||
167 | 'msg' => '新增失败:' . $e->getMessage(), |
||
168 | ]; |
||
297 |