Test Setup Failed
Push — master ( 2ac834...d208c1 )
by
unknown
04:34
created

Vijos::crawling()   F

Complexity

Conditions 31
Paths 3900

Size

Total Lines 132
Code Lines 102

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 31
eloc 102
nc 3900
nop 1
dl 0
loc 132
rs 0
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
3
namespace App\Http\Controllers\VirtualCrawler\Vijos;
4
5
use App\Http\Controllers\VirtualCrawler\CrawlerBase;
6
use App\Models\ProblemModel;
7
use KubAT\PhpSimple\HtmlDomParser;
8
use Auth,Requests,Exception;
9
10
class Vijos extends CrawlerBase
11
{
12
    public $oid=5;
13
    private $con, $imgi;
0 ignored issues
show
introduced by
The private property $imgi is not used, and could be removed.
Loading history...
introduced by
The private property $con is not used, and could be removed.
Loading history...
14
    /**
15
     * Initial
16
     *
17
     * @return Response
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\VirtualCrawler\Vijos\Response was not found. Did you mean Response? If so, make sure to prefix the type with \.
Loading history...
18
     */
19
    public function __construct($action = 'crawl_problem', $con = 'all', $cached = false)
0 ignored issues
show
Unused Code introduced by
The parameter $cached is not used and could be removed. ( Ignorable by Annotation )

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

19
    public function __construct($action = 'crawl_problem', $con = 'all', /** @scrutinizer ignore-unused */ $cached = false)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
20
    {
21
        set_time_limit(0); // Pandora's box, engage!
22
        if ($action=='judge_level') {
23
            $this->judge_level();
24
        } else {
25
            $this->crawling($con);
26
        }
27
    }
28
29
    public function judge_level()
30
    {
31
        // TODO
32
    }
33
34
    public function crawling($con)
35
    {
36
        if ($con == 'all') {
37
            // TODO
38
            return;
39
        }
40
41
        try {
42
            $dom = HtmlDomParser::file_get_html('https://vijos.org/p/'.$con, false, null, 0, -1, true, true, DEFAULT_TARGET_CHARSET, false);
43
        }
44
        catch (Exception $e) {
45
            if (strpos($e->getMessage(), '404 Not Found') !== false) {
46
                header('HTTP/1.1 404 Not Found');
47
                die();
48
            }
49
            if (strpos($e->getMessage(), '403 Forbidden') !== false) {
50
                header('HTTP/1.1 403 Forbidden');
51
                die();
52
            }
53
            throw $e;
54
        }
55
56
        $mainDiv = $dom->find(".section__body", 0);
57
58
        $eles = $mainDiv->children();
59
        array_push($eles, null);
60
        $this->pro['description'] = null;
61
        $this->pro['input'] = null;
62
        $this->pro['output'] = null;
63
        $this->pro['sample'] = [];
64
        $this->pro['note'] = null;
65
        $this->pro['sampleDesc'] = null;
66
        $this->pro['limit'] = null;
67
        $patterns = [
68
            'description' => '<h1>描述</h1>',
69
            '_format' => '<h1>格式</h1>',
70
            'input' => '<h2>输入格式</h2>',
71
            'output' => '<h2>输出格式</h2>',
72
            '_sample' => '/^<h1>样例\d+<\/h1>$/u',
73
            '__sampleInput' => '/^<h2>样例输入\d+<\/h2>$/u',
74
            '__sampleOutput' => '/^<h2>样例输出\d+<\/h2>$/u',
75
            'limit' => '<h1>限制</h1>',
76
            'note' => '<h1>提示</h1>',
77
            'sampleDesc' => '/<h1>样例(说明|解释)<\/h1>|<h2>样例说明1<\/h2>/', // P2036 has <h2>样例说明1</h2>
78
            'source' => '<h1>来源</h1>',
79
        ];
80
        $lastPart = '';
81
        $content = '';
82
        $cursample = [];
83
        foreach ($eles as $ele) {
84
            $html = $ele ? $ele->outertext : null;
85
            $match = !$ele;
86
            if (!$match) {
87
                foreach ($patterns as $key=>$value) {
88
                    if ($value[0] != '/' && $html == $value || $value[0] == '/' && preg_match($value, $html)) {
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: ($value[0] != '/' && $ht...eg_match($value, $html), Probably Intended Meaning: $value[0] != '/' && ($ht...g_match($value, $html))
Loading history...
89
                        $match = $key;
90
                        break;
91
                    }
92
                }
93
            }
94
            if (!$lastPart) {
95
                if ($match) $lastPart = $match;
96
                continue;
97
            }
98
            if ($match) {
99
                if ($lastPart[0] != '_') {
100
                    $this->pro[$lastPart] = $content;
101
                    $content = '';
102
                } else if ($lastPart == '__sampleOutput') { // Assume output always follows by input
103
                    array_push($this->pro['sample'], $cursample);
104
                    $cursample = [];
105
                }
106
                $lastPart = $match;
107
            } else {
108
                if ($lastPart[1] != '_') {
109
                    if ($lastPart != 'source') $content .= $html;
110
                    else $content .= $ele->innertext;
111
                } else { // Code
112
                    $code = trim($ele->find('code', 0)->innertext);
113
                    if ($lastPart == '__sampleInput') { if (isset($cursample['sampleInput'])) die($con); }
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
114
                    else { if (isset($cursample['sampleOutput'])) die($con); }
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
115
                    if (count($ele->children()) != 1) die($con);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
116
                    if ($lastPart == '__sampleInput') $cursample['sample_input'] = $code;
117
                    else $cursample['sample_output'] = $code;
118
                }
119
            }
120
            if (!$ele) break;
121
        }
122
123
        $this->pro['time_limit'] = 1000;
124
        $this->pro['memory_limit'] = 262144;
125
        if ($this->pro['sampleDesc']) {
126
            $this->pro['note'] = '<h3>样例说明</h3>'.$this->pro['sampleDesc'].$this->pro['note'];
0 ignored issues
show
Bug introduced by
Are you sure $this->pro['sampleDesc'] of type void can be used in concatenation? ( Ignorable by Annotation )

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

126
            $this->pro['note'] = '<h3>样例说明</h3>'./** @scrutinizer ignore-type */ $this->pro['sampleDesc'].$this->pro['note'];
Loading history...
127
        }
128
        if ($this->pro['limit']) {
129
            $this->pro['note'] = $this->pro['limit'].$this->pro['note'];
130
            $this->pro['time_limit'] = 0;
131
            $this->pro['memory_limit'] = 0;
132
        }
133
134
        $this->pro['pcode'] = 'VIJ'.$con;
135
        $this->pro['OJ'] = $this->oid;
136
        $this->pro['contest_id'] = null;
137
        $this->pro['index_id'] = $con;
138
        $this->pro['origin'] = 'https://vijos.org/p/'.$con;
139
        $this->pro['title'] = $dom->find('.section__header', 0)->find('h1', 0)->innertext;
140
        $this->pro['input_type'] = 'standard input';
141
        $this->pro['output_type'] = 'standard output';
142
143
        $this->pro['markdown'] = 0;
144
        $this->pro['tot_score'] = 100;
145
        $this->pro["partial"] = 1;
146
        $this->pro['source'] = 'Vijos'; // Force Override
147
148
        $info = $dom->find(".horizontal", 0);
149
        preg_match('/<dt>已通过<\/dt>[\s\S]*<dd>(\d+)<\/dd>/', $info->innertext, $match);
150
        $this->pro['solved_count'] = $match[1];
151
152
        $problemModel=new ProblemModel();
153
        $problem=$problemModel->pid($this->pro['pcode']);
154
155
        if ($problem) {
156
            $problemModel->clearTags($problem);
157
            $new_pid=$this->update_problem($this->oid);
158
        } else {
159
            $new_pid=$this->insert_problem($this->oid);
160
        }
161
162
        $tags = $info->find('.hasjs--hide', 0);
163
        if ($tags) {
164
            foreach ($tags->find('a') as $tag) {
165
                $problemModel->addTags($new_pid, $tag->innertext);
166
            }
167
        }
168
    }
169
}
170