Conditions | 16 |
Paths | 2 |
Total Lines | 94 |
Code Lines | 72 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
145 | protected function form($create = false) |
||
146 | { |
||
147 | $form=new Form(new EloquentProblemModel); |
||
148 | $form->model()->makeVisible('password'); |
||
149 | $form->tab('Basic', function(Form $form){ |
||
150 | $form->text('pid')->readonly(); |
||
151 | $form->text('pcode')->rules('required'); |
||
152 | $form->text('title')->rules('required'); |
||
153 | $form->text('time_limit')->rules('required'); |
||
154 | $form->text('memory_limit')->rules('required'); |
||
155 | $form->textarea('description')->rows(5); |
||
156 | $form->textarea('input','Sample Input')->rows(3); |
||
157 | $form->textarea('output','Sample Output')->rows(3); |
||
158 | $form->textarea('note')->rows(2); |
||
159 | $form->display('OJ'); |
||
160 | $form->display('update_date'); |
||
161 | $form->text('tot_score')->rules('required'); |
||
162 | $form->select('partial', 'Partial Score')->options([ |
||
163 | 0 => "No", |
||
164 | 1 => "Yes" |
||
165 | ])->rules('required'); |
||
166 | $form->select('markdown', 'Markdown Support')->options([ |
||
167 | 0 => "No", |
||
168 | 1 => "Yes" |
||
169 | ])->rules('required'); |
||
170 | $form->file('test_case'); |
||
171 | $form->ignore(['test_case']); |
||
172 | }); |
||
173 | if($create){ |
||
174 | $form->tools(function (Form\Tools $tools) { |
||
175 | $tools->add('<a href="/'.config('admin.route.prefix').'/problems/import" class="btn btn-sm btn-success" style="margin-right:1rem"><i class="MDI file-powerpoint-box"></i> Import from file</a>'); |
||
|
|||
176 | }); |
||
177 | } |
||
178 | $form->saving(function (Form $form){ |
||
179 | $err = function ($msg) { |
||
180 | $error = new MessageBag([ |
||
181 | 'title' => 'Test case file parse faild.', |
||
182 | 'message' => $msg, |
||
183 | ]); |
||
184 | return back()->with(compact('error')); |
||
185 | }; |
||
186 | $pcode = $form->pcode; |
||
187 | $p = EloquentProblemModel::where('pcode',$pcode)->first(); |
||
188 | $pid = $form->pid ?? null; |
||
189 | if(!empty($p) && $p->pid != $pid){ |
||
190 | $error = new MessageBag([ |
||
191 | 'title' => 'Error occur.', |
||
192 | 'message' => 'Pcode has been token', |
||
193 | ]); |
||
194 | return back()->with(compact('error')); |
||
195 | } |
||
196 | $test_case = \request()->file('test_case'); |
||
197 | if(!empty($test_case)){ |
||
198 | if($test_case->extension() != 'zip'){ |
||
199 | $err('You must upload a zip file iuclude test case info and content.'); |
||
200 | } |
||
201 | $path = $test_case->path(); |
||
202 | $zip = new ZipArchive; |
||
203 | if($zip->open($path) !== true) { |
||
204 | $err('You must upload a zip file without encrypt and can open successfully.'); |
||
205 | }; |
||
206 | if(($zip->getFromName('info')) === false){ |
||
207 | $err('The zip files must include a file named info including info of test cases, and the format can see ZsgsDesign/NOJ wiki.'); |
||
208 | }; |
||
209 | $test_case_info = json_decode($zip->getFromName('info'),true); |
||
210 | $test_cases = $test_case_info['test_cases']; |
||
211 | foreach($test_cases as $index => $case) { |
||
212 | if(!isset($case['input_name']) || !isset($case['output_name'])) { |
||
213 | $err("Test case index {$index}: configuration missing input/output files name."); |
||
214 | } |
||
215 | if($zip->getFromName($case['input_name']) === false || $zip->getFromName($case['output_name']) === false ) { |
||
216 | $err("Test case index {$index}: missing input/output files that record in the configuration."); |
||
217 | } |
||
218 | } |
||
219 | if(!empty($form->pid)){ |
||
220 | $problem = EloquentProblemModel::find($form->pid); |
||
221 | if(!empty($problem)){ |
||
222 | $pcode = $problem->pcode; |
||
223 | }else{ |
||
224 | $pcode = $form->pcode; |
||
225 | } |
||
226 | }else{ |
||
227 | $pcode = $form->pcode; |
||
228 | } |
||
229 | |||
230 | if(Storage::exists(base_path().'/storage/test_case/'.$pcode)){ |
||
231 | Storage::deleteDirectory(base_path().'/storage/test_case/'.$pcode); |
||
232 | } |
||
233 | Storage::makeDirectory(base_path().'/storage/test_case/'.$pcode); |
||
234 | $zip->extractTo(base_path().'/storage/test_case/'.$pcode.'/'); |
||
235 | |||
236 | } |
||
237 | }); |
||
238 | return $form; |
||
239 | } |
||
241 |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.