GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Test Failed
Push — master ( 002ab3...f4053f )
by sunsky
03:32
created

MultiRequest   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 106
Duplicated Lines 8.49 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 97.73%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 2
dl 9
loc 106
ccs 43
cts 44
cp 0.9773
rs 10
c 0
b 0
f 0

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace MultiHttp;
4
5
use MultiHttp\Exception\InvalidArgumentException;
6
7
/**
8
 *
9
 * @author  [email protected] [email protected]
10
 * Date: 2017/6/9
11
 * Time: 15:09
12
 * @version $Id: $
13
 * @since 1.0
14
 * @copyright Sina Corp.
15
 */
16
class MultiRequest
0 ignored issues
show
Bug introduced by
Possible parse error: class missing opening or closing brace
Loading history...
17
{
18
<<<<<<< HEAD
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_SL, expecting T_FUNCTION or T_CONST
Loading history...
19
=======
20
    /**
21
     * @var [Response]
22
     */
23
>>>>>>> 1799bc479182f58431deae9f8975bf80f1e7cee6
24
    protected static $requestPool = array();
25
    protected static $multiHandler;
26
    private static $instance;
27
28
    protected function __construct()
29 1
    {
30
    }
31 1
32
    public static function create()
33
    {
34
        if (!(self::$instance instanceof self)) {
35
            self::$instance = new self;
36 1
        }
37
        self::prepare();
38 1
        return self::$instance;
39 1
    }
40 1
//    protected $isLazy = false;
41 1
//    public function lazyStart(){
42 1
//        $this->isLazy = true;
43
//    }
44
//    public function lazyEnd(){
45
//        $this->isLazy = false;
46
//        return $this->execute();
47
//    }
48
    protected function prepare()
49 1
    {
50 1
//        if(is_null(self::$multiHandler) ||  !$this->isLazy){
51 1
            self::$multiHandler = curl_multi_init();
52
//        }
53 1
    }
54
55 1
    public function add($method, $uri, $payload, array $options = array())
56 1
    {
57
        $options = array(
58
                'method' => $method,
59
                'url' => $uri,
60
                'data' => $payload,
61
            ) + $options;
62
        $this->addOptions(array($options));
63
        return $this;
64
    }
65 1
66
67
    /**
68 1
     * @param array $URLOptions
69 1
     * example: array(array('url'=>'http://localhost:9999/','timeout'=>1, 'method'=>'POST', 'data'=>'aa=bb&c=d'))
70 1
     * @return $this
71 1
     */
72 1
    public function addOptions(array $URLOptions)
73 1
    {
74
        foreach ($URLOptions as $options) {
75
            $request = Request::create()->addOptions($options)->applyOptions();
76
            if (isset($options['callback'])) {
77
                $request->onEnd($options['callback']);
78
            }
79
            $this->import($request);
80
        }
81 1
        return $this;
82
    }
83 1
84 1
    public function import(Request $request)
85 1
    {
86 1
        if (!is_resource($request->curlHandle)) {
87 1
            throw new InvalidArgumentException('Request curl handle is not initialized');
88 1
        }
89 1
        curl_multi_add_handle(self::$multiHandler, $request->curlHandle);
90 1
        self::$requestPool[] = $request;
91 1
        return $this;
92
    }
93
94
    /**
95
     * @return array(Response)
96
     */
97
    public function execute()
98 1
    {
99
//        if($this->isLazy) return array();
100 1
        $sleepTime = 1000;//microsecond, prevent  CPU 100%
101
        do {
102
            curl_multi_exec(self::$multiHandler, $active);
103 1
            // bug in PHP 5.3.18+ where curl_multi_select can return -1
104 1
            // https://bugs.php.net/bug.php?id=63411
105 1
            if (curl_multi_select(self::$multiHandler) == -1) {
106
                usleep($sleepTime);
107
            }
108
            usleep($sleepTime);
109
        } while ($active);
110
        $return = array();
111 1
        foreach (self::$requestPool as $request) {
112
            $response = $request->makeResponse(true);
113 1
            $func = $response->request->endCallback();
114
            if (isset($func)) {
115 1
                $func($response);
116
            }
117
            $return[] = $response;
118 1
            curl_multi_remove_handle(self::$multiHandler, $request->curlHandle);
119 1
            curl_close($request->curlHandle);
120 1
        }
121 1
        curl_multi_close(self::$multiHandler);
122 1
        self::$requestPool = array();
123 1
        self::$multiHandler = null;
124 1
        return $return;
125 1
    }
126 1
127
}
128