Skip to content

Commit

Permalink
CS & socialite 3.0 (w7corp#1903)
Browse files Browse the repository at this point in the history
* Fix tests.

* CS.
  • Loading branch information
overtrue authored Jul 29, 2020
1 parent 967932c commit 29c711a
Show file tree
Hide file tree
Showing 32 changed files with 211 additions and 155 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ jobs:
- name: Install Dependencies
run: composer install --no-progress
- name: Run PHP-CS-Fxier
run: ./vendor/bin/php-cs-fixer fix --dry-run --diff 1>&2
run: ./composer check-style
63 changes: 41 additions & 22 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,49 @@
<?php

$header = <<<EOF
This file is part of the overtrue/wechat.
(c) overtrue <[email protected]>
This source file is subject to the MIT license that is bundled
with this source code in the file LICENSE.
EOF;

return PhpCsFixer\Config::create()
->setRiskyAllowed(true)
->setRules(array(
'@Symfony' => true,
'header_comment' => array('header' => $header),
'array_syntax' => array('syntax' => 'short'),
'ordered_imports' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'php_unit_construct' => true,
'php_unit_strict' => true,
'yoda_style' => false,
))
->setRules([
'@PSR2' => true,
'binary_operator_spaces' => true,
'blank_line_after_opening_tag' => true,
'compact_nullable_typehint' => true,
'declare_equal_normalize' => true,
'lowercase_cast' => true,
'lowercase_static_reference' => true,
'new_with_braces' => true,
'no_unused_imports' => true,
'no_blank_lines_after_class_opening' => true,
'no_leading_import_slash' => true,
'no_whitespace_in_blank_line' => true,
'ordered_class_elements' => [
'order' => [
'use_trait',
],
],
'ordered_imports' => [
'imports_order' => [
'class',
'function',
'const',
],
'sort_algorithm' => 'none',
],
'return_type_declaration' => true,
'short_scalar_cast' => true,
'single_blank_line_before_namespace' => true,
'single_trait_insert_per_statement' => true,
'ternary_operator_spaces' => true,
'unary_operator_spaces' => true,
'visibility_required' => [
'elements' => [
'const',
'method',
'property',
],
],
])
->setFinder(
PhpCsFixer\Finder::create()
->exclude('vendor')
->in(__DIR__)
->in([__DIR__.'/src/', __DIR__.'/tests/'])
)
;
25 changes: 22 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"easywechat-composer/easywechat-composer": "^1.1",
"guzzlehttp/guzzle": "^6.2 || ^7.0",
"monolog/monolog": "^1.22 || ^2.0",
"overtrue/socialite": "dev-develop",
"overtrue/socialite": "^3.0",
"pimple/pimple": "^3.0",
"psr/simple-cache": "^1.0",
"symfony/cache": "^3.3 || ^4.3 || ^5.0",
Expand All @@ -33,6 +33,7 @@
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.15",
"brainmaestro/composer-git-hooks": "^2.7",
"mikey179/vfsstream": "^1.6",
"mockery/mockery": "^1.2.3",
"phpstan/phpstan": "^0.12.0",
Expand All @@ -52,9 +53,27 @@
"EasyWeChat\\Tests\\": "tests/"
}
},
"extra": {
"hooks": {
"pre-commit": "composer check-style",
"pre-push": [
"composer test",
"composer fix-style"
]
}
},
"scripts": {
"phpcs": "vendor/bin/php-cs-fixer fix",
"post-update-cmd": [
"cghooks update"
],
"post-merge": "composer install",
"post-install-cmd": [
"cghooks add --ignore-lock",
"cghooks update"
],
"phpstan": "vendor/bin/phpstan analyse",
"test": "vendor/bin/phpunit"
"check-style": "php-cs-fixer fix --using-cache=no --diff --config=.php_cs --dry-run --ansi",
"fix-style": "php-cs-fixer fix --using-cache=no --config=.php_cs --ansi",
"test": "vendor/bin/phpunit --colors=always --testdox"
}
}
10 changes: 6 additions & 4 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ parameters:
- src
inferPrivatePropertyTypeFromConstructor: true
ignoreErrors:
- '#EasyWeChat\\MicroMerchant\\Kernel\\BaseClient::__construct\(\) does not call parent constructor from EasyWeChat\\Payment\\Kernel\\BaseClient#'
- '#Unsafe usage of new static\(\).#'
- '#Psr.*Exception is not subtype of Throwable#'
- '#Call to an undefined method Overtrue\\Socialite\\ProviderInterface#'
- '#Access to an undefined property .*\$base#'
- '#Call to an undefined method EasyWeChat\\Payment\\Application::verifySignature\(\)#'
- '#Access to an undefined property EasyWeChat\\Kernel\\Messages\\Raw::\$content#'
- '#Default value of the parameter \#2 \$depth \(INF.0\) of method EasyWeChat\\Kernel\\Support\\Arr::flatten\(\) is incompatible with type int#'
- '#Access to an undefined property EasyWeChat\\Kernel\\ServiceContainer::\$extension#'
- '#Method EasyWeChat\\Kernel\\Contracts\\MessageInterface::transformToXml\(\) invoked with 1 parameter, 0 required#'
- '#Method EasyWeChat\\Kernel\\Support\\Collection::__set_state\(\) should return object but returns array#'
- '#Property EasyWeChat\\Payment\\Kernel\\BaseClient::\$app \(EasyWeChat\\Payment\\Application\) does not accept EasyWeChat\\MicroMerchant\\Application#'
- '#Instantiated class Symfony\\Component\\Cache\\Simple\\FilesystemCache not found.#'
- '#Call to an undefined method Overtrue\\Socialite\\Contracts\\ProviderInterface::scopes\(\)#'
- '#Call to an undefined method Overtrue\\Socialite\\Contracts\\ProviderInterface::withApiAccessToken\(\).#'
- '#Method (.*?)::createDefaultCache\(\) should return Psr\\SimpleCache\\CacheInterface but returns Symfony\\Component\\Cache\\Simple\\FilesystemCache.#'
- '#Property EasyWeChat\\Payment\\Kernel\\BaseClient::\$app \(EasyWeChat\\Payment\\Application\) does not accept EasyWeChat\\MicroMerchant\\Application.#'
14 changes: 7 additions & 7 deletions src/BasicService/QrCode/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ class Client extends BaseClient
*/
protected $baseUri = 'https://api.weixin.qq.com/cgi-bin/';

const DAY = 86400;
const SCENE_MAX_VALUE = 100000;
const SCENE_QR_CARD = 'QR_CARD';
const SCENE_QR_TEMPORARY = 'QR_SCENE';
const SCENE_QR_TEMPORARY_STR = 'QR_STR_SCENE';
const SCENE_QR_FOREVER = 'QR_LIMIT_SCENE';
const SCENE_QR_FOREVER_STR = 'QR_LIMIT_STR_SCENE';
public const DAY = 86400;
public const SCENE_MAX_VALUE = 100000;
public const SCENE_QR_CARD = 'QR_CARD';
public const SCENE_QR_TEMPORARY = 'QR_SCENE';
public const SCENE_QR_TEMPORARY_STR = 'QR_STR_SCENE';
public const SCENE_QR_FOREVER = 'QR_LIMIT_SCENE';
public const SCENE_QR_FOREVER_STR = 'QR_LIMIT_STR_SCENE';

/**
* Create forever QR code.
Expand Down
59 changes: 33 additions & 26 deletions src/Kernel/BaseClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@
*/
class BaseClient
{
use HasHttpRequests { request as performRequest; }
use HasHttpRequests {
request as performRequest;
}

/**
* @var \EasyWeChat\Kernel\ServiceContainer
*/
protected $app;

/**
* @var \EasyWeChat\Kernel\Contracts\AccessTokenInterface
* @var \EasyWeChat\Kernel\Contracts\AccessTokenInterface|null
*/
protected $accessToken;

protected $accessToken = null;
/**
* @var string
*/
Expand Down Expand Up @@ -133,7 +133,11 @@ public function httpUpload(string $url, array $files = [], array $form = [], arr
$multipart[] = compact('name', 'contents');
}

return $this->request($url, 'POST', ['query' => $query, 'multipart' => $multipart, 'connect_timeout' => 30, 'timeout' => 30, 'read_timeout' => 30]);
return $this->request(
$url,
'POST',
['query' => $query, 'multipart' => $multipart, 'connect_timeout' => 30, 'timeout' => 30, 'read_timeout' => 30]
);
}

/**
Expand Down Expand Up @@ -245,27 +249,30 @@ protected function logMiddleware()
*/
protected function retryMiddleware()
{
return Middleware::retry(function (
$retries,
RequestInterface $request,
ResponseInterface $response = null
) {
// Limit the number of retries to 2
if ($retries < $this->app->config->get('http.max_retries', 1) && $response && $body = $response->getBody()) {
// Retry on server errors
$response = json_decode($body, true);

if (!empty($response['errcode']) && in_array(abs($response['errcode']), [40001, 40014, 42001], true)) {
$this->accessToken->refresh();
$this->app['logger']->debug('Retrying with refreshed access token.');

return true;
return Middleware::retry(
function (
$retries,
RequestInterface $request,
ResponseInterface $response = null
) {
// Limit the number of retries to 2
if ($retries < $this->app->config->get('http.max_retries', 1) && $response && $body = $response->getBody()) {
// Retry on server errors
$response = json_decode($body, true);

if (!empty($response['errcode']) && in_array(abs($response['errcode']), [40001, 40014, 42001], true)) {
$this->accessToken->refresh();
$this->app['logger']->debug('Retrying with refreshed access token.');

return true;
}
}
}

return false;
}, function () {
return abs($this->app->config->get('http.retry_delay', 500));
});
return false;
},
function () {
return abs($this->app->config->get('http.retry_delay', 500));
}
);
}
}
2 changes: 2 additions & 0 deletions src/Kernel/Clauses/Clause.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,7 @@ protected function interceptWhereClause($payload)
return true;
}
}

return false;
}
}
26 changes: 13 additions & 13 deletions src/Kernel/Encryptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@

use EasyWeChat\Kernel\Exceptions\RuntimeException;
use EasyWeChat\Kernel\Support\AES;
use function EasyWeChat\Kernel\Support\str_random;
use EasyWeChat\Kernel\Support\XML;
use Throwable;
use function EasyWeChat\Kernel\Support\str_random;

/**
* Class Encryptor.
Expand All @@ -24,18 +24,18 @@
*/
class Encryptor
{
const ERROR_INVALID_SIGNATURE = -40001; // Signature verification failed
const ERROR_PARSE_XML = -40002; // Parse XML failed
const ERROR_CALC_SIGNATURE = -40003; // Calculating the signature failed
const ERROR_INVALID_AES_KEY = -40004; // Invalid AESKey
const ERROR_INVALID_APP_ID = -40005; // Check AppID failed
const ERROR_ENCRYPT_AES = -40006; // AES EncryptionInterface failed
const ERROR_DECRYPT_AES = -40007; // AES decryption failed
const ERROR_INVALID_XML = -40008; // Invalid XML
const ERROR_BASE64_ENCODE = -40009; // Base64 encoding failed
const ERROR_BASE64_DECODE = -40010; // Base64 decoding failed
const ERROR_XML_BUILD = -40011; // XML build failed
const ILLEGAL_BUFFER = -41003; // Illegal buffer
public const ERROR_INVALID_SIGNATURE = -40001; // Signature verification failed
public const ERROR_PARSE_XML = -40002; // Parse XML failed
public const ERROR_CALC_SIGNATURE = -40003; // Calculating the signature failed
public const ERROR_INVALID_AES_KEY = -40004; // Invalid AESKey
public const ERROR_INVALID_APP_ID = -40005; // Check AppID failed
public const ERROR_ENCRYPT_AES = -40006; // AES EncryptionInterface failed
public const ERROR_DECRYPT_AES = -40007; // AES decryption failed
public const ERROR_INVALID_XML = -40008; // Invalid XML
public const ERROR_BASE64_ENCODE = -40009; // Base64 encoding failed
public const ERROR_BASE64_DECODE = -40010; // Base64 decoding failed
public const ERROR_XML_BUILD = -40011; // XML build failed
public const ILLEGAL_BUFFER = -41003; // Illegal buffer

/**
* App id.
Expand Down
32 changes: 16 additions & 16 deletions src/Kernel/Messages/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,22 @@ abstract class Message implements MessageInterface
{
use HasAttributes;

const TEXT = 2;
const IMAGE = 4;
const VOICE = 8;
const VIDEO = 16;
const SHORT_VIDEO = 32;
const LOCATION = 64;
const LINK = 128;
const DEVICE_EVENT = 256;
const DEVICE_TEXT = 512;
const FILE = 1024;
const TEXT_CARD = 2048;
const TRANSFER = 4096;
const EVENT = 1048576;
const MINIPROGRAM_PAGE = 2097152;
const MINIPROGRAM_NOTICE = 4194304;
const ALL = self::TEXT | self::IMAGE | self::VOICE | self::VIDEO | self::SHORT_VIDEO | self::LOCATION | self::LINK
public const TEXT = 2;
public const IMAGE = 4;
public const VOICE = 8;
public const VIDEO = 16;
public const SHORT_VIDEO = 32;
public const LOCATION = 64;
public const LINK = 128;
public const DEVICE_EVENT = 256;
public const DEVICE_TEXT = 512;
public const FILE = 1024;
public const TEXT_CARD = 2048;
public const TRANSFER = 4096;
public const EVENT = 1048576;
public const MINIPROGRAM_PAGE = 2097152;
public const MINIPROGRAM_NOTICE = 4194304;
public const ALL = self::TEXT | self::IMAGE | self::VOICE | self::VIDEO | self::SHORT_VIDEO | self::LOCATION | self::LINK
| self::DEVICE_EVENT | self::DEVICE_TEXT | self::FILE | self::TEXT_CARD | self::TRANSFER | self::EVENT
| self::MINIPROGRAM_PAGE | self::MINIPROGRAM_NOTICE;

Expand Down
2 changes: 0 additions & 2 deletions src/Kernel/Messages/MiniprogramNotice.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
<?php


namespace EasyWeChat\Kernel\Messages;


class MiniprogramNotice extends Message
{
protected $type = 'miniprogram_notice';
Expand Down
4 changes: 2 additions & 2 deletions src/Kernel/ServerGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ class ServerGuard
/**
* Empty string.
*/
const SUCCESS_EMPTY_RESPONSE = 'success';
public const SUCCESS_EMPTY_RESPONSE = 'success';

/**
* @var array
*/
const MESSAGE_TYPE_MAPPING = [
public const MESSAGE_TYPE_MAPPING = [
'text' => Message::TEXT,
'image' => Message::IMAGE,
'voice' => Message::VOICE,
Expand Down
2 changes: 1 addition & 1 deletion src/Kernel/Support/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public static function last(array $array, callable $callback = null, $default =
*
* @return array
*/
public static function flatten(array $array, $depth = INF)
public static function flatten(array $array, $depth = \PHP_INT_MAX)
{
return array_reduce($array, function ($result, $item) use ($depth) {
$item = $item instanceof Collection ? $item->all() : $item;
Expand Down
4 changes: 2 additions & 2 deletions src/Kernel/Traits/Observable.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ public function push($handler, $condition = '*')

return $this->newClause($handler);
}

/**
* @param array $handlers
*
*
* @return $this
*/
public function setHandlers(array $handlers = [])
Expand Down
Loading

0 comments on commit 29c711a

Please sign in to comment.