From e708b725b6e16829796f6fc89dd958b0ff73ff82 Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Wed, 6 May 2015 15:20:53 +0200 Subject: [PATCH] scrutinizer fixes --- src/Exceptions/LinkedInApiException.php | 2 +- src/Http/CurlRequest.php | 2 +- src/Http/UrlGenerator.php | 2 +- src/LinkedIn.php | 77 ++++++++++++++----------- src/Storage/DataStorageInterface.php | 4 +- 5 files changed, 49 insertions(+), 38 deletions(-) diff --git a/src/Exceptions/LinkedInApiException.php b/src/Exceptions/LinkedInApiException.php index 98a8740..5fd25ba 100644 --- a/src/Exceptions/LinkedInApiException.php +++ b/src/Exceptions/LinkedInApiException.php @@ -17,7 +17,7 @@ class LinkedInApiException extends \Exception /** * Make a new API Exception with the given result. * - * @param array $result The result from the API server + * @param mixed $result The result from the API server */ public function __construct($result) { diff --git a/src/Http/CurlRequest.php b/src/Http/CurlRequest.php index 29ef5b8..14495a8 100644 --- a/src/Http/CurlRequest.php +++ b/src/Http/CurlRequest.php @@ -14,7 +14,7 @@ class CurlRequest implements RequestInterface { /** - * @var array lastHeaders + * @var mixed lastHeaders */ protected $lastHeaders; diff --git a/src/Http/UrlGenerator.php b/src/Http/UrlGenerator.php index 718e886..1725de9 100644 --- a/src/Http/UrlGenerator.php +++ b/src/Http/UrlGenerator.php @@ -52,7 +52,7 @@ public function getUrl($name, $path = '', $params = array()) $url .= $path; } - if ($params) { + if (!empty($params)) { //it needs to be PHP_QUERY_RFC3986. We want to have %20 between scopes // we cant run http_build_query($params, null, '&', PHP_QUERY_RFC3986); because it is not supported in php 5.3 or hhvm $url .= '?'; diff --git a/src/LinkedIn.php b/src/LinkedIn.php index 707cbac..0efa5c9 100644 --- a/src/LinkedIn.php +++ b/src/LinkedIn.php @@ -48,7 +48,7 @@ class LinkedIn /** * An array with default user stuff. * - * @var array + * @var mixed */ protected $user; @@ -128,38 +128,17 @@ public function isAuthenticated() */ public function api($method, $resource, array $options = array()) { - /* - * Add token and format - */ + // Add access token to the headers $options['headers']['Authorization'] = sprintf('Bearer %s', (string) $this->getAccessToken()); - if (isset($options['json'])) { - $options['format'] = 'json'; - } elseif (!isset($options['format'])) { - $options['format'] = $this->getFormat(); - } - // Set correct headers for this format - switch ($options['format']) { - case 'simple_xml': - $options['simple_xml'] = true; - case 'xml': - $options['headers']['Content-Type'] = 'text/xml'; - break; - case 'json': - $options['headers']['Content-Type'] = 'application/json'; - $options['headers']['x-li-format'] = 'json'; - $options['query']['format'] = 'json'; - break; - default: - // Do nothing - } - unset($options['format']); + // Do logic and adjustments to the options + $this->filterRequestOption($options); - //generate an url - $url = $this->getUrlGenerator()->getUrl('api', $resource, isset($options['query'])?$options['query']:array()); + // Generate an url + $url = $this->getUrlGenerator()->getUrl('api', $resource, isset($options['query']) ? $options['query'] : array()); unset($options['query']); - //$method that url + // $method that url $result = $this->getRequest()->send($method, $url, $options); return $result; @@ -272,9 +251,9 @@ protected function getUserFromAvailableData() * 1: We got an access token * 2: The access token has changed or if we don't got a user. */ - if ($accessToken && !($user && $persistedAccessToken == $accessToken)) { + if ($accessToken && !($user !== null && $persistedAccessToken == $accessToken)) { $user = $this->getUserFromAccessToken(); - if ($user) { + if ($user !== null) { $storage->set('user', $user); } else { $storage->clearAll(); @@ -360,7 +339,7 @@ public function getAccessToken() } $newAccessToken = $this->fetchNewAccessToken(); - if ($newAccessToken) { + if ($newAccessToken !== null) { $this->setAccessToken($newAccessToken); } @@ -372,7 +351,7 @@ public function getAccessToken() * Determines and returns the user access token using the authorization code. The intent is to * return a valid user access token, or null if one is determined to not be available. * - * @return string|null A valid user access token, or null if one could not be determined. + * @return string|AccessToken|null A valid user access token, or null if one could not be determined. * * @throws LinkedInApiException */ @@ -381,7 +360,7 @@ protected function fetchNewAccessToken() $storage = $this->getStorage(); $code = $this->getCode(); - if ($code) { + if ($code !== null) { $accessToken = $this->getAccessTokenFromCode($code); if ($accessToken) { $storage->set('code', $code); @@ -680,4 +659,36 @@ public function getLastHeaders() { return $this->getRequest()->getHeadersFromLastResponse(); } + + /** + * Modify and filter the request options. Make sure we use the correct query parameters and headers. + * + * @param array $options + */ + protected function filterRequestOption(array &$options) + { + if (isset($options['json'])) { + $options['format'] = 'json'; + } elseif (!isset($options['format'])) { + $options['format'] = $this->getFormat(); + } + + // Set correct headers for this format + switch ($options['format']) { + case 'simple_xml': + $options['simple_xml'] = true; + // simple_xml is still xml. This should fall through + case 'xml': + $options['headers']['Content-Type'] = 'text/xml'; + break; + case 'json': + $options['headers']['Content-Type'] = 'application/json'; + $options['headers']['x-li-format'] = 'json'; + $options['query']['format'] = 'json'; + break; + default: + // Do nothing + } + unset($options['format']); + } } diff --git a/src/Storage/DataStorageInterface.php b/src/Storage/DataStorageInterface.php index f0b0619..b1f9b5a 100644 --- a/src/Storage/DataStorageInterface.php +++ b/src/Storage/DataStorageInterface.php @@ -17,7 +17,7 @@ interface DataStorageInterface * getPersistentData($key) return $value. This call may be in another request. * * @param string $key - * @param array $value + * @param mixed $value */ public function set($key, $value); @@ -25,7 +25,7 @@ public function set($key, $value); * Get the data for $key, persisted by BaseFacebook::setPersistentData(). * * @param string $key The key of the data to retrieve - * @param boolean $default The default value to return if $key is not found + * @param mixed $default The default value to return if $key is not found * * @return mixed */