Test Setup Failed
Push — master ( d208c1...24e507 )
by
unknown
04:33
created

Judge::__construct()   F

Complexity

Conditions 25
Paths 104

Size

Total Lines 216
Code Lines 142

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 25
eloc 142
nc 104
nop 0
dl 0
loc 216
rs 3.3066
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
namespace App\Http\Controllers\VirtualJudge;
3
4
use App\Models\SubmissionModel;
5
use App\Models\JudgerModel;
6
use App\Http\Controllers\VirtualJudge\Core;
7
use Requests, Exception;
8
9
class Judge extends Core
10
{
11
    private $MODEL;
12
    public $ret=[];
13
    public function __construct()
14
    {
15
        $this->MODEL=new SubmissionModel();
16
        $ret=[];
17
18
        $uva_v=[
0 ignored issues
show
Unused Code introduced by
The assignment to $uva_v is dead and can be removed.
Loading history...
19
            'Wrong answer'=>"Wrong Answer",
20
            'Accepted'=>"Accepted",
21
            'Runtime error'=>"Runtime Error",
22
            'Time limit exceeded'=>"Time Limit Exceed",
23
            'Presentation error'=>"Presentation Error",
24
            'Submission error'=>'Submission Error',
25
            'Compilation error'=>"Compile Error",
26
            'Output Limit Exceeded'=>"Output limit Exceeded",
27
        ];
28
29
        $codeforces_v=[
30
            "COMPILATION_ERROR"=>"Compile Error",
31
            "RUNTIME_ERROR"=> "Runtime Error",
32
            "WRONG_ANSWER"=> "Wrong Answer",
33
            "TIME_LIMIT_EXCEEDED"=>"Time Limit Exceed" ,
34
            "OK"=>"Accepted" ,
35
            "MEMORY_LIMIT_EXCEEDED"=>"Memory Limit Exceed",
36
            "PRESENTATION_ERROR"=>"Presentation Error"];
37
38
        $spoj_v=[
0 ignored issues
show
Unused Code introduced by
The assignment to $spoj_v is dead and can be removed.
Loading history...
39
            "compilation error"=>"Compile Error",
40
            "runtime error"=> "Runtime Error",
41
            "wrong answer"=> "Wrong Answer",
42
            "time limit exceeded"=>"Time Limit Exceed",
43
            "accepted"=>"Accepted"
44
        ];
45
46
        $contesthunter_v=[
47
            '正确'=>"Accepted",
48
            '答案错误'=>"Wrong Answer",
49
            '超出时间限制'=>"Time Limit Exceed",
50
            '运行时错误'=>"Runtime Error",
51
            "超出内存限制"=>"Memory Limit Exceed",
52
            '比较器错误'=>'Submission Error',
53
            '超出输出限制'=>"Output limit Exceeded",
54
            '编译错误'=>"Compile Error",
55
        ];
56
57
        $poj_v=[
58
            'Accepted'=>"Accepted",
59
            "Presentation Error"=>"Presentation Error",
60
            'Time Limit Exceeded'=>"Time Limit Exceed",
61
            "Memory Limit Exceeded"=>"Memory Limit Exceed",
62
            'Wrong Answer'=>"Wrong Answer",
63
            'Runtime Error'=>"Runtime Error",
64
            'Output Limit Exceeded'=>"Output limit Exceeded",
65
            'Compile Error'=>"Compile Error",
66
        ];
67
68
        $vijos_v=[
69
            'Accepted'=>"Accepted",
70
            'Wrong Answer'=>"Wrong Answer",
71
            'Time Exceeded'=>"Time Limit Exceed",
72
            "Memory Exceeded"=>"Memory Limit Exceed",
73
            'Runtime Error'=>"Runtime Error",
74
            'Compile Error'=>"Compile Error",
75
            'System Error'=>"Submission Error",
76
            'Canceled'=>"Submission Error",
77
            'Unknown Error'=>"Submission Error",
78
            'Ignored'=>"Submission Error",
79
        ];
80
81
        $result=$this->MODEL->get_wating_submission();
82
        $judger=new JudgerModel();
83
84
        $cf=$this->get_last_codeforces($this->MODEL->count_wating_submission(2));
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $cf is correct as $this->get_last_codeforc...t_wating_submission(2)) targeting App\Http\Controllers\Vir...::get_last_codeforces() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
85
        $poj=[];
86
87
        $pojJudgerList=$judger->list(4);
88
        $pojJudgerName=urlencode($pojJudgerList[0]["handle"]);
89
        $this->appendPOJStatus($poj, $pojJudgerName);
90
        // $uva=$this->get_last_uva($this->MODEL->count_wating_submission('Uva'));
91
        // $uval=$this->get_last_uvalive($this->MODEL->count_wating_submission('UvaLive'));
92
        // $sj=$this->get_last_spoj($this->MODEL->count_wating_submission('Spoj'));
93
94
        $i=0;
95
        $j=0;
0 ignored issues
show
Unused Code introduced by
The assignment to $j is dead and can be removed.
Loading history...
96
        $k=0;
0 ignored issues
show
Unused Code introduced by
The assignment to $k is dead and can be removed.
Loading history...
97
        $l=0;
0 ignored issues
show
Unused Code introduced by
The assignment to $l is dead and can be removed.
Loading history...
98
99
        foreach ($result as $row) {
100
            if ($row['oid']==2) {
101
                if (isset($codeforces_v[$cf[$i][2]])) {
102
103
                    $sub['verdict'] = $codeforces_v[$cf[$i][2]];
104
                    $sub["score"]=$sub['verdict']=="Accepted"?1:0;
105
                    $sub['time'] = $cf[$i][0];
106
                    $sub['memory'] = $cf[$i][1];
107
                    $sub['remote_id'] = $cf[$i][3];
108
109
                    $ret[$row['sid']] = [
110
                        "verdict"=>$sub['verdict']
111
                    ];
112
113
                    $this->MODEL->update_submission($row['sid'], $sub);
114
                }
115
                $i++;
116
            } else if ($row['oid'] == 3) {
117
                try {
118
                    $res = Requests::get('http://contest-hunter.org:83/record/'.$row['remote_id']);
119
                    preg_match('/<dt>状态<\/dt>[\s\S]*?<dd class=".*?">(.*?)<\/dd>/m', $res->body, $match);
120
                    $status = $match[1];
121
                    if (!array_key_exists($status, $contesthunter_v)) continue;
122
                    $sub['verdict'] = $contesthunter_v[$status];
123
                    $sub["score"]=$sub['verdict']=="Accepted"?1:0;
124
                    $sub['remote_id'] = $row['remote_id'];
125
                    if ($sub['verdict'] != "Submission Error" && $sub['verdict'] != "Compile Error") {
126
                        preg_match('/占用内存[\s\S]*?(\d+).*?KiB/m', $res->body, $match);
127
                        $sub['memory'] = $match[1];
128
                        $maxtime = 0;
129
                        preg_match_all('/<span class="pull-right muted">(\d+) ms \/ \d+ KiB<\/span>/', $res->body, $matches);
130
                        foreach ($matches[1] as $time) {
131
                            if ($time < $maxtime) $maxtime = $time;
132
                        }
133
                        $sub['time'] = $maxtime;
134
                    } else {
135
                        $sub['memory'] = 0;
136
                        $sub['time'] = 0;
137
                    }
138
139
                    $ret[$row['sid']] = [
140
                        "verdict"=>$sub['verdict']
141
                    ];
142
                    $this->MODEL->update_submission($row['sid'], $sub);
143
                }
144
                catch(Exception $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
145
            } else if ($row['oid'] == 4) {
146
                if (!isset($poj[$row['remote_id']])) {
147
                    $this->appendPOJStatus($poj, $pojJudgerName, $row['remote_id']);
148
                    if (!isset($poj[$row['remote_id']])) continue;
149
                }
150
                $status = $poj[$row['remote_id']];
151
                $sub['verdict'] = $poj_v[$status['verdict']];
152
                $sub["score"]=$sub['verdict']=="Accepted"?1:0;
153
                $sub['time'] = $status['time'];
154
                $sub['memory'] = $status['memory'];
155
                $sub['remote_id'] = $row['remote_id'];
156
157
                $ret[$row['sid']] = [
158
                    "verdict"=>$sub['verdict']
159
                ];
160
                $this->MODEL->update_submission($row['sid'], $sub);
161
            } else if ($row['oid'] == 5) {
162
                try {
163
                    $res = Requests::get('https://vijos.org/records/'.$row['remote_id']);
164
                    preg_match('/<span class="record-status--text \w*">\s*(.*?)\s*<\/span>/', $res->body, $match);
165
                    $status = $match[1];
166
                    if (!array_key_exists($status, $vijos_v)) continue;
167
                    $sub['verdict'] = $vijos_v[$status];
168
                    preg_match('/<dt>分数<\/dt>\s*<dd>(\d+)<\/dd>/', $res->body, $match);
169
                    $sub['score'] = $match[1];
170
                    $sub['remote_id'] = $row['remote_id'];
171
                    if ($sub['verdict'] != "Submission Error" && $sub['verdict'] != "Compile Error") {
172
                        $maxtime = 0;
173
                        preg_match_all('/<td class="col--time">(?:&ge;)?(\d+)ms<\/td>/', $res->body, $matches);
174
                        foreach ($matches as $match) {
175
                            if ($match[1] > $maxtime) $maxtime = $match[1];
176
                        }
177
                        $sub['time'] = $maxtime;
178
                        preg_match('/<dt>峰值内存<\/dt>\s*<dd>(?:&ge;)?([\d.]+) ([KM])iB<\/dd>/', $res->body, $match);
179
                        $memory = $match[1];
180
                        if ($match[2] == 'M') $memory *= 1024;
181
                        $sub['memory'] = intval($memory);
182
                    } else {
183
                        $sub['memory'] = 0;
184
                        $sub['time'] = 0;
185
                    }
186
187
                    $ret[$row['sid']] = [
188
                        "verdict"=>$sub['verdict']
189
                    ];
190
                    $this->MODEL->update_submission($row['sid'], $sub);
191
                }
192
                catch(Exception $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
193
            }
194
            // if ($row['oid']=='Spoj') {
195
            //     if (isset($spoj_v[$sj[$j][2]])) {
196
            //         $sub['verdict']=$spoj_v[$sj[$j][2]];
197
            //         $sub['time']=$sj[$j][0];
198
            //         $sub['memory']=$sj[$j][1];
199
            //         $v=$sub['verdict'];
200
            //         $ret[$row['sid']]="<div style='color:{$color[$v]};'>"  .$sub['Verdict']. "</div>";
201
            //         $this->MODEL->update_submission($row['sid'], $sub);
202
            //     }
203
            //     $j++;
204
            // }
205
            // if ($row['oid']=='Uva') {
206
            //     if (isset($uva_v[$uva[$k][2]])) {
207
            //         $sub['verdict']=$uva_v[$uva[$k][2]];
208
            //         $sub['time']=$uva[$k][0];
209
            //         $sub['memory']=$uva[$k][1];
210
            //         $v=$sub['verdict'];
211
            //         $ret[$row['sid']]="<div style='color:{$color[$v]};'>"  .$sub['Verdict']. "</div>";
212
            //         $this->MODEL->update_submission($row['sid'], $sub);
213
            //     }
214
            //     $k++;
215
            // }
216
            // if ($row['oid']=='UvaLive') {
217
            //     if (isset($uva_v[$uval[$l][2]])) {
218
            //         $sub['verdict']=$uva_v[$uval[$l][2]];
219
            //         $sub['time']=$uval[$l][0];
220
            //         $sub['memory']=$uval[$l][1];
221
            //         $v=$sub['verdict'];
222
            //         $ret[$row['sid']]="<div style='color:{$color[$v]};'>"  .$sub['Verdict']. "</div>";
223
            //         $this->MODEL->update_submission($row['sid'], $sub);
224
            //     }
225
            //     $l++;
226
            // }
227
        }
228
        $this->ret=$ret;
229
    }
230
    /**
231
     * [Not Finished] Get last time UVa submission by using API.
232
     *
233
     * @param integer $num
234
     *
235
     * @return void
236
     */
237
    private function get_last_uva($num)
0 ignored issues
show
Unused Code introduced by
The method get_last_uva() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
238
    {
239
        $ret=array();
240
        if ($num==0) {
241
            return $ret;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $ret returns the type array which is incompatible with the documented return type void.
Loading history...
242
        }
243
        $response=$this->grab_page('https://uva.onlinejudge.org', 'uva');
244
        if (!(strpos($response, 'UVa Online Judge - Offline') !== false)) {
245
            $this->uva_live_login('https://uva.onlinejudge.org', 'https://uva.onlinejudge.org/index.php?option=com_comprofiler&task=login', 'uva');
246
        } else {
247
            return $ret;
248
        }
249
250
        $i=0;
251
        while (true) {
252
            $response=$this->grab_page("https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=9&limit=50&limitstart={$i}", 'uva');
253
254
            $exploded = explode('<table cellpadding="4" cellspacing="0" border="0" width="100%">', $response);
255
            $table = explode('</table>', $exploded[1])[0];
256
257
            $table = explode('<tr class="sectiontableentry', $table);
258
259
            for ($j=1;$j<count($table);$j++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
260
                $num--;
261
                $sub=$table[$j];
262
263
                $sub = explode('<td>', $sub);
264
                $verdict=explode('</td>', $sub[3])[0];
265
                $time=explode('</td>', $sub[5])[0];
266
267
                if ((strpos($verdict, '<a href=') !== false)) {
268
                    $verdict=explode('</a', explode('>', explode('<a href=', $verdict)[1])[1])[0];
269
                }
270
271
                array_push($ret, array($time*1000,-1,$verdict));
272
273
                if ($num==0) {
274
                    return array_reverse($ret);
275
                }
276
            }
277
            $i+=50;
278
        }
279
    }
280
281
    /**
282
     * [Not Finished] Get last time UVa Live submission by using API.
283
     *
284
     * @param integer $num
285
     *
286
     * @return void
287
     */
288
    private function get_last_uvalive($num)
0 ignored issues
show
Unused Code introduced by
The method get_last_uvalive() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
289
    {
290
        $ret=array();
291
        if ($num==0) {
292
            return $ret;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $ret returns the type array which is incompatible with the documented return type void.
Loading history...
293
        }
294
295
        $this->uva_live_login('https://icpcarchive.ecs.baylor.edu', 'https://icpcarchive.ecs.baylor.edu/index.php?option=com_comprofiler&task=login', 'uvalive');
0 ignored issues
show
Bug introduced by
The method uva_live_login() does not exist on App\Http\Controllers\VirtualJudge\Judge. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

295
        $this->/** @scrutinizer ignore-call */ 
296
               uva_live_login('https://icpcarchive.ecs.baylor.edu', 'https://icpcarchive.ecs.baylor.edu/index.php?option=com_comprofiler&task=login', 'uvalive');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
296
297
        $i=0;
298
        while (true) {
299
            $response=$this->grab_page("https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=9&limit=50&limitstart={$i}", 'uvalive');
300
301
            $exploded = explode('<table cellpadding="4" cellspacing="0" border="0" width="100%">', $response);
302
            $table = explode('</table>', $exploded[1])[0];
303
304
            $table = explode('<tr class="sectiontableentry', $table);
305
306
            for ($j=1;$j<count($table);$j++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
307
                $num--;
308
                $sub=$table[$j];
309
310
                $sub = explode('<td>', $sub);
311
                $verdict=explode('</td>', $sub[3])[0];
312
                $time=explode('</td>', $sub[5])[0];
313
314
                if ((strpos($verdict, '<a href=') !== false)) {
315
                    $verdict=explode('</a', explode('>', explode('<a href=', $verdict)[1])[1])[0];
316
                }
317
318
                array_push($ret, array($time*1000,-1,$verdict));
319
320
                if ($num==0) {
321
                    return array_reverse($ret);
322
                }
323
            }
324
            $i+=50;
325
        }
326
    }
327
328
329
    /**
330
     * [Not Finished] Get last time CodeForces submission by using API.
331
     *
332
     * @param integer $num
333
     *
334
     * @return void
335
     */
336
    private function get_last_codeforces($num)
337
    {
338
        $ret=array();
339
        if ($num==0) {
340
            return $ret;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $ret returns the type array which is incompatible with the documented return type void.
Loading history...
341
        }
342
343
        $ch=curl_init();
344
        $url="http://codeforces.com/api/user.status?handle=codemaster4&from=1&count={$num}";
345
        curl_setopt($ch, CURLOPT_URL, $url);
346
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
347
        $response=curl_exec($ch);
348
        curl_close($ch);
349
        $result=json_decode($response, true);
350
        if ($result["status"]=="OK") {
351
            for ($i=0;$i<$num;$i++) {
352
                if (!isset($result["result"][$i]["verdict"])) {
353
                    return array_reverse($ret);
0 ignored issues
show
Bug Best Practice introduced by
The expression return array_reverse($ret) returns the type array which is incompatible with the documented return type void.
Loading history...
354
                }
355
                array_push($ret, array($result["result"][$i]["timeConsumedMillis"],$result["result"][$i]["memoryConsumedBytes"]/1000,$result["result"][$i]["verdict"],$result["result"][$i]["id"]));
356
            }
357
        }
358
        return array_reverse($ret);
0 ignored issues
show
Bug Best Practice introduced by
The expression return array_reverse($ret) returns the type array which is incompatible with the documented return type void.
Loading history...
359
    }
360
361
    /**
362
     * [Not Finished] Get last time SPOJ submission by using API.
363
     *
364
     * @param integer $num
365
     *
366
     * @return void
367
     */
368
    private function get_last_spoj($num)
0 ignored issues
show
Unused Code introduced by
The method get_last_spoj() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
369
    {
370
        $ret=array();
371
        if ($num==0) {
372
            return $ret;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $ret returns the type array which is incompatible with the documented return type void.
Loading history...
373
        }
374
375
        $i=0;
376
        while (true) {
377
            $response=file_get_contents("http://www.spoj.com/status/our_judge/all/start={$i}");
378
379
380
            $exploded = explode('<table class="problems table newstatus">', $response);
381
            $table = explode('</table>', $exploded[1])[0];
382
383
            $table = explode('<td class="statustext text-center">', $table);
384
385
            for ($j=1;$j<count($table);$j++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
386
                $num--;
387
                $sub=$table[$j];
388
389
390
                $verdict=explode('</td>', explode('manual="0">', explode('<td class="statusres text-center"', $sub)[1])[1])[0];
391
                if ((strpos($verdict, '<strong>') !== false)) {
392
                    $verdict=explode('</strong>', explode('<strong>', $verdict)[1])[0];
393
                }
394
395
                if ((strpos($verdict, '(') !== false)) {
396
                    $verdict=explode('(', $verdict)[0];
397
                }
398
                if (is_numeric(trim($verdict))) {
399
                    $verdict='accepted';
400
                }
401
402
                $time=explode('</a>', explode('title="See the best solutions">', $sub)[1])[0];
403
                $time=trim($time);
404
                if ($time=='-') {
405
                    $time=0;
406
                }
407
408
                $memory=explode('</td', explode('>', explode('<td class="smemory statustext text-center"', $sub)[1])[1])[0];
409
                $memory=trim($memory);
410
                if ($memory=='-') {
411
                    $memory=0;
412
                } else {
413
                    $memory=substr($memory, 0, strlen($memory)-1);
414
                }
415
416
                array_push($ret, array($time*1000,$memory*1000,trim($verdict)));
417
                if ($num==0) {
418
                    return array_reverse($ret);
0 ignored issues
show
Bug Best Practice introduced by
The expression return array_reverse($ret) returns the type array which is incompatible with the documented return type void.
Loading history...
419
                }
420
            }
421
            $i+=20;
422
        }
423
    }
424
425
    private function appendPOJStatus(&$results, $judger, $first = null)
426
    {
427
        if ($first !== null) $first++;
428
        $res = Requests::get("http://poj.org/status?user_id={$judger}&top={$first}");
429
        $rows = preg_match_all('/<tr align=center><td>(\d+)<\/td><td>.*?<\/td><td>.*?<\/td><td>.*?<font color=.*?>(.*?)<\/font>.*?<\/td><td>(\d*)K?<\/td><td>(\d*)(?:MS)?<\/td>/', $res->body, $matches);
430
        for ($i = 0; $i < $rows; $i++) {
431
            $results[$matches[1][$i]] = [
432
                'verdict'=>$matches[2][$i],
433
                'memory'=>$matches[3][$i] ? $matches[3][$i] : 0,
434
                'time'=>$matches[4][$i] ? $matches[4][$i] : 0,
435
            ];
436
        }
437
    }
438
}
439