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.
Passed
Push — master ( ce9742...4c1af4 )
by sunsky
11:33
created

MultiRequest   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 120
Duplicated Lines 7.5 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 98.18%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 2
dl 9
loc 120
ccs 54
cts 55
cp 0.9818
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A create() 0 8 2
A setDefaults() 0 4 1
A prepare() 0 4 1
A add() 9 10 1
A addOptions() 0 12 3
A import() 0 9 2
A sendAll() 0 23 4

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
17
{
18
    /**
19
     * @var [Response]
20
     */
21
    protected static $requestPool = array();
22
    protected static $multiHandler;
23
    protected $defaultOptions = array();
24
    private static $instance;
25
    protected static $loggerHandler;
26
    /**
27
     * MultiRequest constructor.
28
     */
29 1
    protected function __construct()
30
    {
31 1
    }
32
33
    /**
34
     * @return MultiRequest
35
     */
36 2
    public static function create()
37
    {
38 2
        if (!(self::$instance instanceof self)) {
39 1
            self::$instance = new self;
40 1
        }
41 2
        self::prepare();
42 2
        return self::$instance;
43
    }
44
45
    /**
46
     * @param array $options
47
     * @return $this
48
     */
49 1
    public function setDefaults(array $options = array()){
50 1
        $this->defaultOptions = $options;
51 1
        return $this;
52
    }
53 2
    protected static function prepare()
54
    {
55 2
        self::$multiHandler = curl_multi_init();
56 2
    }
57
58
    /**
59
     * @param $method
60
     * @param $uri
61
     * @param $payload
62
     * @param array $options
63
     * @return $this
64
     */
65 2 View Code Duplication
    public function add($method, $uri, $payload, array $options = array())
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
66
    {
67
        $options = array(
68 2
                'method' => $method,
69 2
                'url' => $uri,
70 2
                'data' => $payload,
71 2
            ) + $options;
72 2
        $this->addOptions(array($options));
73 2
        return $this;
74
    }
75
76
    /**
77
     * @param array $URLOptions
78
     * example: array(array('url'=>'http://localhost:9999/','timeout'=>1, 'method'=>'POST', 'data'=>'aa=bb&c=d'))
79
     * @return $this
80
     */
81 2
    public function addOptions(array $URLOptions)
82
    {
83 2
        foreach ($URLOptions as $options) {
84 2
            $options = $options + $this->defaultOptions;
85 2
            $request = Request::create()->addOptions($options)->applyOptions();
86 2
            if (isset($options['callback'])) {
87 2
                $request->onEnd($options['callback']);
88 2
            }
89 2
            $this->import($request);
90 2
        }
91 2
        return $this;
92
    }
93
94
    /**
95
     * @param Request $request
96
     * @return $this
97
     */
98 2
    public function import(Request $request)
99
    {
100 2
        if (!is_resource($request->curlHandle)) {
101
            throw new InvalidArgumentException('Request curl handle is not initialized');
102
        }
103 2
        curl_multi_add_handle(self::$multiHandler, $request->curlHandle);
104 2
        self::$requestPool[] = $request;
105 2
        return $this;
106
    }
107
108
    /**
109
     * @return array(Response)
0 ignored issues
show
Documentation introduced by
The doc-type array(Response) could not be parsed: Expected "|" or "end of type", but got "(" at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
110
     */
111 2
    public function sendAll()
112
    {
113 2
        $sleepTime = 1000;//microsecond, prevent  CPU 100%
114
        do {
115 2
            curl_multi_exec(self::$multiHandler, $active);
116
            // bug in PHP 5.3.18+ where curl_multi_select can return -1
117
            // https://bugs.php.net/bug.php?id=63411
118 2
            if (curl_multi_select(self::$multiHandler) == -1) {
119 2
                usleep($sleepTime);
120 2
            }
121 2
            usleep($sleepTime);
122 2
        } while ($active);
123 2
        $return = array();
124 2
        foreach (self::$requestPool as $request) {
125 2
            $return[] = $request->send(true);
126 2
            curl_multi_remove_handle(self::$multiHandler, $request->curlHandle);
127 2
            curl_close($request->curlHandle);
128 2
        }
129 2
        curl_multi_close(self::$multiHandler);
130 2
        self::$requestPool = array();
131 2
        self::$multiHandler = null;
132 2
        return $return;
133
    }
134
135
}
136