Passed
Push — master ( 609799...41987d )
by John
04:18
created

Judge   F

Complexity

Total Complexity 82

Size/Duplication

Total Lines 561
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 345
dl 0
loc 561
rs 2
c 0
b 0
f 0
wmc 82

6 Methods

Rating   Name   Duplication   Size   Complexity  
A get_last_codeforces() 0 27 5
B get_last_spoj() 0 54 10
F __construct() 0 371 53
A get_last_uva() 0 16 3
A appendPOJStatus() 0 12 5
B get_last_uvalive() 0 37 6

How to fix   Complexity   

Complex Class

Complex classes like Judge often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Judge, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace App\Babel\Judger;
3
4
use App\Models\SubmissionModel;
5
use App\Models\JudgerModel;
6
use App\Models\ContestModel;
7
use App\Http\Controllers\VirtualJudge\Core;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, App\Babel\Judger\Core. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
8
use Requests;
9
use Exception;
10
11
class Judge extends Core
12
{
13
    private $MODEL;
14
    public $ret=[];
15
    public function __construct()
16
    {
17
        $this->MODEL=new SubmissionModel();
18
        $ret=[];
19
20
        $uva_v=[
21
            10=>'Submission Error',
22
            15=>'Submission Error', // Can't be judged
23
            // 20 In queue
24
            30=>"Compile Error",
25
            35=>"Compile Error", // Restricted function
26
            40=>"Runtime Error",
27
            45=>"Output Limit Exceeded",
28
            50=>"Time Limit Exceed",
29
            60=>"Memory Limit Exceed",
30
            70=>"Wrong Answer",
31
            80=>"Presentation Error",
32
            90=>"Accepted",
33
        ];
34
35
        $codeforces_v=[
36
            "COMPILATION_ERROR"=>"Compile Error",
37
            "RUNTIME_ERROR"=> "Runtime Error",
38
            "WRONG_ANSWER"=> "Wrong Answer",
39
            "TIME_LIMIT_EXCEEDED"=>"Time Limit Exceed",
40
            "OK"=>"Accepted",
41
            "MEMORY_LIMIT_EXCEEDED"=>"Memory Limit Exceed",
42
            "PRESENTATION_ERROR"=>"Presentation Error",
43
            "IDLENESS_LIMIT_EXCEEDED"=>"Idleness Limit Exceed"
44
        ];
45
46
        $spoj_v=[
0 ignored issues
show
Unused Code introduced by
The assignment to $spoj_v is dead and can be removed.
Loading history...
47
            "compilation error"=>"Compile Error",
48
            "runtime error"=> "Runtime Error",
49
            "wrong answer"=> "Wrong Answer",
50
            "time limit exceeded"=>"Time Limit Exceed",
51
            "accepted"=>"Accepted"
52
        ];
53
54
        $contesthunter_v=[
55
            '正确'=>"Accepted",
56
            '答案错误'=>"Wrong Answer",
57
            '超出时间限制'=>"Time Limit Exceed",
58
            '运行时错误'=>"Runtime Error",
59
            "超出内存限制"=>"Memory Limit Exceed",
60
            '比较器错误'=>'Submission Error',
61
            '超出输出限制'=>"Output Limit Exceeded",
62
            '编译错误'=>"Compile Error",
63
        ];
64
65
        $poj_v=[
66
            'Accepted'=>"Accepted",
67
            "Presentation Error"=>"Presentation Error",
68
            'Time Limit Exceeded'=>"Time Limit Exceed",
69
            "Memory Limit Exceeded"=>"Memory Limit Exceed",
70
            'Wrong Answer'=>"Wrong Answer",
71
            'Runtime Error'=>"Runtime Error",
72
            'Output Limit Exceeded'=>"Output Limit Exceeded",
73
            'Compile Error'=>"Compile Error",
74
        ];
75
76
        $vijos_v=[
77
            'Accepted'=>"Accepted",
78
            'Wrong Answer'=>"Wrong Answer",
79
            'Time Exceeded'=>"Time Limit Exceed",
80
            "Memory Exceeded"=>"Memory Limit Exceed",
81
            'Runtime Error'=>"Runtime Error",
82
            'Compile Error'=>"Compile Error",
83
            'System Error'=>"Submission Error",
84
            'Canceled'=>"Submission Error",
85
            'Unknown Error'=>"Submission Error",
86
            'Ignored'=>"Submission Error",
87
        ];
88
89
        $pta_v=[
90
            'ACCEPTED'=>"Accepted",
91
            'COMPILE_ERROR'=>"Compile Error",
92
            'FLOAT_POINT_EXCEPTION'=>"Runtime Error",
93
            'INTERNAL_ERROR'=>"Submission Error",
94
            "MEMORY_LIMIT_EXCEEDED"=>"Memory Limit Exceed",
95
            'MULTIPLE_ERROR'=>"Runtime Error",
96
            'NON_ZERO_EXIT_CODE'=>"Runtime Error",
97
            'NO_ANSWER'=>"Compile Error",
98
            'OUTPUT_LIMIT_EXCEEDED'=>"Output Limit Exceeded",
99
            'OVERRIDDEN'=>"Submission Error",
100
            'PARTIAL_ACCEPTED'=>"Partially Accepted",
101
            "PRESENTATION_ERROR"=>"Presentation Error",
102
            'RUNTIME_ERROR'=>"Runtime Error",
103
            'SAMPLE_ERROR'=>"Wrong Answer",
104
            'SEGMENTATION_FAULT'=>"Runtime Error",
105
            'SKIPPED'=>"Submission Error",
106
            'TIME_LIMIT_EXCEEDED'=>"Time Limit Exceed",
107
            'WRONG_ANSWER'=>"Wrong Answer",
108
        ];
109
110
        $result=$this->MODEL->getWaitingSubmission();
111
        $judger=new JudgerModel();
112
        $contestModel=new ContestModel();
113
        $curl=new Curl();
114
115
        $cfList=$this->get_last_codeforces($this->MODEL->countEarliestWaitingSubmission(2)+100);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $cfList is correct as $this->get_last_codeforc...ingSubmission(2) + 100) targeting App\Babel\Judger\Judge::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...
116
        $uvaList=$this->get_last_uva($this->MODEL->getEarliestSubmission(7));
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $uvaList is correct as $this->get_last_uva($thi...tEarliestSubmission(7)) targeting App\Babel\Judger\Judge::get_last_uva() 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...
117
        $poj=[];
118
119
        $pojJudgerList=$judger->list(4);
120
        $pojJudgerName=urlencode($pojJudgerList[0]["handle"]);
121
        if ($this->MODEL->countWaitingSubmission(5)) {
122
            $this->appendPOJStatus($poj, $pojJudgerName);
123
        }
124
        // $uva=$this->get_last_uva($this->MODEL->countWaitingSubmission('Uva'));
125
        // $uval=$this->get_last_uvalive($this->MODEL->countWaitingSubmission('UvaLive'));
126
        // $sj=$this->get_last_spoj($this->MODEL->countWaitingSubmission('Spoj'));
127
128
        $i=0;
0 ignored issues
show
Unused Code introduced by
The assignment to $i is dead and can be removed.
Loading history...
129
        $j=0;
0 ignored issues
show
Unused Code introduced by
The assignment to $j is dead and can be removed.
Loading history...
130
        $k=0;
0 ignored issues
show
Unused Code introduced by
The assignment to $k is dead and can be removed.
Loading history...
131
        $l=0;
0 ignored issues
show
Unused Code introduced by
The assignment to $l is dead and can be removed.
Loading history...
132
        foreach ($result as $row) {
133
            if ($row['oid']==2) {
134
                $cf=[];
135
                foreach ($cfList as $c) {
0 ignored issues
show
Bug introduced by
The expression $cfList of type void is not traversable.
Loading history...
136
                    if ($c[3]==$row["remote_id"]) {
137
                        $cf=$c;
138
                        break;
139
                    }
140
                }
141
                if (empty($cf)) {
142
143
                    // $this->MODEL->updateSubmission($row['sid'], ['verdict'=>"Submission Error"]);
144
                } else {
145
                    if (isset($codeforces_v[$cf[2]])) {
146
                        $sub=[];
147
                        $sub['verdict']=$codeforces_v[$cf[2]];
148
                        if ($sub['verdict']=='Compile Error') {
149
                            if (!isset($cfCSRF)) {
150
                                $cfCSRF=[];
151
                            }
152
                            $handle=$judger->detail($row['jid'])['handle'];
153
                            if (!isset($cfCSRF[$handle])) {
154
                                $res=$curl->grab_page('http://codeforces.com', 'codeforces', [], $handle);
155
                                preg_match('/<meta name="X-Csrf-Token" content="([0-9a-z]*)"/', $res, $match);
156
                                $cfCSRF[$handle]=$match[1];
157
                            }
158
                            $res=$curl->post_data('http://codeforces.com/data/judgeProtocol', ['submissionId'=>$row['remote_id'], 'csrf_token'=>$cfCSRF[$handle]], 'codeforces', true, false, false, false, [], $handle);
159
                            $sub['compile_info']=json_decode($res);
160
                        }
161
                        $sub["score"]=$sub['verdict']=="Accepted" ? 1 : 0;
162
                        $sub['time']=$cf[0];
163
                        $sub['memory']=$cf[1];
164
                        $sub['remote_id']=$cf[3];
165
166
                        $ret[$row['sid']]=[
167
                            "verdict"=>$sub['verdict']
168
                        ];
169
170
                        $this->MODEL->updateSubmission($row['sid'], $sub);
171
                    }
172
                }
173
174
                // if (isset($codeforces_v[$cf[$i][2]])) {
175
                //     $sub['verdict']=$codeforces_v[$cf[$i][2]];
176
                //     $sub["score"]=$sub['verdict']=="Accepted" ? 1 : 0;
177
                //     $sub['time']=$cf[$i][0];
178
                //     $sub['memory']=$cf[$i][1];
179
                //     $sub['remote_id']=$cf[$i][3];
180
181
                //     $ret[$row['sid']]=[
182
                //         "verdict"=>$sub['verdict']
183
                //     ];
184
185
                //     $this->MODEL->updateSubmission($row['sid'], $sub);
186
                // }
187
                // $i++;
188
            } elseif ($row['oid']==3) {
189
                try {
190
                    $sub=[];
191
                    $res=Requests::get('http://contest-hunter.org:83/record/'.$row['remote_id']);
192
                    preg_match('/<dt>状态<\/dt>[\s\S]*?<dd class=".*?">(.*?)<\/dd>/m', $res->body, $match);
193
                    $status=$match[1];
194
                    if (!array_key_exists($status, $contesthunter_v)) {
195
                        continue;
196
                    }
197
                    $sub['verdict']=$contesthunter_v[$status];
198
                    $sub["score"]=$sub['verdict']=="Accepted" ? 1 : 0;
199
                    $sub['remote_id']=$row['remote_id'];
200
                    if ($sub['verdict']!="Submission Error" && $sub['verdict']!="Compile Error") {
201
                        preg_match('/占用内存[\s\S]*?(\d+).*?KiB/m', $res->body, $match);
202
                        $sub['memory']=$match[1];
203
                        $maxtime=0;
204
                        preg_match_all('/<span class="pull-right muted">(\d+) ms \/ \d+ KiB<\/span>/', $res->body, $matches);
205
                        foreach ($matches[1] as $time) {
206
                            if ($time<$maxtime) {
207
                                $maxtime=$time;
208
                            }
209
                        }
210
                        $sub['time']=$maxtime;
211
                    } else {
212
                        $sub['memory']=0;
213
                        $sub['time']=0;
214
                        if ($sub['verdict']=='Compile Error') {
215
                            preg_match('/<h2>结果 <small>各个测试点的详细结果<\/small><\/h2>\s*<pre>([\s\S]*?)<\/pre>/', $res->body, $match);
216
                            $sub['compile_info']=html_entity_decode($match[1], ENT_QUOTES);
217
                        }
218
                    }
219
220
                    $ret[$row['sid']]=[
221
                        "verdict"=>$sub['verdict']
222
                    ];
223
                    $this->MODEL->updateSubmission($row['sid'], $sub);
224
                } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
225
                }
226
            } elseif ($row['oid']==4) {
227
                $sub=[];
228
                if (!isset($poj[$row['remote_id']])) {
229
                    $this->appendPOJStatus($poj, $pojJudgerName, $row['remote_id']);
230
                    if (!isset($poj[$row['remote_id']])) {
231
                        continue;
232
                    }
233
                }
234
                $status=$poj[$row['remote_id']];
235
                $sub['verdict']=$poj_v[$status['verdict']];
236
                if ($sub['verdict']=='Compile Error') {
237
                    try {
238
                        $res=Requests::get('http://poj.org/showcompileinfo?solution_id='.$row['remote_id']);
239
                        preg_match('/<pre>([\s\S]*)<\/pre>/', $res->body, $match);
240
                        $sub['compile_info']=html_entity_decode($match[1], ENT_QUOTES);
241
                    } catch (Exception $e) {}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
242
                }
243
                $sub["score"]=$sub['verdict']=="Accepted" ? 1 : 0;
244
                $sub['time']=$status['time'];
245
                $sub['memory']=$status['memory'];
246
                $sub['remote_id']=$row['remote_id'];
247
248
                $ret[$row['sid']]=[
249
                    "verdict"=>$sub['verdict']
250
                ];
251
                $this->MODEL->updateSubmission($row['sid'], $sub);
252
            } elseif ($row['oid']==5) {
253
                try {
254
                    $sub=[];
255
                    $res=Requests::get('https://vijos.org/records/'.$row['remote_id']);
256
                    preg_match('/<span class="record-status--text \w*">\s*(.*?)\s*<\/span>/', $res->body, $match);
257
                    $status=$match[1];
258
                    if (!array_key_exists($status, $vijos_v)) {
259
                        continue;
260
                    }
261
                    if ($match[1]=='Compile Error') {
262
                        preg_match('/<pre class="compiler-text">([\s\S]*?)<\/pre>/', $res->body, $match);
263
                        $sub['compile_info']=html_entity_decode($match[1], ENT_QUOTES);
264
                    }
265
                    $sub['verdict']=$vijos_v[$status];
266
                    preg_match('/<dt>分数<\/dt>\s*<dd>(\d+)<\/dd>/', $res->body, $match);
267
                    $isOI=$row['cid'] && $contestModel->rule($row['cid'])==2;
268
                    if ($isOI) {
269
                        $sub['score']=$match[1];
270
                        if ($sub['verdict']=="Wrong Answer" && $sub['score']!=0) {
271
                            $sub['verdict']='Partially Accepted';
272
                        }
273
                    } else {
274
                        $sub['score']=$match[1]==100 ? 100 : 0;
275
                    }
276
                    $sub['remote_id']=$row['remote_id'];
277
                    if ($sub['verdict']!="Submission Error" && $sub['verdict']!="Compile Error") {
278
                        $maxtime=0;
279
                        preg_match_all('/<td class="col--time">(?:&ge;)?(\d+)ms<\/td>/', $res->body, $matches);
280
                        foreach ($matches as $match) {
281
                            if ($match[1]>$maxtime) {
282
                                $maxtime=$match[1];
283
                            }
284
                        }
285
                        $sub['time']=$maxtime;
286
                        preg_match('/<dt>峰值内存<\/dt>\s*<dd>(?:&ge;)?([\d.]+) ([KM])iB<\/dd>/', $res->body, $match);
287
                        $memory=$match[1];
288
                        if ($match[2]=='M') {
289
                            $memory*=1024;
290
                        }
291
                        $sub['memory']=intval($memory);
292
                    } else {
293
                        $sub['memory']=0;
294
                        $sub['time']=0;
295
                    }
296
297
                    $ret[$row['sid']]=[
298
                        "verdict"=>$sub['verdict']
299
                    ];
300
                    $this->MODEL->updateSubmission($row['sid'], $sub);
301
                } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
302
                }
303
            } elseif ($row['oid']==6) {
304
                try {
305
                    $sub=[];
306
                    $response=$curl->grab_page("https://pintia.cn/api/submissions/".$row['remote_id'], 'pta', ['Accept: application/json;charset=UTF-8']);
307
                    $data=json_decode($response, true);
308
                    if (!isset($pta_v[$data['submission']['status']])) {
309
                        continue;
310
                    }
311
                    $sub['verdict']=$pta_v[$data['submission']['status']];
312
                    if ($data['submission']['status']=='COMPILE_ERROR') {
313
                        $sub['compile_info']=$data['submission']['judgeResponseContents'][0]['programmingJudgeResponseContent']['compilationResult']['log'];
314
                    }
315
                    $isOI=$row['cid'] && $contestModel->rule($row['cid'])==2;
316
                    $sub['score']=$data['submission']['score'];
317
                    if (!$isOI) {
318
                        if ($sub['verdict']=="Partially Accepted") {
319
                            $sub['verdict']='Wrong Answer';
320
                            $sub['score']=0;
321
                        }
322
                    }
323
                    $sub['remote_id']=$row['remote_id'];
324
                    $sub['memory']=$data['submission']['memory'] / 1024;
325
                    $sub['time']=$data['submission']['time'] * 1000;
326
327
                    $ret[$row['sid']]=[
328
                        "verdict"=>$sub['verdict']
329
                    ];
330
                    $this->MODEL->updateSubmission($row['sid'], $sub);
331
                } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
332
                }
333
            } elseif ($row['oid']==7) {
334
                if (array_key_exists($row['remote_id'], $uvaList)) {
0 ignored issues
show
Bug introduced by
$uvaList of type void is incompatible with the type array expected by parameter $search of array_key_exists(). ( Ignorable by Annotation )

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

334
                if (array_key_exists($row['remote_id'], /** @scrutinizer ignore-type */ $uvaList)) {
Loading history...
335
                    $sub=[];
336
                    $sub['verdict']=$uva_v[$uvaList[$row['remote_id']]['verdict']];
337
                    if ($sub['verdict']==='Compile Error') {
338
                        $response=$this->grab_page("https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=9&page=show_compilationerror&submission=$row[remote_id]", 'uva', [], $uvaList['handle']);
339
                        if (preg_match('/<pre>([\s\S]*)<\/pre>/', $response, $match)) $sub['compile_info']=trim($match[1]);
340
                    }
341
                    $sub['score']=$sub['verdict']=="Accepted" ? 1 : 0;
342
                    $sub['remote_id']=$row['remote_id'];
343
                    $sub['time']=$uvaList[$row['remote_id']]['time'];
344
345
                    $ret[$row['sid']]=[
346
                        "verdict"=>$sub['verdict']
347
                    ];
348
                    $this->MODEL->updateSubmission($row['sid'], $sub);
349
                }
350
            }
351
            // if ($row['oid']=='Spoj') {
352
            //     if (isset($spoj_v[$sj[$j][2]])) {
353
            //         $sub['verdict']=$spoj_v[$sj[$j][2]];
354
            //         $sub['time']=$sj[$j][0];
355
            //         $sub['memory']=$sj[$j][1];
356
            //         $v=$sub['verdict'];
357
            //         $ret[$row['sid']]="<div style='color:{$color[$v]};'>"  .$sub['Verdict']. "</div>";
358
            //         $this->MODEL->updateSubmission($row['sid'], $sub);
359
            //     }
360
            //     $j++;
361
            // }
362
            // if ($row['oid']=='Uva') {
363
            //     if (isset($uva_v[$uva[$k][2]])) {
364
            //         $sub['verdict']=$uva_v[$uva[$k][2]];
365
            //         $sub['time']=$uva[$k][0];
366
            //         $sub['memory']=$uva[$k][1];
367
            //         $v=$sub['verdict'];
368
            //         $ret[$row['sid']]="<div style='color:{$color[$v]};'>"  .$sub['Verdict']. "</div>";
369
            //         $this->MODEL->updateSubmission($row['sid'], $sub);
370
            //     }
371
            //     $k++;
372
            // }
373
            // if ($row['oid']=='UvaLive') {
374
            //     if (isset($uva_v[$uval[$l][2]])) {
375
            //         $sub['verdict']=$uva_v[$uval[$l][2]];
376
            //         $sub['time']=$uval[$l][0];
377
            //         $sub['memory']=$uval[$l][1];
378
            //         $v=$sub['verdict'];
379
            //         $ret[$row['sid']]="<div style='color:{$color[$v]};'>"  .$sub['Verdict']. "</div>";
380
            //         $this->MODEL->updateSubmission($row['sid'], $sub);
381
            //     }
382
            //     $l++;
383
            // }
384
        }
385
        $this->ret=$ret;
386
    }
387
    /**
388
     * [Not Finished] Get last time UVa submission by using API.
389
     *
390
     * @param integer $num
391
     *
392
     * @return void
393
     */
394
    private function get_last_uva($earliest)
395
    {
396
        $ret = [];
397
        if (!$earliest) 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...
398
399
        $judger=new JudgerModel();
400
        $judgerDetail=$judger->detail($earliest['jid']);
401
        $ret['handle']=$judgerDetail['handle'];
402
403
        $response=$this->grab_page("https://uhunt.onlinejudge.org/api/subs-user/".$judgerDetail['user_id']."/".($earliest['remote_id']-1), 'uva', [], $judgerDetail['handle']);
404
        $result=json_decode($response, true);
405
        foreach ($result['subs'] as $i) {
406
            $ret[$i[0]] = ['time'=>$i[3], 'verdict'=>$i[2]];
407
        }
408
409
        return $ret;
410
    }
411
412
    /**
413
     * [Not Finished] Get last time UVa Live submission by using API.
414
     *
415
     * @param integer $num
416
     *
417
     * @return void
418
     */
419
    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...
420
    {
421
        $ret=array();
422
        if ($num==0) {
423
            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...
424
        }
425
426
        $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\Babel\Judger\Judge. ( Ignorable by Annotation )

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

426
        $this->/** @scrutinizer ignore-call */ 
427
               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...
427
428
        $i=0;
429
        while (true) {
430
            $response=$this->grab_page("https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=9&limit=50&limitstart={$i}", 'uvalive');
431
432
            $exploded=explode('<table cellpadding="4" cellspacing="0" border="0" width="100%">', $response);
433
            $table=explode('</table>', $exploded[1])[0];
434
435
            $table=explode('<tr class="sectiontableentry', $table);
436
437
            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...
438
                $num--;
439
                $sub=$table[$j];
440
441
                $sub=explode('<td>', $sub);
442
                $verdict=explode('</td>', $sub[3])[0];
443
                $time=explode('</td>', $sub[5])[0];
444
445
                if ((strpos($verdict, '<a href=')!==false)) {
446
                    $verdict=explode('</a', explode('>', explode('<a href=', $verdict)[1])[1])[0];
447
                }
448
449
                array_push($ret, array($time * 1000, -1, $verdict));
450
451
                if ($num==0) {
452
                    return array_reverse($ret);
453
                }
454
            }
455
            $i+=50;
456
        }
457
    }
458
459
460
    /**
461
     * [Not Finished] Get last time CodeForces submission by using API.
462
     *
463
     * @param integer $num
464
     *
465
     * @return void
466
     */
467
    private function get_last_codeforces($num)
468
    {
469
        $ret=array();
470
        if ($num==0) {
471
            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...
472
        }
473
474
        $judger=new JudgerModel();
475
        $judger_list=$judger->list(2);
476
        $judgerName=$judger_list[array_rand($judger_list)]['handle'];
477
478
        $ch=curl_init();
479
        $url="http://codeforces.com/api/user.status?handle={$judgerName}&from=1&count={$num}";
480
        curl_setopt($ch, CURLOPT_URL, $url);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

480
        curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_URL, $url);
Loading history...
481
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
482
        $response=curl_exec($ch);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

482
        $response=curl_exec(/** @scrutinizer ignore-type */ $ch);
Loading history...
483
        curl_close($ch);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_close() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

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

483
        curl_close(/** @scrutinizer ignore-type */ $ch);
Loading history...
484
        $result=json_decode($response, true);
485
        if ($result["status"]=="OK") {
486
            for ($i=0; $i<$num; $i++) {
487
                if (!isset($result["result"][$i]["verdict"])) {
488
                    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...
489
                }
490
                array_push($ret, array($result["result"][$i]["timeConsumedMillis"], $result["result"][$i]["memoryConsumedBytes"] / 1000, $result["result"][$i]["verdict"], $result["result"][$i]["id"]));
491
            }
492
        }
493
        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...
494
    }
495
496
    /**
497
     * [Not Finished] Get last time SPOJ submission by using API.
498
     *
499
     * @param integer $num
500
     *
501
     * @return void
502
     */
503
    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...
504
    {
505
        $ret=array();
506
        if ($num==0) {
507
            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...
508
        }
509
510
        $i=0;
511
        while (true) {
512
            $response=file_get_contents("http://www.spoj.com/status/our_judge/all/start={$i}");
513
514
515
            $exploded=explode('<table class="problems table newstatus">', $response);
516
            $table=explode('</table>', $exploded[1])[0];
517
518
            $table=explode('<td class="statustext text-center">', $table);
519
520
            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...
521
                $num--;
522
                $sub=$table[$j];
523
524
525
                $verdict=explode('</td>', explode('manual="0">', explode('<td class="statusres text-center"', $sub)[1])[1])[0];
526
                if ((strpos($verdict, '<strong>')!==false)) {
527
                    $verdict=explode('</strong>', explode('<strong>', $verdict)[1])[0];
528
                }
529
530
                if ((strpos($verdict, '(')!==false)) {
531
                    $verdict=explode('(', $verdict)[0];
532
                }
533
                if (is_numeric(trim($verdict))) {
534
                    $verdict='accepted';
535
                }
536
537
                $time=explode('</a>', explode('title="See the best solutions">', $sub)[1])[0];
538
                $time=trim($time);
539
                if ($time=='-') {
540
                    $time=0;
541
                }
542
543
                $memory=explode('</td', explode('>', explode('<td class="smemory statustext text-center"', $sub)[1])[1])[0];
544
                $memory=trim($memory);
545
                if ($memory=='-') {
546
                    $memory=0;
547
                } else {
548
                    $memory=substr($memory, 0, strlen($memory)-1);
549
                }
550
551
                array_push($ret, array($time * 1000, $memory * 1000, trim($verdict)));
552
                if ($num==0) {
553
                    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...
554
                }
555
            }
556
            $i+=20;
557
        }
558
    }
559
560
    private function appendPOJStatus(&$results, $judger, $first=null)
561
    {
562
        if ($first!==null) {
563
            $first++;
564
        }
565
        $res=Requests::get("http://poj.org/status?user_id={$judger}&top={$first}");
566
        $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);
567
        for ($i=0; $i<$rows; $i++) {
568
            $results[$matches[1][$i]]=[
569
                'verdict'=>$matches[2][$i],
570
                'memory'=>$matches[3][$i] ? $matches[3][$i] : 0,
571
                'time'=>$matches[4][$i] ? $matches[4][$i] : 0,
572
            ];
573
        }
574
    }
575
}
576