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 ( 94f9cf...d31b64 )
by sunsky
02:35
created

Request::optionAlias()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 7
cts 7
cp 1
rs 9.2
c 0
b 0
f 0
nc 3
cc 4
eloc 7
nop 1
crap 4
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
use MultiHttp\Exception\InvalidOperationException;
16
17
class Request extends Http {
18
	protected static $curlAlias = array(
19
		'url'             => 'CURLOPT_URL',
20
		'debug'           => 'CURLOPT_VERBOSE',//for debug verbose
21
		'method'          => 'CURLOPT_CUSTOMREQUEST',
22
		'data'            => 'CURLOPT_POSTFIELDS', // array or string , file begin with '@'
23
		'ua'              => 'CURLOPT_USERAGENT',
24
		'timeout'         => 'CURLOPT_TIMEOUT', // (secs) 0 means indefinitely
25
		'connect_timeout' => 'CURLOPT_CONNECTTIMEOUT',
26
		'referer'         => 'CURLOPT_REFERER',
27
		'binary'          => 'CURLOPT_BINARYTRANSFER',
28
		'port'            => 'CURLOPT_PORT',
29
		'header'          => 'CURLOPT_HEADER', // TRUE:include header
30
		'headers'         => 'CURLOPT_HTTPHEADER', // array
31
		'download'        => 'CURLOPT_FILE', // writing file stream (using fopen()), default is STDOUT
32
		'upload'          => 'CURLOPT_INFILE', // reading file stream
33
		'transfer'        => 'CURLOPT_RETURNTRANSFER', // TRUE:return string; FALSE:output directly (curl_exec)
34
		'follow_location' => 'CURLOPT_FOLLOWLOCATION',
35
		'timeout_ms'      => 'CURLOPT_TIMEOUT_MS', // milliseconds,  libcurl version > 7.36.0 ,
36
	);
37
	public $curlHandle;
38
    protected $options = array(
39
        'CURLOPT_MAXREDIRS' => 10,
40
        'CURLOPT_IPRESOLVE' => CURL_IPRESOLVE_V4,//IPv4
41
        'header' => true,
42
        'method' => self::GET,
43
        'transfer' => true,
44
        'follow_location' => true,
45
        'timeout' => 0);
46
    protected $endCallback;
47
	protected $withURIQuery;
48
49 2
	protected function __construct() {
50
51 2
	}
52
53 2
	public static function create() {
54 2
		return new self;
55
	}
56
57 1
	public function endCallback() {
58 1
		return $this->endCallback;
59
	}
60
61 2
	public function hasEndCallback() {
62 2
		return isset($this->endCallback);
63
	}
64
65 2
	public function onEnd(callable$callback) {
66 2
		if (!is_callable($callback)) {throw new InvalidArgumentException('callback not is callable :'.print_r(callback, 1));
67
		}
68
69 2
		$this->endCallback = $callback;
70 2
		return $this;
71
	}
72
73 2
	public function getURI() {
74 2
		return $this->getIni('url');
75
	}
76
77
	/**
78
	 * @param $field alias or field name
79
	 * @return bool|mixed
80
	 */
81 2
	public function getIni($field) {
82
//		if (!$this->hasInitialized()) {throw new InvalidOperationException('options has not been initialized');}
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% 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...
83
84 2
		$alias = self::optionAlias($field);
85 2
		if (null === ($rawField = constant($alias))) {throw new InvalidArgumentException('field is invalid');
86
		}
87
88 2
		return isset($this->options[$rawField])?$this->options[$rawField]:false;
89
	}
90
91
	public function hasInitialized() {
92
		return $this->curlHandle?true:false;
93
	}
94
95 1
	public function addQuery($data) {
96 1
		if (!empty($data)) {
97 1
			if (is_array($data)) {
98 1
				$this->withURIQuery = http_build_query($data);
99 1
			} else if (is_string($data)) {
100 1
				$this->withURIQuery = $data;
101
			} else {
102
				throw new InvalidArgumentException('data must be array or string');
103
			}
104
		}
105 1
		return $this;
106
	}
107
108 1
	public function post($uri, array $payload = array(), array $options = array()) {
109 1
		return $this->ini(Http::POST, $uri, $payload, $options);
110
	}
111
112 2
	public function ini($method, $url, array $data = array(), array $options = array()) {
113 2
		$options = array('url' => $url, 'method' => $method, 'data' => $data)+$options;
114 2
		$this->addOptions($options);
115
116 2
		return $this;
117
	}
118
119 2
	public function addOptions(array $options = array()) {
120 2
		$this->options = $options+$this->options;
121 2
		if (empty($this->options['url'])) {throw new InvalidArgumentException('url can not empty');
122
		}
123
124 2
		if (isset($this->options['data'])) {
125 2
			$this->options['data'] = is_array($this->options['data'])?http_build_query($this->options['data']):$this->options['data'];//for better compatibility
126
		}
127 2
		if (isset($this->withURIQuery)) {
128 1
			$this->options['url'] .= strpos($this->options['url'], '?') === FALSE?'?':'&';
129 1
			$this->options['url'] .= $this->withURIQuery;
130
		}
131 2
		if (isset($this->options['callback'])) {
132 2
			$this->onEnd($this->options['callback']);
133 2
			unset($this->options['callback']);
134
		}
135
136 2
		return $this;
137
	}
138
139
	/*  no body  */
140
141
	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...
142
		return $this->ini(Http::PUT, $uri, $payload, $options);
143
	}
144
145
	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...
146
		return $this->ini(Http::PATCH, $uri, $payload, $options);
147
	}
148
149 1
	public function get($uri, array $options = array()) {
150 1
		return $this->ini(Http::GET, $uri, array(), $options);
151
	}
152
153 1
	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...
154 1
		return $this->ini(Http::OPTIONS, $uri, array(), $options);
155
	}
156
157
	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...
158
		return $this->ini(Http::HEAD, $uri, array('CURLOPT_NOBODY' => true), $options);
159
	}
160
161
	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...
162
		return $this->ini(Http::DELETE, $uri, array(), $options);
163
	}
164
165 2
	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...
166 2
		return $this->ini(Http::TRACE, $uri, array(), $options);
167
	}
168
169
	/**
170
	 * @return Response
171
	 */
172 1
	public function execute() {
173 1
		$this->applyOptions();
174 1
		$response = $this->makeResponse();
175 1
		if ($this->endCallback) {
176 1
			$func = $this->endCallback;
177 1
			$func($response);
178
		}
179 1
		return $response;
180
	}
181
182 2
	public function applyOptions() {
183 2
		$curl             = curl_init();
184 2
		$this->curlHandle = $curl;
185 2
		$this->prepare();
186 2
		return $this;
187
	}
188
189 2
	protected function prepare() {
190
		//swap ip and host
191 2
		if (!empty($this->options['ip'])) {
192 1
			$matches = array();
193 1
			preg_match('/\/\/([^\/]+)/', $this->options['url'], $matches);
194 1
			$host = $matches[1];
195 1
			if (empty($this->options['headers']) || !is_array($this->options['headers'])) {
196 1
				$this->options['headers'] = array('Host: '.$host);
197
			} else {
198
				$this->options['headers'][] = 'Host: '.$host;
199
			}
200 1
			$this->options['url'] = preg_replace('/\/\/([^\/]+)/', '//'.$this->options['ip'], $this->options['url']);
201 1
			unset($this->options['ip']);
202 1
			unset($host);
203
		}
204
		//process version
205 2
		if (!empty($this->options['http_version'])) {
206
			$version                                                             = $this->options['http_version'];
207
			if ($version == '1.0') {$this->options['CURLOPT_HTTP_VERSION']       = CURLOPT_HTTP_VERSION_1_0;
208
			} elseif ($version == '1.1') {$this->options['CURLOPT_HTTP_VERSION'] = CURLOPT_HTTP_VERSION_1_1;
209
			}
210
211
			unset($version);
212
		}
213
214
		//convert secs to milliseconds
215 2
		if (defined('CURLOPT_TIMEOUT_MS')) {
216 2
			if (!isset($this->options['timeout_ms'])) {
217 2
				$this->options['timeout_ms'] = intval($this->options['timeout']*1000);
218
			} else {
219 1
				$this->options['timeout_ms'] = intval($this->options['timeout_ms']);
220
			}
221
		}
222
223 2
		self::filterAndRaw($this->options);
224 2
        var_dump($this->options);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($this->options); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
225
226 2
        curl_setopt_array($this->curlHandle, $this->options);
227
		//        curl_setopt($this->curlHandle, CURLOPT_VERBOSE, true);
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
228
229 2
		return $this;
230
	}
231
232 2
	protected static function filterAndRaw(array&$options) {
233 2
		$opts = array();
234 2
		foreach ($options as $key => $val) {
235 2
			$alias = self::optionAlias($key);
236 2
			unset($options[$key]);
237 2
			if ($alias) {$opts[constant($alias)] = $val;
238
			}
239
		}
240 2
		$options = $opts;
241 2
	}
242
243
	/**
244
	 * @param $key
245
	 * @return mixed
246
	 */
247 2
	protected static function optionAlias($key) {
248 2
		$alias = false;
249 2
		if (isset(self::$curlAlias[$key])) {
250 2
			$alias = self::$curlAlias[$key];
251 2
		} elseif ((substr($key, 0, strlen('CURLOPT_')) == 'CURLOPT_') && defined($key)) {
252 2
			$alias = $key;
253
		}
254 2
		return $alias;
255
	}
256 2
	public function makeResponse($isMultiCurl = false) {
257 2
		$body     = $isMultiCurl?curl_multi_getcontent($this->curlHandle):curl_exec($this->curlHandle);
258 2
		$info     = curl_getinfo($this->curlHandle);
259 2
		$errno    = curl_errno($this->curlHandle);
260 2
		$error    = curl_error($this->curlHandle);
261 2
		$response = Response::create($this, $body, $info, $errno, $error);
262 2
		return $response;
263
	}
264
}
265