Passed
Push — main ( cf021d...1522c6 )
by smiley
02:01
created
src/Core/OAuth1Provider.php 1 patch
Spacing   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -21,7 +21,7 @@  discard block
 block discarded – undo
21 21
 /**
22 22
  * Implements an abstract OAuth1 provider with all methods required by the OAuth1Interface.
23 23
  */
24
-abstract class OAuth1Provider extends OAuthProvider implements OAuth1Interface{
24
+abstract class OAuth1Provider extends OAuthProvider implements OAuth1Interface {
25 25
 
26 26
 	/**
27 27
 	 * The request OAuth1 token URL
@@ -60,7 +60,7 @@  discard block
 block discarded – undo
60 60
 			->withHeader('Content-Length', '0') // tumblr requires a content-length header set
61 61
 		;
62 62
 
63
-		foreach($this->authHeaders as $header => $value){
63
+		foreach ($this->authHeaders as $header => $value) {
64 64
 			$request = $request->withAddedHeader($header, $value);
65 65
 		}
66 66
 
@@ -81,20 +81,20 @@  discard block
 block discarded – undo
81 81
 	protected function parseTokenResponse(ResponseInterface $response, bool $checkCallbackConfirmed = null):AccessToken{
82 82
 		$data = $this->parseQuery(decompress_content($response));
83 83
 
84
-		if(!$data || !is_array($data)){
84
+		if (!$data || !is_array($data)) {
85 85
 			throw new ProviderException('unable to parse token response');
86 86
 		}
87
-		elseif(isset($data['error'])){
87
+		elseif (isset($data['error'])) {
88 88
 			throw new ProviderException('error retrieving access token: '.$data['error']);
89 89
 		}
90
-		elseif(!isset($data['oauth_token']) || !isset($data['oauth_token_secret'])){
90
+		elseif (!isset($data['oauth_token']) || !isset($data['oauth_token_secret'])) {
91 91
 			throw new ProviderException('invalid token');
92 92
 		}
93 93
 
94
-		if(
94
+		if (
95 95
 			$checkCallbackConfirmed
96 96
 			&& (!isset($data['oauth_callback_confirmed']) || $data['oauth_callback_confirmed'] !== 'true')
97
-		){
97
+		) {
98 98
 			throw new ProviderException('oauth callback unconfirmed');
99 99
 		}
100 100
 
@@ -145,7 +145,7 @@  discard block
 block discarded – undo
145 145
 	protected function getSignature(string $url, array $params, string $method, string $accessTokenSecret = null):string{
146 146
 		$parseURL = parseUrl($url);
147 147
 
148
-		if(!isset($parseURL['host']) || !isset($parseURL['scheme']) || !in_array($parseURL['scheme'], ['http', 'https'], true)){
148
+		if (!isset($parseURL['host']) || !isset($parseURL['scheme']) || !in_array($parseURL['scheme'], ['http', 'https'], true)) {
149 149
 			throw new ProviderException('getSignature: invalid url');
150 150
 		}
151 151
 
@@ -203,7 +203,7 @@  discard block
 block discarded – undo
203 203
 			$token->accessTokenSecret
204 204
 		);
205 205
 
206
-		if(isset($query['oauth_session_handle'])){
206
+		if (isset($query['oauth_session_handle'])) {
207 207
 			$parameters['oauth_session_handle'] = $query['oauth_session_handle']; // @codeCoverageIgnore
208 208
 		}
209 209
 
Please login to merge, or discard this patch.
src/Core/OAuth2Provider.php 1 patch
Spacing   +24 added lines, -24 removed lines patch added patch discarded remove patch
@@ -26,7 +26,7 @@  discard block
 block discarded – undo
26 26
  * Implements an abstract OAuth2 provider with all methods required by the OAuth2Interface.
27 27
  * It also implements the ClientCredentials, CSRFToken and TokenRefresh interfaces in favor over traits.
28 28
  */
29
-abstract class OAuth2Provider extends OAuthProvider implements OAuth2Interface{
29
+abstract class OAuth2Provider extends OAuthProvider implements OAuth2Interface {
30 30
 
31 31
 	/**
32 32
 	 * Specifies the authentication method:
@@ -83,11 +83,11 @@  discard block
 block discarded – undo
83 83
 			'type'          => 'web_server',
84 84
 		]);
85 85
 
86
-		if(!empty($scopes)){
86
+		if (!empty($scopes)) {
87 87
 			$params['scope'] = implode($this->scopesDelimiter, $scopes);
88 88
 		}
89 89
 
90
-		if($this instanceof CSRFToken){
90
+		if ($this instanceof CSRFToken) {
91 91
 			$params = $this->setState($params);
92 92
 		}
93 93
 
@@ -109,19 +109,19 @@  discard block
 block discarded – undo
109 109
 		// silly amazon sends compressed data...
110 110
 		$data = json_decode(decompress_content($response), true, 512, JSON_THROW_ON_ERROR);
111 111
 
112
-		if(!is_array($data)){
112
+		if (!is_array($data)) {
113 113
 			throw new ProviderException('unable to parse token response');
114 114
 		}
115 115
 
116
-		foreach(['error_description', 'error'] as $field){
116
+		foreach (['error_description', 'error'] as $field) {
117 117
 
118
-			if(isset($data[$field])){
118
+			if (isset($data[$field])) {
119 119
 				throw new ProviderException('error retrieving access token: "'.$data[$field].'"');
120 120
 			}
121 121
 
122 122
 		}
123 123
 
124
-		if(!isset($data['access_token'])){
124
+		if (!isset($data['access_token'])) {
125 125
 			throw new ProviderException('token missing');
126 126
 		}
127 127
 
@@ -144,7 +144,7 @@  discard block
 block discarded – undo
144 144
 	 */
145 145
 	public function getAccessToken(string $code, string $state = null):AccessToken{
146 146
 
147
-		if($this instanceof CSRFToken){
147
+		if ($this instanceof CSRFToken) {
148 148
 			$this->checkState($state);
149 149
 		}
150 150
 
@@ -162,7 +162,7 @@  discard block
 block discarded – undo
162 162
 			->withHeader('Accept-Encoding', 'identity')
163 163
 			->withBody($this->streamFactory->createStream($this->buildQuery($body, PHP_QUERY_RFC1738)));
164 164
 
165
-		foreach($this->authHeaders as $header => $value){
165
+		foreach ($this->authHeaders as $header => $value) {
166 166
 			$request = $request->withHeader($header, $value);
167 167
 		}
168 168
 
@@ -178,11 +178,11 @@  discard block
 block discarded – undo
178 178
 	 */
179 179
 	public function getRequestAuthorization(RequestInterface $request, AccessToken $token):RequestInterface{
180 180
 
181
-		if($this->authMethod === OAuth2Interface::AUTH_METHOD_HEADER){
181
+		if ($this->authMethod === OAuth2Interface::AUTH_METHOD_HEADER) {
182 182
 			return $request->withHeader('Authorization', $this->authMethodHeader.' '.$token->accessToken);
183 183
 		}
184 184
 
185
-		if($this->authMethod === OAuth2Interface::AUTH_METHOD_QUERY){
185
+		if ($this->authMethod === OAuth2Interface::AUTH_METHOD_QUERY) {
186 186
 			$uri = $this->mergeQuery((string)$request->getUri(), [$this->authMethodQuery => $token->accessToken]);
187 187
 
188 188
 			return $request->withUri($this->uriFactory->createUri($uri));
@@ -203,13 +203,13 @@  discard block
 block discarded – undo
203 203
 	 */
204 204
 	public function getClientCredentialsToken(array $scopes = null):AccessToken{
205 205
 
206
-		if(!$this instanceof ClientCredentials){
206
+		if (!$this instanceof ClientCredentials) {
207 207
 			throw new ProviderException('client credentials token not supported');
208 208
 		}
209 209
 
210 210
 		$params = ['grant_type' => 'client_credentials'];
211 211
 
212
-		if($scopes !== null){
212
+		if ($scopes !== null) {
213 213
 			$params['scope'] = implode($this->scopesDelimiter, $scopes);
214 214
 		}
215 215
 
@@ -221,7 +221,7 @@  discard block
 block discarded – undo
221 221
 			->withBody($this->streamFactory->createStream($this->buildQuery($params, PHP_QUERY_RFC1738)))
222 222
 		;
223 223
 
224
-		foreach($this->authHeaders as $header => $value){
224
+		foreach ($this->authHeaders as $header => $value) {
225 225
 			$request = $request->withAddedHeader($header, $value);
226 226
 		}
227 227
 
@@ -244,17 +244,17 @@  discard block
 block discarded – undo
244 244
 	 */
245 245
 	public function refreshAccessToken(AccessToken $token = null):AccessToken{
246 246
 
247
-		if(!$this instanceof TokenRefresh){
247
+		if (!$this instanceof TokenRefresh) {
248 248
 			throw new ProviderException('token refresh not supported');
249 249
 		}
250 250
 
251
-		if($token === null){
251
+		if ($token === null) {
252 252
 			$token = $this->storage->getAccessToken($this->serviceName);
253 253
 		}
254 254
 
255 255
 		$refreshToken = $token->refreshToken;
256 256
 
257
-		if(empty($refreshToken)){
257
+		if (empty($refreshToken)) {
258 258
 			throw new ProviderException(
259 259
 				sprintf('no refresh token available, token expired [%s]', date('Y-m-d h:i:s A', $token->expires))
260 260
 			);
@@ -275,13 +275,13 @@  discard block
 block discarded – undo
275 275
 			->withBody($this->streamFactory->createStream($this->buildQuery($body, PHP_QUERY_RFC1738)))
276 276
 		;
277 277
 
278
-		foreach($this->authHeaders as $header => $value){
278
+		foreach ($this->authHeaders as $header => $value) {
279 279
 			$request = $request->withAddedHeader($header, $value);
280 280
 		}
281 281
 
282 282
 		$newToken = $this->parseTokenResponse($this->http->sendRequest($request));
283 283
 
284
-		if(empty($newToken->refreshToken)){
284
+		if (empty($newToken->refreshToken)) {
285 285
 			$newToken->refreshToken = $refreshToken;
286 286
 		}
287 287
 
@@ -304,17 +304,17 @@  discard block
 block discarded – undo
304 304
 	 */
305 305
 	public function checkState(string $state = null):void{
306 306
 
307
-		if(!$this instanceof CSRFToken){
307
+		if (!$this instanceof CSRFToken) {
308 308
 			throw new ProviderException('CSRF protection not supported');
309 309
 		}
310 310
 
311
-		if(empty($state) || !$this->storage->hasCSRFState($this->serviceName)){
311
+		if (empty($state) || !$this->storage->hasCSRFState($this->serviceName)) {
312 312
 			throw new ProviderException('invalid state for '.$this->serviceName);
313 313
 		}
314 314
 
315 315
 		$knownState = $this->storage->getCSRFState($this->serviceName);
316 316
 
317
-		if(!hash_equals($knownState, $state)){
317
+		if (!hash_equals($knownState, $state)) {
318 318
 			throw new ProviderException('invalid CSRF state: '.$this->serviceName.' '.$state);
319 319
 		}
320 320
 
@@ -334,11 +334,11 @@  discard block
 block discarded – undo
334 334
 	 */
335 335
 	public function setState(array $params):array{
336 336
 
337
-		if(!$this instanceof CSRFToken){
337
+		if (!$this instanceof CSRFToken) {
338 338
 			throw new ProviderException('CSRF protection not supported');
339 339
 		}
340 340
 
341
-		if(!isset($params['state'])){
341
+		if (!isset($params['state'])) {
342 342
 			$params['state'] = sha1(random_bytes(256));
343 343
 		}
344 344
 
Please login to merge, or discard this patch.
src/Core/OAuthProvider.php 2 patches
Spacing   +31 added lines, -31 removed lines patch added patch discarded remove patch
@@ -42,7 +42,7 @@  discard block
 block discarded – undo
42 42
  * @property string                                          $serviceName
43 43
  * @property string|null                                     $userRevokeURL
44 44
  */
45
-abstract class OAuthProvider implements OAuthInterface{
45
+abstract class OAuthProvider implements OAuthInterface {
46 46
 	use LoggerAwareTrait;
47 47
 
48 48
 	protected const ALLOWED_PROPERTIES = [
@@ -156,7 +156,7 @@  discard block
 block discarded – undo
156 156
 		OAuthStorageInterface $storage,
157 157
 		SettingsContainerInterface $options,
158 158
 		LoggerInterface $logger = null
159
-	){
159
+	) {
160 160
 		$this->http    = $http;
161 161
 		$this->storage = $storage;
162 162
 		$this->options = $options;
@@ -170,10 +170,10 @@  discard block
 block discarded – undo
170 170
 
171 171
 		$this->serviceName = (new ReflectionClass($this))->getShortName();
172 172
 
173
-		if(!empty($this->endpointMap) && class_exists($this->endpointMap)){
173
+		if (!empty($this->endpointMap) && class_exists($this->endpointMap)) {
174 174
 			$this->endpoints = new $this->endpointMap;
175 175
 
176
-			if(!$this->endpoints instanceof EndpointMapInterface){
176
+			if (!$this->endpoints instanceof EndpointMapInterface) {
177 177
 				throw new ApiClientException('invalid endpoint map'); // @codeCoverageIgnore
178 178
 			}
179 179
 
@@ -188,9 +188,9 @@  discard block
 block discarded – undo
188 188
 	 *
189 189
 	 * @return mixed|null
190 190
 	 */
191
-	public function __get(string $name){
191
+	public function __get(string $name) {
192 192
 
193
-		if(in_array($name, $this::ALLOWED_PROPERTIES, true)){
193
+		if (in_array($name, $this::ALLOWED_PROPERTIES, true)) {
194 194
 			return $this->{$name};
195 195
 		}
196 196
 
@@ -248,11 +248,11 @@  discard block
 block discarded – undo
248 248
 	 */
249 249
 	public function __call(string $endpointName, array $arguments):ResponseInterface{
250 250
 
251
-		if(!$this->endpoints instanceof EndpointMap){
251
+		if (!$this->endpoints instanceof EndpointMap) {
252 252
 			throw new ApiClientException('MagicAPI not available'); // @codeCoverageIgnore
253 253
 		}
254 254
 
255
-		if(!isset($this->endpoints->{$endpointName})){
255
+		if (!isset($this->endpoints->{$endpointName})) {
256 256
 			throw new ApiClientException('endpoint not found: "'.$endpointName.'"');
257 257
 		}
258 258
 
@@ -272,25 +272,25 @@  discard block
 block discarded – undo
272 272
 		$path_element_count = count($path_elements);
273 273
 		$query_param_count  = count($query_params);
274 274
 
275
-		if($path_element_count > 0){
275
+		if ($path_element_count > 0) {
276 276
 			$path = $this->parsePathElements($path, $path_elements, $path_element_count, $arguments);
277 277
 		}
278 278
 
279
-		if($query_param_count > 0){
279
+		if ($query_param_count > 0) {
280 280
 			// $params is the first argument after path segments
281 281
 			$params = $arguments[$path_element_count] ?? null;
282 282
 
283
-			if(is_array($params)){
283
+			if (is_array($params)) {
284 284
 				$params = $this->cleanQueryParams($this->removeUnlistedParams($params, $query_params));
285 285
 			}
286 286
 		}
287 287
 
288
-		if(in_array($method, ['POST', 'PATCH', 'PUT', 'DELETE']) && $has_body){
288
+		if (in_array($method, ['POST', 'PATCH', 'PUT', 'DELETE']) && $has_body) {
289 289
 			// if no query params are present, $body is the first argument after any path segments
290 290
 			$argPos = $query_param_count > 0 ? 1 : 0;
291 291
 			$body   = $arguments[$path_element_count + $argPos] ?? null;
292 292
 
293
-			if(is_array($body)){
293
+			if (is_array($body)) {
294 294
 				$body = $this->cleanBodyParams($body);
295 295
 			}
296 296
 		}
@@ -311,13 +311,13 @@  discard block
 block discarded – undo
311 311
 		// we don't know if all of the given arguments are path elements...
312 312
 		$urlparams = array_slice($arguments, 0, $path_element_count);
313 313
 
314
-		if(count($urlparams) !== $path_element_count){
314
+		if (count($urlparams) !== $path_element_count) {
315 315
 			throw new APIClientException('too few URL params, required: '.implode(', ', $path_elements));
316 316
 		}
317 317
 
318
-		foreach($urlparams as $i => $param){
318
+		foreach ($urlparams as $i => $param) {
319 319
 			// ...but we do know that the arguments after the path elements are usually array or null
320
-			if(!is_scalar($param)){
320
+			if (!is_scalar($param)) {
321 321
 				$msg = 'invalid path element value for "%s": %s';
322 322
 
323 323
 				throw new APIClientException(sprintf($msg, $path_elements[$i], var_export($param, true)));
@@ -333,9 +333,9 @@  discard block
 block discarded – undo
333 333
 	protected function removeUnlistedParams(array $params, array $allowed):array{
334 334
 		$query = [];
335 335
 		// remove any params that are not listed
336
-		foreach($params as $key => $value){
336
+		foreach ($params as $key => $value) {
337 337
 
338
-			if(!in_array($key, $allowed, true)){
338
+			if (!in_array($key, $allowed, true)) {
339 339
 				continue;
340 340
 			}
341 341
 
@@ -394,28 +394,28 @@  discard block
 block discarded – undo
394 394
 		$request = $this->requestFactory
395 395
 			->createRequest($method ?? 'GET', $this->mergeQuery($this->getRequestTarget($path), $params ?? []));
396 396
 
397
-		foreach(array_merge($this->apiHeaders, $headers ?? []) as $header => $value){
397
+		foreach (array_merge($this->apiHeaders, $headers ?? []) as $header => $value) {
398 398
 			$request = $request->withAddedHeader($header, $value);
399 399
 		}
400 400
 
401
-		if($request->hasHeader('content-type')){
401
+		if ($request->hasHeader('content-type')) {
402 402
 			$contentType = strtolower($request->getHeaderLine('content-type'));
403 403
 
404
-			if(is_array($body)){
405
-				if($contentType === 'application/x-www-form-urlencoded'){
404
+			if (is_array($body)) {
405
+				if ($contentType === 'application/x-www-form-urlencoded') {
406 406
 					$body = $this->streamFactory->createStream($this->buildQuery($body, PHP_QUERY_RFC1738));
407 407
 				}
408
-				elseif($contentType === 'application/json' || $contentType === 'application/vnd.api+json'){
408
+				elseif ($contentType === 'application/json' || $contentType === 'application/vnd.api+json') {
409 409
 					$body = $this->streamFactory->createStream(json_encode($body));
410 410
 				}
411 411
 			}
412
-			elseif(is_string($body)){
412
+			elseif (is_string($body)) {
413 413
 				// we don't check if the given string matches the content type - this is the implementor's responsibility
414 414
 				$body = $this->streamFactory->createStream($body);
415 415
 			}
416 416
 		}
417 417
 
418
-		if($body instanceof StreamInterface){
418
+		if ($body instanceof StreamInterface) {
419 419
 			$request = $request
420 420
 				->withBody($body)
421 421
 				->withHeader('Content-length', (string)$body->getSize())
@@ -437,16 +437,16 @@  discard block
 block discarded – undo
437 437
 	protected function getRequestTarget(string $uri):string{
438 438
 		$parsedURL = parseUrl($uri);
439 439
 
440
-		if(!isset($parsedURL['path'])){
440
+		if (!isset($parsedURL['path'])) {
441 441
 			throw new ProviderException('invalid path');
442 442
 		}
443 443
 
444 444
 		// for some reason we were given a host name
445
-		if(isset($parsedURL['host'])){
445
+		if (isset($parsedURL['host'])) {
446 446
 
447 447
 			// back out if it doesn't match
448 448
 			/** @phan-suppress-next-line PhanTypeArraySuspiciousNullable - $this->>apiURL should always return a host */
449
-			if($parsedURL['host'] !== parseUrl($this->apiURL)['host']){
449
+			if ($parsedURL['host'] !== parseUrl($this->apiURL)['host']) {
450 450
 				throw new ProviderException('given host does not match provider host');
451 451
 			}
452 452
 
@@ -464,15 +464,15 @@  discard block
 block discarded – undo
464 464
 	public function sendRequest(RequestInterface $request):ResponseInterface{
465 465
 
466 466
 		// get authorization only if we request the provider API
467
-		if(strpos((string)$request->getUri(), $this->apiURL) === 0){
467
+		if (strpos((string)$request->getUri(), $this->apiURL) === 0) {
468 468
 			$token = $this->storage->getAccessToken($this->serviceName);
469 469
 
470 470
 			// attempt to refresh an expired token
471
-			if(
471
+			if (
472 472
 				$this instanceof TokenRefresh
473 473
 				&& $this->options->tokenAutoRefresh
474 474
 				&& ($token->isExpired() || $token->expires === $token::EOL_UNKNOWN)
475
-			){
475
+			) {
476 476
 				$token = $this->refreshAccessToken($token);
477 477
 			}
478 478
 
Please login to merge, or discard this patch.
Braces   +2 added lines, -4 removed lines patch added patch discarded remove patch
@@ -404,12 +404,10 @@
 block discarded – undo
404 404
 			if(is_array($body)){
405 405
 				if($contentType === 'application/x-www-form-urlencoded'){
406 406
 					$body = $this->streamFactory->createStream($this->buildQuery($body, PHP_QUERY_RFC1738));
407
-				}
408
-				elseif($contentType === 'application/json' || $contentType === 'application/vnd.api+json'){
407
+				} elseif($contentType === 'application/json' || $contentType === 'application/vnd.api+json'){
409 408
 					$body = $this->streamFactory->createStream(json_encode($body));
410 409
 				}
411
-			}
412
-			elseif(is_string($body)){
410
+			} elseif(is_string($body)){
413 411
 				// we don't check if the given string matches the content type - this is the implementor's responsibility
414 412
 				$body = $this->streamFactory->createStream($body);
415 413
 			}
Please login to merge, or discard this patch.