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

Curl::post_data()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 30
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 24
nc 6
nop 9
dl 0
loc 30
rs 9.536
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
namespace App\Babel\Judger;
3
4
use App\Models\SubmissionModel;
5
6
class Curl
7
{
8
    public function __construct()
9
    {
10
        //
11
    }
12
13
    protected function login($url, $data, $oj, $ret='false', $handle="default")
14
    {
15
        $datapost=curl_init();
16
        $headers=array("Expect:");
17
18
        curl_setopt($datapost, CURLOPT_CAINFO, dirname(__FILE__)."/cookie/cacert.pem");
0 ignored issues
show
Bug introduced by
It seems like $datapost 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

18
        curl_setopt(/** @scrutinizer ignore-type */ $datapost, CURLOPT_CAINFO, dirname(__FILE__)."/cookie/cacert.pem");
Loading history...
19
        curl_setopt($datapost, CURLOPT_URL, $url);
20
        curl_setopt($datapost, CURLOPT_HEADER, true); //
21
        curl_setopt($datapost, CURLOPT_HTTPHEADER, $headers); //
22
        curl_setopt($datapost, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36");
23
        curl_setopt($datapost, CURLOPT_POST, true);
24
25
        curl_setopt($datapost, CURLOPT_RETURNTRANSFER, $ret);
26
        curl_setopt($datapost, CURLOPT_FOLLOWLOCATION, true);
27
28
        curl_setopt($datapost, CURLOPT_POSTFIELDS, $data);
29
        curl_setopt($datapost, CURLOPT_COOKIEFILE, dirname(__FILE__)."/cookie/{$oj}_{$handle}_cookie.txt");
30
        curl_setopt($datapost, CURLOPT_COOKIEJAR, dirname(__FILE__)."/cookie/{$oj}_{$handle}_cookie.txt");
31
        ob_start();
32
        $response=curl_exec($datapost);
0 ignored issues
show
Bug introduced by
It seems like $datapost 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

32
        $response=curl_exec(/** @scrutinizer ignore-type */ $datapost);
Loading history...
33
        if (curl_errno($datapost)) {
0 ignored issues
show
Bug introduced by
It seems like $datapost can also be of type false; however, parameter $ch of curl_errno() 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

33
        if (curl_errno(/** @scrutinizer ignore-type */ $datapost)) {
Loading history...
34
            die(curl_error($datapost));
0 ignored issues
show
Bug introduced by
It seems like $datapost can also be of type false; however, parameter $ch of curl_error() 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

34
            die(curl_error(/** @scrutinizer ignore-type */ $datapost));
Loading history...
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...
35
        }
36
        ob_end_clean();
37
        curl_close($datapost);
0 ignored issues
show
Bug introduced by
It seems like $datapost 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

37
        curl_close(/** @scrutinizer ignore-type */ $datapost);
Loading history...
38
        unset($datapost);
39
        return $response;
40
    }
41
42
    protected function grab_page($site, $oj, $headers=[], $handle="default")
43
    {
44
        $ch=curl_init();
45
        curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__)."/cookie/cacert.pem");
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

45
        curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_CAINFO, dirname(__FILE__)."/cookie/cacert.pem");
Loading history...
46
        // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
47
        // curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
48
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
49
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36");
50
        curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__)."/cookie/{$oj}_{$handle}_cookie.txt");
51
        curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__)."/cookie/{$oj}_{$handle}_cookie.txt");
52
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
53
        curl_setopt($ch, CURLOPT_URL, $site);
54
        ob_start();
55
        $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

55
        $response=curl_exec(/** @scrutinizer ignore-type */ $ch);
Loading history...
56
        if (curl_errno($ch)) {
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_errno() 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

56
        if (curl_errno(/** @scrutinizer ignore-type */ $ch)) {
Loading history...
57
            die(curl_error($ch));
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...
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_error() 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

57
            die(curl_error(/** @scrutinizer ignore-type */ $ch));
Loading history...
58
        }
59
        ob_end_clean();
60
        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

60
        curl_close(/** @scrutinizer ignore-type */ $ch);
Loading history...
61
        return $response;
62
    }
63
64
    protected function post_data($site, $data, $oj, $ret=false, $follow=true, $returnHeader=true, $postJson=false, $extraHeaders=[], $handle="default")
65
    {
66
        $datapost=curl_init();
67
        $headers=array("Expect:");
68
        if ($postJson) {
69
            $data=$data ? json_encode($data) : '{}';
70
            array_push($headers, 'Content-Type: application/json', 'Content-Length: '.strlen($data));
71
        }
72
        curl_setopt($datapost, CURLOPT_CAINFO, dirname(__FILE__)."/cookie/cacert.pem");
0 ignored issues
show
Bug introduced by
It seems like $datapost 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

72
        curl_setopt(/** @scrutinizer ignore-type */ $datapost, CURLOPT_CAINFO, dirname(__FILE__)."/cookie/cacert.pem");
Loading history...
73
        curl_setopt($datapost, CURLOPT_URL, $site);
74
        curl_setopt($datapost, CURLOPT_HEADER, $returnHeader);
75
        curl_setopt($datapost, CURLOPT_HTTPHEADER, array_merge($headers, $extraHeaders));
76
        curl_setopt($datapost, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36");
77
        curl_setopt($datapost, CURLOPT_POST, true);
78
79
        curl_setopt($datapost, CURLOPT_RETURNTRANSFER, $ret);
80
        curl_setopt($datapost, CURLOPT_FOLLOWLOCATION, $follow);
81
82
        curl_setopt($datapost, CURLOPT_POSTFIELDS, $data);
83
        curl_setopt($datapost, CURLOPT_COOKIEFILE, dirname(__FILE__)."/cookie/{$oj}_{$handle}_cookie.txt");
84
        curl_setopt($datapost, CURLOPT_COOKIEJAR, dirname(__FILE__)."/cookie/{$oj}_{$handle}_cookie.txt");
85
        ob_start();
86
        $response=curl_exec($datapost);
0 ignored issues
show
Bug introduced by
It seems like $datapost 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

86
        $response=curl_exec(/** @scrutinizer ignore-type */ $datapost);
Loading history...
87
        if (curl_errno($datapost)) {
0 ignored issues
show
Bug introduced by
It seems like $datapost can also be of type false; however, parameter $ch of curl_errno() 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

87
        if (curl_errno(/** @scrutinizer ignore-type */ $datapost)) {
Loading history...
88
            die(curl_error($datapost));
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...
Bug introduced by
It seems like $datapost can also be of type false; however, parameter $ch of curl_error() 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

88
            die(curl_error(/** @scrutinizer ignore-type */ $datapost));
Loading history...
89
        }
90
        ob_end_clean();
91
        curl_close($datapost);
0 ignored issues
show
Bug introduced by
It seems like $datapost 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

91
        curl_close(/** @scrutinizer ignore-type */ $datapost);
Loading history...
92
        unset($datapost);
93
        return $response;
94
    }
95
}
96