| Conditions | 19 |
| Paths | 1 |
| Total Lines | 168 |
| Code Lines | 120 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 146 | protected function form($create = false) |
||
|
|
|||
| 147 | { |
||
| 148 | $form=new Form(new EloquentProblemModel); |
||
| 149 | $form->model()->makeVisible('password'); |
||
| 150 | $form->tab('Basic', function(Form $form){ |
||
| 151 | $form->text('pid')->readonly(); |
||
| 152 | $form->text('pcode')->rules('required'); |
||
| 153 | $form->text('title')->rules('required'); |
||
| 154 | $form->text('time_limit')->rules('required'); |
||
| 155 | $form->text('memory_limit')->rules('required'); |
||
| 156 | $form->simplemde('description')->rules('required'); |
||
| 157 | $form->simplemde('input'); |
||
| 158 | $form->simplemde('output'); |
||
| 159 | $form->simplemde('note'); |
||
| 160 | $form->hasMany('problemSamples', 'samples', function (Form\NestedForm $form) { |
||
| 161 | $form->textarea('sample_input', 'sample input')->rows(3); |
||
| 162 | $form->textarea('sample_output', 'sample output')->rows(3); |
||
| 163 | $form->textarea('sample_note', 'sample note')->rows(3); |
||
| 164 | }); |
||
| 165 | /* $form->table('samples', function ($table) { |
||
| 166 | $table->textarea('sample_input', 'sample input'); |
||
| 167 | $table->textarea('sample_output', 'sample output'); |
||
| 168 | $table->textarea('sample_note', 'sample note'); |
||
| 169 | }); */ |
||
| 170 | $ojs_temp = EloquentOJModel::select('oid', 'name')->get()->all(); |
||
| 171 | $ojs = []; |
||
| 172 | foreach($ojs_temp as $v){ |
||
| 173 | $ojs[$v->oid] = $v->name; |
||
| 174 | } |
||
| 175 | $form->select('oj', 'OJ')->options($ojs)->default(1)->rules('required'); |
||
| 176 | $form->select('Hide')->options([ |
||
| 177 | 1 => 'yes', |
||
| 178 | 0 => 'no' |
||
| 179 | ])->default(0)->rules('required'); |
||
| 180 | /* $form->display('update_date'); */ |
||
| 181 | /* $form->text('tot_score')->rules('required'); |
||
| 182 | $form->select('partial', 'Partial Score')->options([ |
||
| 183 | 0 => "No", |
||
| 184 | 1 => "Yes" |
||
| 185 | ])->rules('required'); */ |
||
| 186 | $form->hidden('markdown'); |
||
| 187 | $form->hidden('input_type'); |
||
| 188 | $form->hidden('output_type'); |
||
| 189 | $form->hidden('solved_count'); |
||
| 190 | $form->hidden('difficulty'); |
||
| 191 | $form->hidden('file'); |
||
| 192 | |||
| 193 | $form->radio('spj', 'Use SPJ') |
||
| 194 | ->options([ |
||
| 195 | 0 => 'NO', |
||
| 196 | 1 => 'YES', |
||
| 197 | ])->when(0, function (Form $form) { |
||
| 198 | })->when(1, function (Form $form) { |
||
| 199 | // $form->clang('spj_src','SPJ Source Code')->rules('required'); |
||
| 200 | // Admin::script("CodeMirror.fromTextArea(document.getElementById(\"spj_src\"), {'mode':'text/x-csrc','lineNumbers':true,'matchBrackets':true});"); |
||
| 201 | })->rules('required'); |
||
| 202 | $form->clang('spj_src','SPJ Source Code'); |
||
| 203 | $form->file('test_case')->rules('required'); |
||
| 204 | $form->ignore(['test_case']); |
||
| 205 | }); |
||
| 206 | /* if($create){ |
||
| 207 | $form->tools(function (Form\Tools $tools) { |
||
| 208 | $tools->append('<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>'); |
||
| 209 | }); |
||
| 210 | } */ |
||
| 211 | $form->saving(function (Form $form){ |
||
| 212 | $err = function ($msg) { |
||
| 213 | $error = new MessageBag([ |
||
| 214 | 'title' => 'Test case file parse faild.', |
||
| 215 | 'message' => $msg, |
||
| 216 | ]); |
||
| 217 | return back()->with(compact('error')); |
||
| 218 | }; |
||
| 219 | $pcode = $form->pcode; |
||
| 220 | $p = EloquentProblemModel::where('pcode',$pcode)->first(); |
||
| 221 | $pid = $form->pid ?? null; |
||
| 222 | if(!empty($p) && $p->pid != $pid){ |
||
| 223 | $error = new MessageBag([ |
||
| 224 | 'title' => 'Error occur.', |
||
| 225 | 'message' => 'Pcode has been token', |
||
| 226 | ]); |
||
| 227 | return back()->with(compact('error')); |
||
| 228 | } |
||
| 229 | $test_case = \request()->file('test_case'); |
||
| 230 | if(!empty($test_case)){ |
||
| 231 | if($test_case->extension() != 'zip'){ |
||
| 232 | $err('You must upload a zip file iuclude test case info and content.'); |
||
| 233 | } |
||
| 234 | $path = $test_case->path(); |
||
| 235 | $zip = new ZipArchive; |
||
| 236 | if($zip->open($path) !== true) { |
||
| 237 | $err('You must upload a zip file without encrypt and can open successfully.'); |
||
| 238 | }; |
||
| 239 | $info_content = []; |
||
| 240 | if(($zip->getFromName('info')) === false){ |
||
| 241 | $info_content = [ |
||
| 242 | 'spj' => false, |
||
| 243 | 'test_cases' => [] |
||
| 244 | ]; |
||
| 245 | $files = []; |
||
| 246 | for ($i = 0; $i < $zip->numFiles; $i++) { |
||
| 247 | $filename = $zip->getNameIndex($i); |
||
| 248 | $files[] = $filename; |
||
| 249 | } |
||
| 250 | $files_in = array_filter($files, function ($filename) { |
||
| 251 | return strpos('.in', $filename) != -1; |
||
| 252 | }); |
||
| 253 | sort($files_in); |
||
| 254 | $testcase_index = 1; |
||
| 255 | foreach($files_in as $filename_in){ |
||
| 256 | $filename = basename($filename_in, '.in'); |
||
| 257 | $filename_out = $filename.'.out'; |
||
| 258 | if(($zip->getFromName($filename_out)) === false) { |
||
| 259 | continue; |
||
| 260 | } |
||
| 261 | $test_case_in = $zip->getFromName($filename_in); |
||
| 262 | $test_case_out = $zip->getFromName($filename_out); |
||
| 263 | $info_content['test_cases']["{$testcase_index}"] = [ |
||
| 264 | 'input_size' => strlen($test_case_in), |
||
| 265 | 'input_name' => $filename_in, |
||
| 266 | 'output_size' => strlen($test_case_out), |
||
| 267 | 'output_name' => $filename_out, |
||
| 268 | 'stripped_output_md5' => md5(utf8_encode(rtrim($test_case_out))) |
||
| 269 | ]; |
||
| 270 | $testcase_index += 1; |
||
| 271 | } |
||
| 272 | $zip->addFromString('info', json_encode($info_content)); |
||
| 273 | $zip->close(); |
||
| 274 | //$err('The zip files must include a file named info including info of test cases, and the format can see ZsgsDesign/NOJ wiki.'); |
||
| 275 | }else{ |
||
| 276 | $info_content = json_decode($zip->getFromName('info'),true); |
||
| 277 | }; |
||
| 278 | $zip->open($path); |
||
| 279 | $test_cases = $info_content['test_cases']; |
||
| 280 | foreach($test_cases as $index => $case) { |
||
| 281 | if(!isset($case['input_name']) || !isset($case['output_name'])) { |
||
| 282 | $err("Test case index {$index}: configuration missing input/output files name."); |
||
| 283 | } |
||
| 284 | if($zip->getFromName($case['input_name']) === false || $zip->getFromName($case['output_name']) === false ) { |
||
| 285 | $err("Test case index {$index}: missing input/output files that record in the configuration."); |
||
| 286 | } |
||
| 287 | } |
||
| 288 | if(!empty($form->pid)){ |
||
| 289 | $problem = EloquentProblemModel::find($form->pid); |
||
| 290 | if(!empty($problem)){ |
||
| 291 | $pcode = $problem->pcode; |
||
| 292 | }else{ |
||
| 293 | $pcode = $form->pcode; |
||
| 294 | } |
||
| 295 | }else{ |
||
| 296 | $pcode = $form->pcode; |
||
| 297 | } |
||
| 298 | |||
| 299 | if(Storage::exists(base_path().'/storage/test_case/'.$pcode)){ |
||
| 300 | Storage::deleteDirectory(base_path().'/storage/test_case/'.$pcode); |
||
| 301 | } |
||
| 302 | Storage::makeDirectory(base_path().'/storage/test_case/'.$pcode); |
||
| 303 | $zip->extractTo(base_path().'/storage/test_case/'.$pcode.'/'); |
||
| 304 | |||
| 305 | } |
||
| 306 | $form->markdown = true; |
||
| 307 | $form->input_type = 'standard input'; |
||
| 308 | $form->output_type = 'standard output'; |
||
| 309 | $form->solved_count = 0; |
||
| 310 | $form->difficulty = -1; |
||
| 311 | $form->file = 0; |
||
| 312 | }); |
||
| 313 | return $form; |
||
| 314 | } |
||
| 316 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.