Skip to content
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
21 changes: 21 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

$finder = PhpCsFixer\Finder::create()
->in(__DIR__)
->exclude('vendor')
->path('lib')
->path('init.php')
->path('build.php')
->path('tests')
->path('.php-cs-fixer.php');

$config = new PhpCsFixer\Config();
return $config->setRules([
'@PSR12' => true,
'strict_param' => true,
'array_syntax' => ['syntax' => 'short'],
'no_unused_imports' => true,
// Don't force public keyword for const (for compatibility with PHP < 7.1)
'visibility_required' => ['elements' => ['property', 'method']],
])
->setFinder($finder);
28 changes: 22 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
PHP 5.6 and later.

> Even if it is not a corresponding version, it may work, but it does not support it.
Due to the PHP [END OF LIFE](http://php.net/supported-versions.php) cycle.
Due to the PHP [END OF LIFE](http://php.net/supported-versions.php) cycle.

## Composer

Expand Down Expand Up @@ -55,12 +55,12 @@ echo $charge->amount; // 2000
## Retry on HTTP Status Code 429

- See [Rate Limit Guideline](https://pay.jp/docs/guideline-rate-limit#2-%E3%83%AA%E3%83%88%E3%83%A9%E3%82%A4)
- When you exceeded rate-limit, you can retry request by setting `$maxRetry`
- When you exceeded rate-limit, you can retry request by setting `$maxRetry`
like `\Payjp\Payjp::setMaxRetry(3);` .
- The retry interval base value is `$retryInitialDelay`
Adjust the value like `\Payjp\Payjp::setRetryInitialDelay(4);`
- The retry interval base value is `$retryInitialDelay`
Adjust the value like `\Payjp\Payjp::setRetryInitialDelay(4);`
The smaller is shorter.
- The retry interval calcurating is based on "Exponential backoff with equal jitter" algorithm.
- The retry interval calcurating is based on "Exponential backoff with equal jitter" algorithm.
See https://aws.amazon.com/jp/blogs/architecture/exponential-backoff-and-jitter/

## Logging
Expand All @@ -78,7 +78,9 @@ echo $charge->amount; // 2000

- When you access inaccessible or non-existing property

## Tests
## Development

### Running Tests

In order to run tests first install [PHPUnit](http://packagist.org/packages/phpunit/phpunit) via [Composer](http://getcomposer.org/):

Expand All @@ -87,3 +89,17 @@ In order to run tests first install [PHPUnit](http://packagist.org/packages/phpu
To run the test suite:

./vendor/bin/phpunit

You can also use Composer to run the tests:

composer test

This will run both the code style checks (PSR2) and the PHPUnit tests.

### Code Formatting

To format the code according to our standards:

composer fix

This command requires PHP 7.4 or higher and will automatically fix coding style issues using PHP-CS-Fixer.
1 change: 1 addition & 0 deletions build.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env php
<?php

chdir(dirname(__FILE__));

$autoload = (int)$argv[1];
Expand Down
10 changes: 10 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@
"test": [
"phpcs --standard=PSR2 -n lib tests *.php",
"phpunit"
],
"fix": [
"@php -r \"if (PHP_VERSION_ID < 70400) { echo 'PHP 7.4 or higher is required. Current version: ' . PHP_VERSION . PHP_EOL; exit(1); }\"",
"PHP_CS_FIXER_IGNORE_ENV=1 ./vendor/bin/php-cs-fixer fix --allow-risky=yes"
]
},
"config": {
"platform-check": false
},
"suggest": {
"friendsofphp/php-cs-fixer": "Required for the fix command (requires PHP >= 7.4)"
}
}
2 changes: 1 addition & 1 deletion lib/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function instanceUrl()
return parent::instanceUrl();
}
}

/**
* @param string|null $id
* @param array|string|null $opts
Expand Down
20 changes: 10 additions & 10 deletions lib/ApiRequestor.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,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 Down Expand Up @@ -68,10 +68,10 @@ private function getRetryDelay($retryCount)
public function request($method, $url, $params = null, $headers = null)
{
if (!$params) {
$params = array();
$params = [];
}
if (!$headers) {
$headers = array();
$headers = [];
}

for ($i = 0; $i <= Payjp::getMaxRetry(); $i++) {
Expand All @@ -85,7 +85,7 @@ public function request($method, $url, $params = null, $headers = null)
}
}
$resp = $this->_interpretResponse($rbody, $rcode);
return array($resp, $myApiKey);
return [$resp, $myApiKey];
}

/**
Expand Down Expand Up @@ -145,18 +145,18 @@ private function _requestRaw($method, $url, $params, $headers)
$params = self::_encodeObjects($params);
$langVersion = phpversion();
$uname = php_uname();
$ua = array(
$ua = [
'bindings_version' => Payjp::VERSION,
'lang' => 'php',
'lang_version' => $langVersion,
'publisher' => 'payjp',
'uname' => $uname,
);
$defaultHeaders = array(
];
$defaultHeaders = [
'X-Payjp-Client-User-Agent' => json_encode($ua),
'User-Agent' => 'Payjp/v1 PhpBindings/' . Payjp::VERSION,
'Authorization' => 'Basic ' . base64_encode($myApiKey.':')
);
];
if (Payjp::$apiVersion) {
$defaultHeaders['Payjp-Version'] = Payjp::$apiVersion;
}
Expand All @@ -178,7 +178,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 @@ -191,7 +191,7 @@ private function _requestRaw($method, $url, $params, $headers)
$params,
$hasFile
);
return array($rbody, $rcode, $myApiKey);
return [$rbody, $rcode, $myApiKey];
}

private function _processResourceParam($resource, $hasCurlFile)
Expand Down
6 changes: 3 additions & 3 deletions lib/ApiResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

abstract class ApiResource extends PayjpObject
{
private static $HEADERS_TO_PERSIST = array('Payjp-Account' => true, 'Payjp-Version' => true);
private static $HEADERS_TO_PERSIST = ['Payjp-Account' => true, 'Payjp-Version' => true];

/**
* @return ApiResource The refreshed resource.
Expand Down Expand Up @@ -95,7 +95,7 @@ private static function _validateParams($params = null)
*
* @return list(array, RequestOptions)
*/
protected function _request($method, $url, $params = array(), $options = null)
protected function _request($method, $url, $params = [], $options = null)
{
$opts = $this->_opts->merge($options);
return static::_staticRequest($method, $url, $params, $opts);
Expand All @@ -111,7 +111,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
1 change: 0 additions & 1 deletion lib/Card.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@

class Card extends ExternalAccount
{

}
6 changes: 3 additions & 3 deletions lib/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ 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 ? $params : [], $query);
}

return array($url['path'], $params);
return [$url['path'], $params];
}
}
4 changes: 2 additions & 2 deletions lib/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ private static function _validateParams($params = null)
. "would be: \"Payjp\\Charge::create(array('amount' => 100, "
. "'currency' => 'usd', 'card' => array('number' => "
. "4242424242424242, 'exp_month' => 5, 'exp_year' => 2015)))\")";
throw new Error\Api($message);
throw new Error\Api($message);
}
}
/**
Expand Down Expand Up @@ -77,7 +77,7 @@ public function delete($params = null, $opts = null)
public function charges($params = null)
{
if (!$params) {
$params = array();
$params = [];
}
$params['customer'] = $this->id;
$charges = Charge::all($params, $this->_opts);
Expand Down
6 changes: 3 additions & 3 deletions lib/HttpClient/CurlClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function request($method, $absUrl, $headers, $params, $hasFile)
{
$curl = curl_init();
$method = strtolower($method);
$opts = array();
$opts = [];
if ($method == 'get') {
if ($hasFile) {
throw new Error\Api(
Expand Down Expand Up @@ -89,7 +89,7 @@ public function request($method, $absUrl, $headers, $params, $hasFile)

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

/**
Expand Down Expand Up @@ -142,7 +142,7 @@ public static function encode($arr, $prefix = null)
return $arr;
}

$r = array();
$r = [];
foreach ($arr as $k => $v) {
if (is_null($v)) {
continue;
Expand Down
4 changes: 2 additions & 2 deletions lib/Logger/DefaultLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

class DefaultLogger implements LoggerInterface
{
public function error($message, array $context = array())
public function error($message, array $context = [])
{
error_log($message);
}

public function info($message, array $context = array())
public function info($message, array $context = [])
{
// if you need any output, please use \Payjp\Payjp::setLogger
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Logger/LoggerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface LoggerInterface
* @param array $context
* @return void
*/
public function error($message, array $context = array());
public function error($message, array $context = []);

/**
* Interesting events.
Expand All @@ -26,5 +26,5 @@ public function error($message, array $context = array());
* @param array $context
* @return void
*/
public function info($message, array $context = array());
public function info($message, array $context = []);
}
15 changes: 7 additions & 8 deletions lib/PayjpObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Payjp;

use ArrayAccess;
use InvalidArgumentException;

class PayjpObject implements ArrayAccess
{
Expand All @@ -20,11 +19,11 @@ class PayjpObject implements ArrayAccess

public static function init()
{
self::$permanentAttributes = new Util\Set(array('_opts', 'id'));
self::$nestedUpdatableAttributes = new Util\Set(array(
self::$permanentAttributes = new Util\Set(['_opts', 'id']);
self::$nestedUpdatableAttributes = new Util\Set([
'summary', 'accounts_enabled', 'merchant',
0, 1, 2, 3, 4 // Max 3, but leave the 4th so errors work properly
));
]);
}

protected $_opts;
Expand All @@ -36,11 +35,11 @@ public static function init()
public function __construct($id = null, $opts = null)
{
$this->_opts = $opts ? $opts : new Util\RequestOptions();
$this->_values = array();
$this->_values = [];
$this->_unsavedValues = new Util\Set();
$this->_transientValues = new Util\Set();

$this->_retrieveOptions = array();
$this->_retrieveOptions = [];
if (is_array($id)) {
foreach ($id as $key => $value) {
if ($key != 'id') {
Expand Down Expand Up @@ -84,7 +83,7 @@ public function &__get($k)
{
if (array_key_exists($k, $this->_values)) {
return $this->_values[$k];
} else if ($this->_transientValues->includes($k)) {
} elseif ($this->_transientValues->includes($k)) {
$class = get_class($this);
$attrs = join(', ', array_keys($this->_values));
$message = "Payjp Notice: Undefined property of $class instance: $k. "
Expand Down Expand Up @@ -197,7 +196,7 @@ public function refreshFrom($values, $opts, $partial = false)
*/
public function serializeParameters()
{
$params = array();
$params = [];
if ($this->_unsavedValues) {
foreach ($this->_unsavedValues->toArray() as $k) {
$v = $this->$k;
Expand Down
Loading