CrawlerBase::_cacheImage()   B
last analyzed

Complexity

Conditions 11
Paths 3

Size

Total Lines 50
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 40
nc 3
nop 1
dl 0
loc 50
rs 7.3166
c 0
b 0
f 0

How to fix   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\Babel\Crawl;
4
5
use App\Models\ProblemModel;
6
use KubAT\PhpSimple\HtmlDomParser;
7
use Auth;
8
use Exception;
9
10
class CrawlerBase
11
{
12
    public $pro=[
13
        'file'=> 0,
14
        'file_url'=> null,
15
        'pcode'=>'',
16
        'solved_count'=>'',
17
        'time_limit'=>'',
18
        'memory_limit'=>'',
19
        'title'=>'',
20
        'OJ'=>'',
21
        'description'=>'',
22
        'input'=>'',
23
        'output'=>'',
24
        'note'=>'',
25
        'input_type'=>'',
26
        'output_type'=>'',
27
        'contest_id'=>'',
28
        'index_id'=>'',
29
        'origin'=>'',
30
        'source'=>'',
31
        'sample'=>[],
32
        'markdown'=>0,
33
        'tot_score'=>1,
34
        'partial'=>0,
35
        'special_compiler'=>null,
36
    ];
37
38
    public $data=null;
39
    public $command=null;
40
41
    /**
42
     * Initial
43
     *
44
     * @return Response
0 ignored issues
show
Bug introduced by
The type App\Babel\Crawl\Response was not found. Did you mean Response? If so, make sure to prefix the type with \.
Loading history...
45
     */
46
    public function __construct()
47
    {
48
    }
49
50
    protected function _resetPro()
51
    {
52
        $this->pro=[
53
            'file'=> 0,
54
            'pcode'=>'',
55
            'solved_count'=>'',
56
            'time_limit'=>'',
57
            'memory_limit'=>'',
58
            'title'=>'',
59
            'OJ'=>'',
60
            'description'=>'',
61
            'input'=>'',
62
            'output'=>'',
63
            'note'=>'',
64
            'input_type'=>'',
65
            'output_type'=>'',
66
            'contest_id'=>'',
67
            'index_id'=>'',
68
            'origin'=>'',
69
            'source'=>'',
70
            'sample'=>[],
71
            'markdown'=>0,
72
            'tot_score'=>1,
73
            'partial'=>0,
74
            'special_compiler'=>null,
75
            'order_index'=>null,
76
        ];
77
    }
78
79
    public function importCommandLine($commandTemp)
80
    {
81
        $this->command=$commandTemp;
82
    }
83
84
    public static function cmp($a, $b)
85
    {
86
        return ($a[1]>$b[1]) ?-1 : 1;
87
    }
88
89
    private function _cacheImage($data)
90
    {
91
        if (!isset($data["ori"]) || !isset($data["path"]) || !isset($data["baseurl"]) || !isset($data["space_deli"]) || !isset($data["cookie"])) {
92
            throw new Exception("data is not completely exist in cacheImage");
93
        }
94
        $ori=$data["ori"];
95
        $path=$data["path"];
96
        $baseurl=$data["baseurl"];
97
        $space_deli=$data["space_deli"];
98
        $cookie=$data["cookie"];
99
100
        $para["path"]=$path;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$para was never initialized. Although not strictly required by PHP, it is generally a good practice to add $para = array(); before regardless.
Loading history...
101
        $para["base"]=$baseurl;
102
        $para["trans"]=!$space_deli;
103
        $para["cookie"]=$cookie;
104
105
        if ($space_deli) {
106
            $reg="/< *im[a]?g[^>]*src *= *[\"\\']?([^\"\\' >]*)[^>]*>/si";
107
        } else {
108
            $reg="/< *im[a]?g[^>]*src *= *[\"\\']?([^\"\\'>]*)[^>]*>/si";
109
        }
110
111
        return preg_replace_callback($reg, function($matches) use ($para) {
112
            global $config;
113
            $url=trim($matches[1]);
114
            if (stripos($url, "http://")===false && stripos($url, "https://")===false) {
115
                if ($para["trans"]) {
116
                    $url=str_replace(" ", "%20", $url);
117
                }
118
                $url=$para["base"].$url;
119
            }
120
            $name=basename($url);
121
            $name="images/".strtr($name, ":", "_");
122
            $result=str_replace(trim($matches[1]), "online_Judges/spoj/images/".strtr(basename($url), ":", "_"), $matches[0]);
123
            $ch=curl_init();
124
            curl_setopt($ch, CURLOPT_URL, $url);
125
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
126
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
127
            if ($para["cookie"]!="") {
128
                curl_setopt($ch, CURLOPT_COOKIEFILE, $para["cookie"]);
129
            }
130
            curl_setopt($ch, CURLOPT_HEADER, 0);
131
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
132
            $content=curl_exec($ch);
133
            curl_close($ch);
134
            $fp=fopen($name, "wb");
135
            fwrite($fp, $content);
0 ignored issues
show
Bug introduced by
It seems like $content can also be of type true; however, parameter $data of fwrite() does only seem to accept string, 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

135
            fwrite($fp, /** @scrutinizer ignore-type */ $content);
Loading history...
136
            fclose($fp);
137
            return $result;
138
        }, $ori);
139
    }
140
141
    public function procInfo($data)
142
    {
143
        if (isset($data["path"])) {
144
            $path=$data["path"];
145
        } else {
146
            throw new Exception("path is not exist in data");
147
        }
148
        if (isset($data["baseurl"])) {
149
            $baseurl=$data["baseurl"];
150
        } else {
151
            throw new Exception("baseurl is not exist in data");
152
        }
153
        if (isset($data["space_deli"])) {
154
            $space_deli=$data["space_deli"];
155
        } else {
156
            $space_deli=true;
157
        }
158
        if (isset($data["cookie"])) {
159
            $cookie=$data["cookie"];
160
        } else {
161
            $cookie="";
162
        }
163
164
        $this->pro["description"]=$this->_cacheImage($this->pro["description"], $path, $baseurl, $space_deli, $cookie);
0 ignored issues
show
Unused Code introduced by
The call to App\Babel\Crawl\CrawlerBase::_cacheImage() has too many arguments starting with $path. ( Ignorable by Annotation )

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

164
        /** @scrutinizer ignore-call */ 
165
        $this->pro["description"]=$this->_cacheImage($this->pro["description"], $path, $baseurl, $space_deli, $cookie);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
165
        $this->pro["input"]=$this->_cacheImage($this->pro["input"], $path, $baseurl, $space_deli, $cookie);
166
        $this->pro["output"]=$this->_cacheImage($this->pro["output"], $path, $baseurl, $space_deli, $cookie);
167
        $this->pro["note"]=$this->_cacheImage($this->pro["note"], $path, $baseurl, $space_deli, $cookie);
168
    }
169
170
    public function getUrl($url)
171
    {
172
        $ch=curl_init();
173
        curl_setopt($ch, CURLOPT_URL, $url);
174
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
175
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
176
        curl_setopt($ch, CURLOPT_HEADER, 0);
177
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
178
        $content=curl_exec($ch);
179
        curl_close($ch);
180
        return $content;
181
    }
182
183
    public function insertProblem($oid=2)
0 ignored issues
show
Unused Code introduced by
The parameter $oid 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

183
    public function insertProblem(/** @scrutinizer ignore-unused */ $oid=2)

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...
184
    {
185
        $problemModel=new ProblemModel();
186
        return $problemModel->insertProblem($this->pro);
187
    }
188
189
    public function updateProblem($oid=2)
0 ignored issues
show
Unused Code introduced by
The parameter $oid 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

189
    public function updateProblem(/** @scrutinizer ignore-unused */ $oid=2)

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...
190
    {
191
        $problemModel=new ProblemModel();
192
        return $problemModel->updateProblem($this->pro);
193
    }
194
195
    protected function line($line)
196
    {
197
        if (is_null($this->command)) {
198
            echo $line;
199
        } else {
200
            $this->command->line($line);
201
        }
202
    }
203
}
204