Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use short array syntax everywhere #414

Merged
merged 1 commit into from
Jan 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ Simple usage looks like:

```php
\Stripe\Stripe::setApiKey('sk_test_BQokikJOvBiI2HlWgH4olfQ2');
$charge = \Stripe\Charge::create(array('amount' => 2000, 'currency' => 'usd', 'source' => 'tok_189fqt2eZvKYlo2CTGBeg6Uq' ));
$charge = \Stripe\Charge::create(['amount' => 2000, 'currency' => 'usd', 'source' => 'tok_189fqt2eZvKYlo2CTGBeg6Uq']);
echo $charge;
```

Expand Down Expand Up @@ -103,7 +103,7 @@ Need to set a proxy for your requests? Pass in the requisite `CURLOPT_*` array t

```php
// set up your tweaked Curl client
$curl = new \Stripe\HttpClient\CurlClient(array(CURLOPT_PROXY => 'proxy.local:80'));
$curl = new \Stripe\HttpClient\CurlClient([CURLOPT_PROXY => 'proxy.local:80']);
// tell Stripe to use the tweaked client
\Stripe\ApiRequestor::setHttpClient($curl);
```
Expand All @@ -125,7 +125,7 @@ end up there instead of `error_log`:
You can access the data from the last API response on any object via `getLastResponse()`.

```php
$charge = \Stripe\Charge::create(array('amount' => 2000, 'currency' => 'usd', 'source' => 'tok_visa'));
$charge = \Stripe\Charge::create(['amount' => 2000, 'currency' => 'usd', 'source' => 'tok_visa']);
echo $charge->getLastResponse()->headers['Request-Id'];
```

Expand All @@ -136,7 +136,7 @@ Stripe's API now requires that [all connections use TLS 1.2](https://stripe.com/
The recommended course of action is to [upgrade your cURL and OpenSSL packages](https://support.stripe.com/questions/how-do-i-upgrade-my-stripe-integration-from-tls-1-0-to-tls-1-2#php) so that TLS 1.2 is used by default, but if that is not possible, you might be able to solve the issue by setting the `CURLOPT_SSLVERSION` option to either `CURL_SSLVERSION_TLSv1` or `CURL_SSLVERSION_TLSv1_2`:

```php
$curl = new \Stripe\HttpClient\CurlClient(array(CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1));
$curl = new \Stripe\HttpClient\CurlClient([CURLOPT_SSLVERSION => CURL_SSLVERSION_TLSv1]);
\Stripe\ApiRequestor::setHttpClient($curl);
```

Expand Down
12 changes: 6 additions & 6 deletions examples/oauth.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
$code = $_GET['code'];

try {
$resp = \Stripe\OAuth::token(array(
$resp = \Stripe\OAuth::token([
'grant_type' => 'authorization_code',
'code' => $code,
));
]);
} catch (\Stripe\Error\OAuth\OAuthBase $e) {
exit("Error: " . $e->getMessage());
}
Expand All @@ -37,9 +37,9 @@
$accountId = $_GET['deauth'];

try {
\Stripe\OAuth::deauthorize(array(
\Stripe\OAuth::deauthorize([
'stripe_user_id' => $accountId,
));
]);
} catch (\Stripe\Error\OAuth\OAuthBase $e) {
exit("Error: " . $e->getMessage());
}
Expand All @@ -48,8 +48,8 @@
echo "<p>Click <a href=\"?\">here</a> to restart the OAuth flow.</p>\n";

} else {
$url = \Stripe\OAuth::authorizeUrl(array(
$url = \Stripe\OAuth::authorizeUrl([
'scope' => 'read_only',
));
]);
echo "<a href=\"$url\">Connect with Stripe</a>\n";
}
4 changes: 2 additions & 2 deletions lib/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ public static function all($params = null, $opts = null)
*/
public function deauthorize($clientId = null, $opts = null)
{
$params = array(
$params = [
'client_id' => $clientId,
'stripe_user_id' => $this->id,
);
];
OAuth::deauthorize($params, $opts);
}

Expand Down
24 changes: 10 additions & 14 deletions lib/ApiRequestor.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private static function _encodeObjects($d)
} elseif ($d === false) {
return 'false';
} elseif (is_array($d)) {
$res = array();
$res = [];
foreach ($d as $k => $v) {
$res[$k] = self::_encodeObjects($v);
}
Expand All @@ -54,17 +54,13 @@ private static function _encodeObjects($d)
*/
public function request($method, $url, $params = null, $headers = null)
{
if (!$params) {
$params = array();
}
if (!$headers) {
$headers = array();
}
$params = $params ?: [];
$headers = $headers ?: [];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opportunistically did a few of these "fixes" which is why the PR has a negative net line count.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1.

list($rbody, $rcode, $rheaders, $myApiKey) =
$this->_requestRaw($method, $url, $params, $headers);
$json = $this->_interpretResponse($rbody, $rcode, $rheaders);
$resp = new ApiResponse($rbody, $rcode, $rheaders, $json);
return array($resp, $myApiKey);
return [$resp, $myApiKey];
}

/**
Expand Down Expand Up @@ -181,13 +177,13 @@ private static function _defaultHeaders($apiKey, $clientInfo = null)
$uname = php_uname();

$appInfo = Stripe::getAppInfo();
$ua = array(
$ua = [
'bindings_version' => Stripe::VERSION,
'lang' => 'php',
'lang_version' => $langVersion,
'publisher' => 'stripe',
'uname' => $uname,
);
];
if ($clientInfo) {
$ua = array_merge($clientInfo, $ua);
}
Expand All @@ -196,11 +192,11 @@ private static function _defaultHeaders($apiKey, $clientInfo = null)
$ua['application'] = $appInfo;
}

$defaultHeaders = array(
$defaultHeaders = [
'X-Stripe-Client-User-Agent' => json_encode($ua),
'User-Agent' => $uaString,
'Authorization' => 'Bearer ' . $apiKey,
);
];
return $defaultHeaders;
}

Expand Down Expand Up @@ -256,7 +252,7 @@ private function _requestRaw($method, $url, $params, $headers)
}

$combinedHeaders = array_merge($defaultHeaders, $headers);
$rawHeaders = array();
$rawHeaders = [];

foreach ($combinedHeaders as $header => $value) {
$rawHeaders[] = $header . ': ' . $value;
Expand All @@ -269,7 +265,7 @@ private function _requestRaw($method, $url, $params, $headers)
$params,
$hasFile
);
return array($rbody, $rcode, $rheaders, $myApiKey);
return [$rbody, $rcode, $rheaders, $myApiKey];
}

private function _processResourceParam($resource, $hasCurlFile)
Expand Down
12 changes: 6 additions & 6 deletions lib/ApiResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
abstract class ApiResource extends StripeObject
{
private static $HEADERS_TO_PERSIST = array('Stripe-Account' => true, 'Stripe-Version' => true);
private static $HEADERS_TO_PERSIST = ['Stripe-Account' => true, 'Stripe-Version' => true];

public static function baseUrl()
{
Expand Down Expand Up @@ -98,18 +98,18 @@ protected static function _validateParams($params = null)
if ($params && !is_array($params)) {
$message = "You must pass an array as the first argument to Stripe API "
. "method calls. (HINT: an example call to create a charge "
. "would be: \"Stripe\\Charge::create(array('amount' => 100, "
. "'currency' => 'usd', 'source' => 'tok_1234'))\")";
. "would be: \"Stripe\\Charge::create(['amount' => 100, "
. "'currency' => 'usd', 'source' => 'tok_1234'])\")";
throw new Error\Api($message);
}
}

protected function _request($method, $url, $params = array(), $options = null)
protected function _request($method, $url, $params = [], $options = null)
{
$opts = $this->_opts->merge($options);
list($resp, $options) = static::_staticRequest($method, $url, $params, $opts);
$this->setLastResponse($resp);
return array($resp->json, $options);
return [$resp->json, $options];
}

protected static function _staticRequest($method, $url, $params, $options)
Expand All @@ -122,7 +122,7 @@ protected static function _staticRequest($method, $url, $params, $options)
unset($opts->headers[$k]);
}
}
return array($response, $opts);
return [$response, $opts];
}

protected static function _retrieve($id, $options = null)
Expand Down
6 changes: 3 additions & 3 deletions lib/Charge.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function updateDispute($params = null, $options = null)
{
$url = $this->instanceUrl() . '/dispute';
list($response, $opts) = $this->_request('post', $url, $params, $options);
$this->refreshFrom(array('dispute' => $response), $opts, true);
$this->refreshFrom(['dispute' => $response], $opts, true);
return $this->dispute;
}

Expand All @@ -162,7 +162,7 @@ public function closeDispute($options = null)
*/
public function markAsFraudulent($opts = null)
{
$params = array('fraud_details' => array('user_report' => 'fraudulent'));
$params = ['fraud_details' => ['user_report' => 'fraudulent']];
$url = $this->instanceUrl();
list($response, $opts) = $this->_request('post', $url, $params, $opts);
$this->refreshFrom($response, $opts);
Expand All @@ -176,7 +176,7 @@ public function markAsFraudulent($opts = null)
*/
public function markAsSafe($opts = null)
{
$params = array('fraud_details' => array('user_report' => 'safe'));
$params = ['fraud_details' => ['user_report' => 'safe']];
$url = $this->instanceUrl();
list($response, $opts) = $this->_request('post', $url, $params, $opts);
$this->refreshFrom($response, $opts);
Expand Down
9 changes: 4 additions & 5 deletions lib/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
class Collection extends ApiResource
{
protected $_requestParams = array();
protected $_requestParams = [];

public function setRequestParams($params)
{
Expand Down Expand Up @@ -76,12 +76,11 @@ private function extractPathAndUpdateParams($params)
if (isset($url['query'])) {
// If the URL contains a query param, parse it out into $params so they
// don't interact weirdly with each other.
$query = array();
$query = [];
parse_str($url['query'], $query);
// PHP 5.2 doesn't support the ?: operator :(
$params = array_merge($params ? $params : array(), $query);
$params = array_merge($params ?: [], $query);
}

return array($url['path'], $params);
return [$url['path'], $params];
}
}
22 changes: 7 additions & 15 deletions lib/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,7 @@ public function delete($params = null, $opts = null)
*/
public function addInvoiceItem($params = null)
{
if (!$params) {
$params = array();
}
$params = $params ?: [];
$params['customer'] = $this->id;
$ii = InvoiceItem::create($params, $this->_opts);
return $ii;
Expand All @@ -117,9 +115,7 @@ public function addInvoiceItem($params = null)
*/
public function invoices($params = null)
{
if (!$params) {
$params = array();
}
$params = $params ?: [];
$params['customer'] = $this->id;
$invoices = Invoice::all($params, $this->_opts);
return $invoices;
Expand All @@ -132,9 +128,7 @@ public function invoices($params = null)
*/
public function invoiceItems($params = null)
{
if (!$params) {
$params = array();
}
$params = $params ?: [];
$params['customer'] = $this->id;
$iis = InvoiceItem::all($params, $this->_opts);
return $iis;
Expand All @@ -147,9 +141,7 @@ public function invoiceItems($params = null)
*/
public function charges($params = null)
{
if (!$params) {
$params = array();
}
$params = $params ?: [];
$params['customer'] = $this->id;
$charges = Charge::all($params, $this->_opts);
return $charges;
Expand All @@ -164,7 +156,7 @@ public function updateSubscription($params = null)
{
$url = $this->instanceUrl() . '/subscription';
list($response, $opts) = $this->_request('post', $url, $params);
$this->refreshFrom(array('subscription' => $response), $opts, true);
$this->refreshFrom(['subscription' => $response], $opts, true);
return $this->subscription;
}

Expand All @@ -177,7 +169,7 @@ public function cancelSubscription($params = null)
{
$url = $this->instanceUrl() . '/subscription';
list($response, $opts) = $this->_request('delete', $url, $params);
$this->refreshFrom(array('subscription' => $response), $opts, true);
$this->refreshFrom(['subscription' => $response], $opts, true);
return $this->subscription;
}

Expand All @@ -188,7 +180,7 @@ public function deleteDiscount()
{
$url = $this->instanceUrl() . '/discount';
list($response, $opts) = $this->_request('delete', $url);
$this->refreshFrom(array('discount' => null), $opts, true);
$this->refreshFrom(['discount' => null], $opts, true);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/HttpClient/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface ClientInterface
* @param boolean $hasFile Whether or not $params references a file (via an @ prefix or
* CurlFile)
* @throws \Stripe\Error\Api & \Stripe\Error\ApiConnection
* @return array($rawBody, $httpStatusCode, $httpHeader)
* @return [$rawBody, $httpStatusCode, $httpHeader]
*/
public function request($method, $absUrl, $headers, $params, $hasFile);
}
10 changes: 5 additions & 5 deletions lib/HttpClient/CurlClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ public function __construct($defaultOptions = null)
public function initUserAgentInfo()
{
$curlVersion = curl_version();
$this->userAgentInfo = array(
$this->userAgentInfo = [
'httplib' => 'curl ' . $curlVersion['version'],
'ssllib' => $curlVersion['ssl_version'],
);
];
}

public function getDefaultOptions()
Expand Down Expand Up @@ -113,7 +113,7 @@ public function request($method, $absUrl, $headers, $params, $hasFile)
$curl = curl_init();
$method = strtolower($method);

$opts = array();
$opts = [];
if (is_callable($this->defaultOptions)) { // call defaultOptions callback, set options to return value
$opts = call_user_func_array($this->defaultOptions, func_get_args());
if (!is_array($opts)) {
Expand Down Expand Up @@ -148,7 +148,7 @@ public function request($method, $absUrl, $headers, $params, $hasFile)
}

// Create a callback to capture HTTP headers for the response
$rheaders = array();
$rheaders = [];
$headerCallback = function ($curl, $header_line) use (&$rheaders) {
// Ignore the HTTP request line (HTTP/1.1 200 OK)
if (strpos($header_line, ":") === false) {
Expand Down Expand Up @@ -215,7 +215,7 @@ public function request($method, $absUrl, $headers, $params, $hasFile)

$rcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
return array($rbody, $rcode, $rheaders);
return [$rbody, $rcode, $rheaders];
}

/**
Expand Down
9 changes: 2 additions & 7 deletions lib/OAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ abstract class OAuth
*/
public static function authorizeUrl($params = null, $opts = null)
{
if (!$params) {
$params = array();
}
$params = $params ?: [];

$base = ($opts && array_key_exists('connect_base', $opts)) ? $opts['connect_base'] : Stripe::$connectBase;

Expand Down Expand Up @@ -61,10 +59,7 @@ public static function token($params = null, $opts = null)
*/
public static function deauthorize($params = null, $opts = null)
{
if (!$params) {
$params = array();
}

$params = $params ?: [];
$base = ($opts && array_key_exists('connect_base', $opts)) ? $opts['connect_base'] : Stripe::$connectBase;
$requestor = new ApiRequestor(null, $base);
$params['client_id'] = self::_getClientId($params);
Expand Down
Loading