Conditions | 13 |
Paths | 163 |
Total Lines | 66 |
Code Lines | 44 |
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 |
||
28 | public function pref($compiler_list, $pid, $uid, $cid=null) |
||
29 | { |
||
30 | $countCompilerList=count($compiler_list); |
||
31 | $pref=-1; |
||
32 | $precise=true; |
||
33 | // get user pref of this problem for compilers |
||
34 | $temp_last_submission=DB::table("submission")->where(["pid"=>$pid, "uid"=>$uid, "cid"=>$cid])->orderBy('submission_date', 'desc')->first(); |
||
35 | if (empty($temp_last_submission)) { |
||
36 | // get user pref of this OJ for compilers |
||
37 | $problemModel=new ProblemModel(); |
||
38 | $oid=$problemModel->oid($pid); |
||
39 | $temp_last_submission=DB::table("submission")->join("problem", "submission.pid", "=", "problem.pid")->where(["OJ"=>$oid, "uid"=>$uid])->orderBy('submission_date', 'desc')->first(); |
||
40 | if (empty($temp_last_submission)) { |
||
41 | // get user pref for compilers |
||
42 | $temp_last_submission=DB::table("submission")->where(["uid"=>$uid])->orderBy('submission_date', 'desc')->first(); |
||
43 | if (empty($temp_last_submission)) { |
||
44 | return [ |
||
45 | "pref"=>$pref, |
||
46 | "code"=>"" |
||
47 | ]; |
||
48 | } |
||
49 | } |
||
50 | $precise=false; |
||
51 | } |
||
52 | $last_submission=$temp_last_submission; |
||
53 | if ($precise) { |
||
54 | $ret["code"]=$last_submission["solution"]; |
||
|
|||
55 | $ret['code']=str_replace('\\', '\\\\', $ret['code']); |
||
56 | $ret['code']=str_replace("\r\n", "\\n", $ret['code']); |
||
57 | $ret['code']=str_replace("\n", "\\n", $ret['code']); |
||
58 | $ret['code']=str_replace("\"", "\\\"", $ret['code']); |
||
59 | $ret['code']=str_replace("<", "\<", $ret['code']); |
||
60 | $ret['code']=str_replace(">", "\>", $ret['code']); |
||
61 | } else { |
||
62 | $ret["code"]=""; |
||
63 | } |
||
64 | $ret["coid"]=$last_submission["coid"]; |
||
65 | $ret["detail"]=$this->detail($last_submission["coid"]); |
||
66 | // match precise compiler |
||
67 | for ($i=0; $i<$countCompilerList; $i++) { |
||
68 | if ($compiler_list[$i]["coid"]==$ret["coid"]) { |
||
69 | $pref=$i; |
||
70 | break; |
||
71 | } |
||
72 | } |
||
73 | if ($pref==-1) { |
||
74 | // precise compiler is dead, use other compiler with same lang |
||
75 | for ($i=0; $i<$countCompilerList; $i++) { |
||
76 | if ($compiler_list[$i]["lang"]==$ret["detail"]["lang"]) { |
||
77 | $pref=$i; |
||
78 | break; |
||
79 | } |
||
80 | } |
||
81 | } |
||
82 | if ($pref==-1) { |
||
83 | // same lang compilers are all dead, use other compiler within the same group |
||
84 | for ($i=0; $i<$countCompilerList; $i++) { |
||
85 | if ($compiler_list[$i]["comp"]==$ret["detail"]["comp"]) { |
||
86 | $pref=$i; |
||
87 | break; |
||
88 | } |
||
89 | } |
||
90 | } |
||
91 | // the entire comp group dead |
||
92 | $ret["pref"]=$pref; |
||
93 | return $ret; |
||
94 | } |
||
131 |