| Conditions | 12 |
| Paths | 11 |
| Total Lines | 65 |
| Code Lines | 50 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 81 | public function save(EntityFieldRequest $request) |
||
| 82 | { |
||
| 83 | try { |
||
| 84 | $data = $request->only($this->formNames); |
||
| 85 | $table = EntityRepository::find($data['entity_id']); |
||
| 86 | if (!$table) { |
||
| 87 | return [ |
||
| 88 | 'code' => 1, |
||
| 89 | 'msg' => '新增失败:模型不存在', |
||
| 90 | ]; |
||
| 91 | } |
||
| 92 | if (Schema::hasColumn($table->table_name, $data['name'])) { |
||
| 93 | return [ |
||
| 94 | 'code' => 2, |
||
| 95 | 'msg' => '新增失败:字段已存在', |
||
| 96 | ]; |
||
| 97 | } |
||
| 98 | if (!in_array($data['type'], config('light.db_table_field_type'))) { |
||
| 99 | return [ |
||
| 100 | 'code' => 3, |
||
| 101 | 'msg' => '新增失败:无效字段类型', |
||
| 102 | ]; |
||
| 103 | } |
||
| 104 | |||
| 105 | Schema::table($table->table_name, function (Blueprint $table) use ($data) { |
||
| 106 | $m = $data['type']; |
||
| 107 | $length = intval($data['field_length']); |
||
| 108 | $total = intval($data['field_total']); |
||
| 109 | $scale = intval($data['field_scale']); |
||
| 110 | if (in_array($m, ['char', 'string'])) { |
||
| 111 | $table->$m($data['name'], $length > 0 ? $length : 255) |
||
| 112 | ->comment($data['comment']) |
||
| 113 | ->default(strval($data['default_value'])); |
||
| 114 | } elseif (Str::contains(Str::lower($m), 'integer')) { |
||
| 115 | $table->$m($data['name']) |
||
| 116 | ->comment($data['comment']) |
||
| 117 | ->default(intval($data['default_value'])); |
||
| 118 | } elseif (in_array($m, ['float', 'double', 'decimal', 'unsignedDecimal'])) { |
||
| 119 | if ($total > 0 && $scale > 0 && $total > $scale) { |
||
| 120 | $table->$m($data['name'], $total, $scale) |
||
| 121 | ->comment($data['comment']) |
||
| 122 | ->default(doubleval($data['default_value'])); |
||
| 123 | } else { |
||
| 124 | $table->$m($data['name']) |
||
| 125 | ->comment($data['comment']) |
||
| 126 | ->default(doubleval($data['default_value'])); |
||
| 127 | } |
||
| 128 | } else { |
||
| 129 | $table->$m($data['name'])->comment($data['comment'])->nullable(); |
||
| 130 | } |
||
| 131 | }); |
||
| 132 | |||
| 133 | unset($data['field_length'], $data['field_total'], $data['field_scale']); |
||
| 134 | EntityFieldRepository::add($data); |
||
| 135 | |||
| 136 | return [ |
||
| 137 | 'code' => 0, |
||
| 138 | 'msg' => '新增成功', |
||
| 139 | 'redirect' => true |
||
| 140 | ]; |
||
| 141 | } catch (\Exception $e) { |
||
| 142 | \Log::error($e); |
||
| 143 | return [ |
||
| 144 | 'code' => 1, |
||
| 145 | 'msg' => '新增失败:' . $e->getMessage(), |
||
| 146 | ]; |
||
| 242 |