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 ( cd061f...7d03fc )
by sunsky
02:03
created

Request::hasInitialized()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
nc 2
cc 2
eloc 2
nop 0
crap 6
1
<?php
2
/**
3
 *
4
 * @author  [email protected] [email protected]
5
 * Date: 2017/6/16
6
 * Time: 10:02
7
 * @version $Id: $
8
 * @since 1.0
9
 * @copyright Sina Corp.
10
 */
11
12
namespace MultiHttp;
13
14
use MultiHttp\Exception\InvalidArgumentException;
15
16
class Request extends Http {
17
	protected static $curlAlias = array(
18
		'url'             => 'CURLOPT_URL',
19
		'debug'           => 'CURLOPT_VERBOSE',//for debug verbose
20
		'method'          => 'CURLOPT_CUSTOMREQUEST',
21
		'data'            => 'CURLOPT_POSTFIELDS', // array or string , file begin with '@'
22
		'ua'              => 'CURLOPT_USERAGENT',
23
		'timeout'         => 'CURLOPT_TIMEOUT', // (secs) 0 means indefinitely
24
		'connect_timeout' => 'CURLOPT_CONNECTTIMEOUT',
25
		'referer'         => 'CURLOPT_REFERER',
26
		'binary'          => 'CURLOPT_BINARYTRANSFER',
27
		'port'            => 'CURLOPT_PORT',
28
		'header'          => 'CURLOPT_HEADER', // TRUE:include header
29
		'headers'         => 'CURLOPT_HTTPHEADER', // array
30
		'download'        => 'CURLOPT_FILE', // writing file stream (using fopen()), default is STDOUT
31
		'upload'          => 'CURLOPT_INFILE', // reading file stream
32
		'transfer'        => 'CURLOPT_RETURNTRANSFER', // TRUE:return string; FALSE:output directly (curl_exec)
33
		'follow_location' => 'CURLOPT_FOLLOWLOCATION',
34
		'timeout_ms'      => 'CURLOPT_TIMEOUT_MS', // milliseconds,  libcurl version > 7.36.0 ,
35
	);
36
	public $curlHandle;
37
    protected $options = array(
38
        'CURLOPT_MAXREDIRS' => 10,
39
//        'CURLOPT_IPRESOLVE' => CURL_IPRESOLVE_V4,//IPv4
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
40
        'header' => true,
41
        'method' => self::GET,
42
        'transfer' => true,
43
        'follow_location' => true,
44
        'timeout' => 0);
45
    protected $endCallback;
46
	protected $withURIQuery;
47
48
	protected function __construct() {
49 2
50
	}
51 2
52
	public static function create() {
53 2
		return new self;
54 2
	}
55
56
	public function endCallback() {
57 1
		return $this->endCallback;
58 1
	}
59
60
	public function hasEndCallback() {
61 2
		return isset($this->endCallback);
62 2
	}
63
64
	public function onEnd(callable$callback) {
65 2
		if (!is_callable($callback)) {throw new InvalidArgumentException('callback not is callable :'.print_r(callback, 1));
66 2
		}
67
68
		$this->endCallback = $callback;
69 2
		return $this;
70 2
	}
71
72
	public function getURI() {
73 2
		return $this->getIni('url');
74 2
	}
75
76
	/**
77
	 * @param $field alias or field name
78
	 * @return bool|mixed
79
	 */
80
	public function getIni($field) {
81 2
		$alias = self::optionAlias($field);
82
		if (null === ($rawField = constant($alias))) {throw new InvalidArgumentException('field is invalid');
83
		}
84 2
85 2
		return isset($this->options[$rawField])?$this->options[$rawField]:false;
86
	}
87
88 2
89
	public function addQuery($data) {
90
		if (!empty($data)) {
91
			if (is_array($data)) {
92
				$this->withURIQuery = http_build_query($data);
93
			} else if (is_string($data)) {
94
				$this->withURIQuery = $data;
95 1
			} else {
96 1
				throw new InvalidArgumentException('data must be array or string');
97 1
			}
98 1
		}
99 1
		return $this;
100 1
	}
101
102
	public function post($uri, array $payload = array(), array $options = array()) {
103
		return $this->ini(Http::POST, $uri, $payload, $options);
104
	}
105 1
106
	protected function ini($method, $url, array $data = array(), array $options = array()) {
107
		$options = array('url' => $url, 'method' => $method, 'data' => $data)+$options;
108 1
		$this->addOptions($options);
109 1
110
		return $this;
111
	}
112 2
113 2
	public function addOptions(array $options = array()) {
114 2
		$this->options = $options+$this->options;
115
		if (empty($this->options['url'])) {throw new InvalidArgumentException('url can not empty');
116 2
		}
117
118
		if (isset($this->options['data'])) {
119 2
			$this->options['data'] = is_array($this->options['data'])?http_build_query($this->options['data']):$this->options['data'];//for better compatibility
120 2
		}
121 2
		if (isset($this->withURIQuery)) {
122
			$this->options['url'] .= strpos($this->options['url'], '?') === FALSE?'?':'&';
123
			$this->options['url'] .= $this->withURIQuery;
124 2
		}
125 2
		if (isset($this->options['callback'])) {
126
			$this->onEnd($this->options['callback']);
127 2
			unset($this->options['callback']);
128 1
		}
129 1
130
		return $this;
131 2
	}
132 2
133 2
	/*  no body  */
134
135
	function put($uri, array $payload = array(), array $options = array()) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
136 2
		return $this->ini(Http::PUT, $uri, $payload, $options);
137
	}
138
139
	function patch($uri, array $payload = array(), array $options = array()) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
140
		return $this->ini(Http::PATCH, $uri, $payload, $options);
141
	}
142
143
	public function get($uri, array $options = array()) {
144
		return $this->ini(Http::GET, $uri, array(), $options);
145
	}
146
147
	function options($uri, array $options = array()) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
148
		return $this->ini(Http::OPTIONS, $uri, array(), $options);
149 1
	}
150 1
151
	function head($uri, array $options = array()) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
152
		return $this->ini(Http::HEAD, $uri, array('CURLOPT_NOBODY' => true), $options);
153 1
	}
154 1
155
	function delete($uri, array $options = array()) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
156
		return $this->ini(Http::DELETE, $uri, array(), $options);
157
	}
158
159
	function trace($uri, array $options = array()) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
160
		return $this->ini(Http::TRACE, $uri, array(), $options);
161
	}
162
163
	/**
164
	 * @return Response
165 2
	 */
166 2
	public function execute() {
167
		$this->applyOptions();
168
		$response = $this->makeResponse();
169
		if ($this->endCallback) {
170
			$func = $this->endCallback;
171
			$func($response);
172 1
		}
173 1
		return $response;
174 1
	}
175 1
176 1
	public function applyOptions() {
177 1
		$curl             = curl_init();
178
		$this->curlHandle = $curl;
179 1
		$this->prepare();
180
		return $this;
181
	}
182 2
183 2
	protected function prepare() {
184 2
		//swap ip and host
185 2
		if (!empty($this->options['ip'])) {
186 2
			$matches = array();
187
			preg_match('/\/\/([^\/]+)/', $this->options['url'], $matches);
188
			$host = $matches[1];
189 2
			if (empty($this->options['headers']) || !is_array($this->options['headers'])) {
190
				$this->options['headers'] = array('Host: '.$host);
191 2
			} else {
192 1
				$this->options['headers'][] = 'Host: '.$host;
193 1
			}
194 1
			$this->options['url'] = preg_replace('/\/\/([^\/]+)/', '//'.$this->options['ip'], $this->options['url']);
195 1
			unset($this->options['ip']);
196 1
			unset($host);
197
		}
198
		//process version
199
		if (!empty($this->options['http_version'])) {
200 1
			$version                                                             = $this->options['http_version'];
201 1
			if ($version == '1.0') {$this->options['CURLOPT_HTTP_VERSION']       = CURLOPT_HTTP_VERSION_1_0;
202 1
			} elseif ($version == '1.1') {$this->options['CURLOPT_HTTP_VERSION'] = CURLOPT_HTTP_VERSION_1_1;
203
			}
204
205 2
			unset($version);
206
		}
207
208
		//convert secs to milliseconds
209
		if (defined('CURLOPT_TIMEOUT_MS')) {
210
			if (!isset($this->options['timeout_ms'])) {
211
				$this->options['timeout_ms'] = intval($this->options['timeout']*1000);
212
			} else {
213
				$this->options['timeout_ms'] = intval($this->options['timeout_ms']);
214
			}
215 2
		}
216 2
217 2
		self::filterAndRaw($this->options);
218
219 1
        curl_setopt_array($this->curlHandle, $this->options);
220
221
		return $this;
222
	}
223 2
224 2
	protected static function filterAndRaw(array&$options) {
225
		$opts = array();
226 2
		foreach ($options as $key => $val) {
227
			$alias = self::optionAlias($key);
228
			unset($options[$key]);
229 2
			if ($alias) {$opts[constant($alias)] = $val;
230
			}
231
		}
232 2
		$options = $opts;
233 2
	}
234 2
235 2
	/**
236 2
	 * @param $key
237 2
	 * @return mixed
238
	 */
239
	protected static function optionAlias($key) {
240 2
		$alias = false;
241 2
		if (isset(self::$curlAlias[$key])) {
242
			$alias = self::$curlAlias[$key];
243
		} elseif ((substr($key, 0, strlen('CURLOPT_')) == 'CURLOPT_') && defined($key)) {
244
			$alias = $key;
245
		}
246
		return $alias;
247 2
	}
248 2
	public function makeResponse($isMultiCurl = false) {
249 2
		$body     = $isMultiCurl?curl_multi_getcontent($this->curlHandle):curl_exec($this->curlHandle);
250 2
		$info     = curl_getinfo($this->curlHandle);
251 2
		$errno    = curl_errno($this->curlHandle);
252 2
		$error    = curl_error($this->curlHandle);
253
		$response = Response::create($this, $body, $info, $errno, $error);
254 2
		return $response;
255
	}
256
}
257