From 8b2cb216ac2a4c6efba71e8ac6477103c37c58b0 Mon Sep 17 00:00:00 2001 From: reatang Date: Fri, 18 Aug 2023 16:44:45 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E8=A7=A3=E5=86=B3response=E4=B8=8D?= =?UTF-8?q?=E5=AD=98=E5=9C=A8header=E7=9A=84=E6=97=B6=E5=80=99=EF=BC=8Cmax?= =?UTF-8?q?=E6=8A=A5=E9=94=99=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Kernel/ServerResponse.php | 16 +++++++++------- tests/Kernel/ServerResponseTest.php | 16 +++++++++++++--- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/Kernel/ServerResponse.php b/src/Kernel/ServerResponse.php index 7ddcc1226..a0d6e6de0 100644 --- a/src/Kernel/ServerResponse.php +++ b/src/Kernel/ServerResponse.php @@ -187,16 +187,18 @@ public static function closeOutputBuffers(int $targetLevel, bool $flush): void public function __toString(): string { $headers = $this->getHeaders(); + $headersString = ''; - ksort($headers); + if (!empty($headers)) { + ksort($headers); - $max = max(array_map('strlen', array_keys($headers))) + 1; - $headersString = ''; + $max = max(array_map('strlen', array_keys($headers))) + 1; - foreach ($headers as $name => $values) { - $name = ucwords($name, '-'); - foreach ($values as $value) { - $headersString .= sprintf("%-{$max}s %s\r\n", $name.':', $value); + foreach ($headers as $name => $values) { + $name = ucwords($name, '-'); + foreach ($values as $value) { + $headersString .= sprintf("%-{$max}s %s\r\n", $name.':', $value); + } } } diff --git a/tests/Kernel/ServerResponseTest.php b/tests/Kernel/ServerResponseTest.php index d173b8ead..cb9655bc5 100644 --- a/tests/Kernel/ServerResponseTest.php +++ b/tests/Kernel/ServerResponseTest.php @@ -12,9 +12,19 @@ public function test_to_string() { $response = ServerResponse::make(new Response(200, ['X-Foo' => 'bar'], 'foo')); - $response = explode("\r\n", $response); - $this->assertEquals('HTTP/1.1 200 OK', $response[0]); - $this->assertEquals('X-Foo: bar', $response[1]); + $responseLines = explode("\r\n", $response); + $this->assertEquals('HTTP/1.1 200 OK', $responseLines[0]); + $this->assertEquals('X-Foo: bar', $responseLines[1]); + $this->assertEquals('foo', $responseLines[3]); + } + + public function test_to_string_without_headers() + { + $response = ServerResponse::make(new Response(200, [], 'foo')); + + $responseLines = explode("\r\n", $response); + $this->assertEquals('HTTP/1.1 200 OK', $responseLines[0]); + $this->assertEquals('foo', $responseLines[2]); } public function test_it_can_send_response() From c93a92b23a83faf84742c0f6e7db22b2e876ff61 Mon Sep 17 00:00:00 2001 From: reatang Date: Fri, 18 Aug 2023 17:05:30 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=8C=89=E7=85=A7=E9=A2=84=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E4=BB=A3=E7=A0=81=E9=A3=8E=E6=A0=BC=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Kernel/Config.php | 1 + src/Kernel/Encryptor.php | 16 +++++---- src/Kernel/Form/File.php | 26 +++++++------- .../HttpClient/AccessTokenAwareClient.php | 5 +-- src/Kernel/HttpClient/RequestUtil.php | 16 +++++---- src/Kernel/HttpClient/RequestWithPresets.php | 3 +- src/Kernel/HttpClient/Response.php | 36 ++++++++++--------- src/Kernel/ServerResponse.php | 16 +++++---- src/Kernel/Support/AesCbc.php | 6 ++-- src/Kernel/Support/AesEcb.php | 6 ++-- src/Kernel/Support/AesGcm.php | 8 +++-- src/Kernel/Support/Arr.php | 3 +- src/Kernel/Support/PrivateKey.php | 3 +- src/Kernel/Support/PublicKey.php | 1 + src/Kernel/Support/Str.php | 3 +- src/Kernel/Support/UserAgent.php | 3 +- src/Kernel/Support/Xml.php | 2 +- src/Kernel/Traits/InteractWithConfig.php | 1 + src/Kernel/Traits/InteractWithHandlers.php | 7 ++-- src/Kernel/Traits/InteractWithHttpClient.php | 3 +- src/Kernel/Traits/RespondXmlMessage.php | 11 +++--- src/MiniApp/Application.php | 7 ++-- src/MiniApp/Decryptor.php | 5 +-- src/OfficialAccount/AccessToken.php | 13 +++---- src/OfficialAccount/Application.php | 9 ++--- src/OfficialAccount/JsApiTicket.php | 1 + src/OfficialAccount/Server.php | 6 ++-- src/OfficialAccount/Utils.php | 1 + src/OpenPlatform/Application.php | 9 ++--- src/OpenPlatform/ComponentAccessToken.php | 11 +++--- src/OpenPlatform/Server.php | 9 ++--- src/OpenPlatform/VerifyTicket.php | 7 ++-- src/OpenWork/Application.php | 15 ++++---- src/OpenWork/AuthorizerAccessToken.php | 4 +-- src/OpenWork/JsApiTicket.php | 11 +++--- src/OpenWork/ProviderAccessToken.php | 10 +++--- src/OpenWork/Server.php | 11 +++--- src/OpenWork/SuiteAccessToken.php | 14 ++++---- src/OpenWork/SuiteTicket.php | 7 ++-- src/Pay/Client.php | 9 ++--- src/Pay/LegacySignature.php | 3 +- src/Pay/Merchant.php | 3 +- src/Pay/Message.php | 3 +- src/Pay/Server.php | 13 +++---- src/Pay/Signature.php | 5 +-- src/Pay/URLSchemeBuilder.php | 1 + src/Pay/Utils.php | 7 ++-- src/Work/AccessToken.php | 16 +++++---- src/Work/Application.php | 3 +- src/Work/JsApiTicket.php | 11 +++--- src/Work/Server.php | 6 ++-- src/Work/Utils.php | 1 + 52 files changed, 233 insertions(+), 174 deletions(-) diff --git a/src/Kernel/Config.php b/src/Kernel/Config.php index eb1173a79..36d087a9c 100644 --- a/src/Kernel/Config.php +++ b/src/Kernel/Config.php @@ -9,6 +9,7 @@ use EasyWeChat\Kernel\Exceptions\InvalidArgumentException; use EasyWeChat\Kernel\Support\Arr; use JetBrains\PhpStorm\Pure; + use function strval; /** diff --git a/src/Kernel/Encryptor.php b/src/Kernel/Encryptor.php index af94eb6a7..8e9d3c9b7 100644 --- a/src/Kernel/Encryptor.php +++ b/src/Kernel/Encryptor.php @@ -4,25 +4,27 @@ namespace EasyWeChat\Kernel; -use function base64_decode; -use function base64_encode; +use const OPENSSL_NO_PADDING; +use const SORT_STRING; + use EasyWeChat\Kernel\Exceptions\RuntimeException; use EasyWeChat\Kernel\Support\Pkcs7; use EasyWeChat\Kernel\Support\Str; use EasyWeChat\Kernel\Support\Xml; use Exception; +use Throwable; + +use function base64_decode; +use function base64_encode; use function implode; use function openssl_decrypt; use function openssl_encrypt; -use const OPENSSL_NO_PADDING; use function pack; use function random_bytes; use function sha1; use function sort; -use const SORT_STRING; use function strlen; use function substr; -use Throwable; use function time; use function trim; use function unpack; @@ -63,7 +65,7 @@ class Encryptor protected ?string $receiveId = null; - public function __construct(string $appId, string $token, string $aesKey, ?string $receiveId = null) + public function __construct(string $appId, string $token, string $aesKey, string $receiveId = null) { $this->appId = $appId; $this->token = $token; @@ -80,7 +82,7 @@ public function getToken(): string * @throws RuntimeException * @throws Exception */ - public function encrypt(string $plaintext, string|null $nonce = null, int|string $timestamp = null): string + public function encrypt(string $plaintext, string $nonce = null, int|string $timestamp = null): string { try { $plaintext = Pkcs7::padding(random_bytes(16).pack('N', strlen($plaintext)).$plaintext.$this->appId, 32); diff --git a/src/Kernel/Form/File.php b/src/Kernel/Form/File.php index 8237a66ef..ae29f9d19 100644 --- a/src/Kernel/Form/File.php +++ b/src/Kernel/Form/File.php @@ -2,14 +2,16 @@ namespace EasyWeChat\Kernel\Form; +use const PATHINFO_EXTENSION; + use EasyWeChat\Kernel\Exceptions\RuntimeException; +use Symfony\Component\Mime\MimeTypes; +use Symfony\Component\Mime\Part\DataPart; + use function file_put_contents; use function md5; use function pathinfo; -use const PATHINFO_EXTENSION; use function strtolower; -use Symfony\Component\Mime\MimeTypes; -use Symfony\Component\Mime\Part\DataPart; use function sys_get_temp_dir; use function tempnam; @@ -20,9 +22,9 @@ class File extends DataPart */ public static function from( string $pathOrContents, - ?string $filename = null, - ?string $contentType = null, - ?string $encoding = null + string $filename = null, + string $contentType = null, + string $encoding = null ): DataPart { if (file_exists($pathOrContents)) { return static::fromPath($pathOrContents, $filename, $contentType); @@ -36,9 +38,9 @@ public static function from( */ public static function fromContents( string $contents, - ?string $filename = null, - ?string $contentType = null, - ?string $encoding = null + string $filename = null, + string $contentType = null, + string $encoding = null ): DataPart { if (null === $contentType) { $mimeTypes = new MimeTypes(); @@ -68,9 +70,9 @@ public static function fromContents( */ public static function withContents( string $contents, - ?string $filename = null, - ?string $contentType = null, - ?string $encoding = null + string $filename = null, + string $contentType = null, + string $encoding = null ): DataPart { return self::fromContents(...func_get_args()); } diff --git a/src/Kernel/HttpClient/AccessTokenAwareClient.php b/src/Kernel/HttpClient/AccessTokenAwareClient.php index 06308154f..80c3e829e 100644 --- a/src/Kernel/HttpClient/AccessTokenAwareClient.php +++ b/src/Kernel/HttpClient/AccessTokenAwareClient.php @@ -4,7 +4,6 @@ namespace EasyWeChat\Kernel\HttpClient; -use function array_merge; use Closure; use EasyWeChat\Kernel\Contracts\AccessToken as AccessTokenInterface; use EasyWeChat\Kernel\Contracts\AccessTokenAwareHttpClient as AccessTokenAwareHttpClientInterface; @@ -15,6 +14,8 @@ use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; use Symfony\Contracts\HttpClient\HttpClientInterface; +use function array_merge; + /** * Class AccessTokenAwareClient. * @@ -31,7 +32,7 @@ class AccessTokenAwareClient implements AccessTokenAwareHttpClientInterface use RequestWithPresets; public function __construct( - ?HttpClientInterface $client = null, + HttpClientInterface $client = null, protected ?AccessTokenInterface $accessToken = null, protected ?Closure $failureJudge = null, protected bool $throw = true diff --git a/src/Kernel/HttpClient/RequestUtil.php b/src/Kernel/HttpClient/RequestUtil.php index 3955b3aa9..81bc025b9 100644 --- a/src/Kernel/HttpClient/RequestUtil.php +++ b/src/Kernel/HttpClient/RequestUtil.php @@ -3,23 +3,25 @@ namespace EasyWeChat\Kernel\HttpClient; use const ARRAY_FILTER_USE_KEY; -use function array_key_exists; +use const JSON_FORCE_OBJECT; +use const JSON_UNESCAPED_UNICODE; + use EasyWeChat\Kernel\Support\UserAgent; use EasyWeChat\Kernel\Support\Xml; -use function in_array; use InvalidArgumentException; -use function is_array; -use function is_string; use JetBrains\PhpStorm\ArrayShape; -use function json_encode; -use const JSON_FORCE_OBJECT; -use const JSON_UNESCAPED_UNICODE; use Nyholm\Psr7\Factory\Psr17Factory; use Nyholm\Psr7Server\ServerRequestCreator; use Psr\Http\Message\ServerRequestInterface; use Symfony\Component\HttpClient\Retry\GenericRetryStrategy; use Symfony\Contracts\HttpClient\HttpClientInterface; +use function array_key_exists; +use function in_array; +use function is_array; +use function is_string; +use function json_encode; + class RequestUtil { /** diff --git a/src/Kernel/HttpClient/RequestWithPresets.php b/src/Kernel/HttpClient/RequestWithPresets.php index aaea86826..79ac016ef 100644 --- a/src/Kernel/HttpClient/RequestWithPresets.php +++ b/src/Kernel/HttpClient/RequestWithPresets.php @@ -2,12 +2,13 @@ namespace EasyWeChat\Kernel\HttpClient; -use function array_merge; use EasyWeChat\Kernel\Exceptions\InvalidArgumentException; use EasyWeChat\Kernel\Exceptions\RuntimeException; use EasyWeChat\Kernel\Form\File; use EasyWeChat\Kernel\Form\Form; use EasyWeChat\Kernel\Support\Str; + +use function array_merge; use function in_array; use function is_file; use function is_string; diff --git a/src/Kernel/HttpClient/Response.php b/src/Kernel/HttpClient/Response.php index 74484eb99..b84268358 100644 --- a/src/Kernel/HttpClient/Response.php +++ b/src/Kernel/HttpClient/Response.php @@ -2,27 +2,20 @@ namespace EasyWeChat\Kernel\HttpClient; -use function array_key_exists; +use const JSON_UNESCAPED_UNICODE; + use ArrayAccess; -use function base64_encode; use Closure; use EasyWeChat\Kernel\Contracts\Arrayable; use EasyWeChat\Kernel\Contracts\Jsonable; use EasyWeChat\Kernel\Exceptions\BadMethodCallException; use EasyWeChat\Kernel\Exceptions\BadResponseException; use EasyWeChat\Kernel\Support\Xml; -use function file_put_contents; use Http\Discovery\Exception\NotFoundException; use Http\Discovery\Psr17FactoryDiscovery; -use function json_encode; -use const JSON_UNESCAPED_UNICODE; use Nyholm\Psr7\Factory\Psr17Factory; use Psr\Http\Message\ResponseFactoryInterface; use Psr\Http\Message\StreamFactoryInterface; -use function sprintf; -use function str_contains; -use function str_starts_with; -use function strtolower; use Symfony\Component\HttpClient\Response\MockResponse; use Symfony\Component\HttpClient\Response\StreamableInterface; use Symfony\Component\HttpClient\Response\StreamWrapper; @@ -34,6 +27,15 @@ use Symfony\Contracts\HttpClient\ResponseInterface; use Throwable; +use function array_key_exists; +use function base64_encode; +use function file_put_contents; +use function json_encode; +use function sprintf; +use function str_contains; +use function str_starts_with; +use function strtolower; + /** * @implements \ArrayAccess * @@ -110,7 +112,7 @@ public function isFailed(): bool * @throws ClientExceptionInterface * @throws BadResponseException */ - public function toArray(?bool $throw = null): array + public function toArray(bool $throw = null): array { $throw ??= $this->throw; @@ -141,7 +143,7 @@ public function toArray(?bool $throw = null): array * @throws ClientExceptionInterface * @throws BadResponseException */ - public function toJson(?bool $throw = null): string|false + public function toJson(bool $throw = null): string|false { return json_encode($this->toArray($throw), JSON_UNESCAPED_UNICODE); } @@ -149,7 +151,7 @@ public function toJson(?bool $throw = null): string|false /** * {@inheritdoc} */ - public function toStream(?bool $throw = null) + public function toStream(bool $throw = null) { if ($this->response instanceof StreamableInterface) { return $this->response->toStream($throw ?? $this->throw); @@ -288,12 +290,12 @@ public function getStatusCode(): int return $this->response->getStatusCode(); } - public function getHeaders(?bool $throw = null): array + public function getHeaders(bool $throw = null): array { return $this->response->getHeaders($throw ?? $this->throw); } - public function getContent(?bool $throw = null): string + public function getContent(bool $throw = null): string { return $this->response->getContent($throw ?? $this->throw); } @@ -327,7 +329,7 @@ public function __toString(): string * @throws RedirectionExceptionInterface * @throws ClientExceptionInterface */ - public function hasHeader(string $name, ?bool $throw = null): bool + public function hasHeader(string $name, bool $throw = null): bool { return isset($this->getHeaders($throw)[$name]); } @@ -340,7 +342,7 @@ public function hasHeader(string $name, ?bool $throw = null): bool * @throws RedirectionExceptionInterface * @throws ClientExceptionInterface */ - public function getHeader(string $name, ?bool $throw = null): array + public function getHeader(string $name, bool $throw = null): array { $name = strtolower($name); $throw ??= $this->throw; @@ -354,7 +356,7 @@ public function getHeader(string $name, ?bool $throw = null): array * @throws RedirectionExceptionInterface * @throws ClientExceptionInterface */ - public function getHeaderLine(string $name, ?bool $throw = null): string + public function getHeaderLine(string $name, bool $throw = null): string { $name = strtolower($name); $throw ??= $this->throw; diff --git a/src/Kernel/ServerResponse.php b/src/Kernel/ServerResponse.php index a0d6e6de0..c51b8c189 100644 --- a/src/Kernel/ServerResponse.php +++ b/src/Kernel/ServerResponse.php @@ -2,17 +2,19 @@ namespace EasyWeChat\Kernel; -use function array_keys; -use function array_map; -use function count; -use function header; -use JetBrains\PhpStorm\Pure; -use function max; use const PHP_OUTPUT_HANDLER_CLEANABLE; use const PHP_OUTPUT_HANDLER_FLUSHABLE; use const PHP_OUTPUT_HANDLER_REMOVABLE; + +use JetBrains\PhpStorm\Pure; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\StreamInterface; + +use function array_keys; +use function array_map; +use function count; +use function header; +use function max; use function sprintf; use function ucwords; @@ -189,7 +191,7 @@ public function __toString(): string $headers = $this->getHeaders(); $headersString = ''; - if (!empty($headers)) { + if (! empty($headers)) { ksort($headers); $max = max(array_map('strlen', array_keys($headers))) + 1; diff --git a/src/Kernel/Support/AesCbc.php b/src/Kernel/Support/AesCbc.php index 36f80ee6d..4e3714ab1 100644 --- a/src/Kernel/Support/AesCbc.php +++ b/src/Kernel/Support/AesCbc.php @@ -2,12 +2,14 @@ namespace EasyWeChat\Kernel\Support; -use function base64_decode; +use const OPENSSL_RAW_DATA; + use EasyWeChat\Kernel\Contracts\Aes; use EasyWeChat\Kernel\Exceptions\InvalidArgumentException; + +use function base64_decode; use function openssl_decrypt; use function openssl_error_string; -use const OPENSSL_RAW_DATA; class AesCbc implements Aes { diff --git a/src/Kernel/Support/AesEcb.php b/src/Kernel/Support/AesEcb.php index 1ed5f424c..863008615 100644 --- a/src/Kernel/Support/AesEcb.php +++ b/src/Kernel/Support/AesEcb.php @@ -2,12 +2,14 @@ namespace EasyWeChat\Kernel\Support; -use function base64_decode; +use const OPENSSL_RAW_DATA; + use EasyWeChat\Kernel\Contracts\Aes; use EasyWeChat\Kernel\Exceptions\InvalidArgumentException; + +use function base64_decode; use function openssl_decrypt; use function openssl_error_string; -use const OPENSSL_RAW_DATA; class AesEcb implements Aes { diff --git a/src/Kernel/Support/AesGcm.php b/src/Kernel/Support/AesGcm.php index 83b2a2847..c40256342 100644 --- a/src/Kernel/Support/AesGcm.php +++ b/src/Kernel/Support/AesGcm.php @@ -2,14 +2,16 @@ namespace EasyWeChat\Kernel\Support; -use function base64_decode; -use function base64_encode; +use const OPENSSL_RAW_DATA; + use EasyWeChat\Kernel\Contracts\Aes; use EasyWeChat\Kernel\Exceptions\InvalidArgumentException; + +use function base64_decode; +use function base64_encode; use function openssl_decrypt; use function openssl_encrypt; use function openssl_error_string; -use const OPENSSL_RAW_DATA; class AesGcm implements Aes { diff --git a/src/Kernel/Support/Arr.php b/src/Kernel/Support/Arr.php index 06883b160..88a6bdc3d 100644 --- a/src/Kernel/Support/Arr.php +++ b/src/Kernel/Support/Arr.php @@ -4,9 +4,10 @@ namespace EasyWeChat\Kernel\Support; -use function is_string; use JetBrains\PhpStorm\Pure; +use function is_string; + class Arr { /** diff --git a/src/Kernel/Support/PrivateKey.php b/src/Kernel/Support/PrivateKey.php index 034ec2130..49ccba30a 100644 --- a/src/Kernel/Support/PrivateKey.php +++ b/src/Kernel/Support/PrivateKey.php @@ -2,9 +2,10 @@ namespace EasyWeChat\Kernel\Support; +use JetBrains\PhpStorm\Pure; + use function file_exists; use function file_get_contents; -use JetBrains\PhpStorm\Pure; use function str_starts_with; class PrivateKey diff --git a/src/Kernel/Support/PublicKey.php b/src/Kernel/Support/PublicKey.php index 473fb9e62..64e3ab244 100644 --- a/src/Kernel/Support/PublicKey.php +++ b/src/Kernel/Support/PublicKey.php @@ -3,6 +3,7 @@ namespace EasyWeChat\Kernel\Support; use EasyWeChat\Kernel\Exceptions\InvalidConfigException; + use function file_exists; use function file_get_contents; use function openssl_x509_parse; diff --git a/src/Kernel/Support/Str.php b/src/Kernel/Support/Str.php index b0c18246a..5ba28fd91 100644 --- a/src/Kernel/Support/Str.php +++ b/src/Kernel/Support/Str.php @@ -2,8 +2,9 @@ namespace EasyWeChat\Kernel\Support; -use function base64_encode; use Exception; + +use function base64_encode; use function preg_replace; use function random_bytes; use function str_replace; diff --git a/src/Kernel/Support/UserAgent.php b/src/Kernel/Support/UserAgent.php index 2bd7b7891..560a72a40 100644 --- a/src/Kernel/Support/UserAgent.php +++ b/src/Kernel/Support/UserAgent.php @@ -4,10 +4,11 @@ namespace EasyWeChat\Kernel\Support; +use Composer\InstalledVersions; + use function array_map; use function array_unshift; use function class_exists; -use Composer\InstalledVersions; use function curl_version; use function defined; use function explode; diff --git a/src/Kernel/Support/Xml.php b/src/Kernel/Support/Xml.php index 8ea42e282..e7689117f 100644 --- a/src/Kernel/Support/Xml.php +++ b/src/Kernel/Support/Xml.php @@ -8,7 +8,7 @@ class Xml { - public static function parse(string $xml): array|null + public static function parse(string $xml): ?array { return Transformer::toArray($xml); } diff --git a/src/Kernel/Traits/InteractWithConfig.php b/src/Kernel/Traits/InteractWithConfig.php index 000677a75..b73b1ab8a 100644 --- a/src/Kernel/Traits/InteractWithConfig.php +++ b/src/Kernel/Traits/InteractWithConfig.php @@ -7,6 +7,7 @@ use EasyWeChat\Kernel\Config; use EasyWeChat\Kernel\Contracts\Config as ConfigInterface; use EasyWeChat\Kernel\Exceptions\InvalidArgumentException; + use function is_array; trait InteractWithConfig diff --git a/src/Kernel/Traits/InteractWithHandlers.php b/src/Kernel/Traits/InteractWithHandlers.php index 73d8f7f87..6889814ef 100644 --- a/src/Kernel/Traits/InteractWithHandlers.php +++ b/src/Kernel/Traits/InteractWithHandlers.php @@ -4,17 +4,18 @@ namespace EasyWeChat\Kernel\Traits; +use Closure; +use EasyWeChat\Kernel\Exceptions\InvalidArgumentException; +use JetBrains\PhpStorm\ArrayShape; + use function array_reverse; use function array_unshift; use function call_user_func; -use Closure; -use EasyWeChat\Kernel\Exceptions\InvalidArgumentException; use function func_get_args; use function gettype; use function is_array; use function is_callable; use function is_string; -use JetBrains\PhpStorm\ArrayShape; use function method_exists; use function spl_object_hash; diff --git a/src/Kernel/Traits/InteractWithHttpClient.php b/src/Kernel/Traits/InteractWithHttpClient.php index 146be0f70..aa914a9ce 100644 --- a/src/Kernel/Traits/InteractWithHttpClient.php +++ b/src/Kernel/Traits/InteractWithHttpClient.php @@ -5,11 +5,12 @@ namespace EasyWeChat\Kernel\Traits; use EasyWeChat\Kernel\HttpClient\RequestUtil; -use function property_exists; use Psr\Log\LoggerAwareInterface; use Symfony\Component\HttpClient\HttpClient; use Symfony\Contracts\HttpClient\HttpClientInterface; +use function property_exists; + trait InteractWithHttpClient { protected ?HttpClientInterface $httpClient = null; diff --git a/src/Kernel/Traits/RespondXmlMessage.php b/src/Kernel/Traits/RespondXmlMessage.php index eb7f1586c..95307c866 100644 --- a/src/Kernel/Traits/RespondXmlMessage.php +++ b/src/Kernel/Traits/RespondXmlMessage.php @@ -2,17 +2,18 @@ namespace EasyWeChat\Kernel\Traits; -use function array_merge; use EasyWeChat\Kernel\Encryptor; use EasyWeChat\Kernel\Exceptions\InvalidArgumentException; use EasyWeChat\Kernel\Exceptions\RuntimeException; use EasyWeChat\Kernel\Message; use EasyWeChat\Kernel\Support\Xml; +use Nyholm\Psr7\Response; +use Psr\Http\Message\ResponseInterface; + +use function array_merge; use function is_array; use function is_callable; use function is_string; -use Nyholm\Psr7\Response; -use Psr\Http\Message\ResponseInterface; use function time; trait RespondXmlMessage @@ -21,7 +22,7 @@ trait RespondXmlMessage * @throws RuntimeException * @throws InvalidArgumentException */ - public function transformToReply(mixed $response, Message $message, ?Encryptor $encryptor = null): ResponseInterface + public function transformToReply(mixed $response, Message $message, Encryptor $encryptor = null): ResponseInterface { if (empty($response)) { return new Response(200, [], 'success'); @@ -78,7 +79,7 @@ protected function normalizeResponse(mixed $response): array * * @throws RuntimeException */ - protected function createXmlResponse(array $attributes, ?Encryptor $encryptor = null): ResponseInterface + protected function createXmlResponse(array $attributes, Encryptor $encryptor = null): ResponseInterface { $xml = Xml::build($attributes); diff --git a/src/MiniApp/Application.php b/src/MiniApp/Application.php index b49326420..608bf23ec 100644 --- a/src/MiniApp/Application.php +++ b/src/MiniApp/Application.php @@ -4,7 +4,6 @@ namespace EasyWeChat\MiniApp; -use function array_merge; use EasyWeChat\Kernel\Contracts\AccessToken as AccessTokenInterface; use EasyWeChat\Kernel\Contracts\Server as ServerInterface; use EasyWeChat\Kernel\Encryptor; @@ -20,13 +19,15 @@ use EasyWeChat\Kernel\Traits\InteractWithServerRequest; use EasyWeChat\MiniApp\Contracts\Account as AccountInterface; use EasyWeChat\MiniApp\Contracts\Application as ApplicationInterface; -use function is_null; use JetBrains\PhpStorm\Pure; use Psr\Log\LoggerAwareTrait; -use function str_contains; use Symfony\Component\HttpClient\Response\AsyncContext; use Symfony\Component\HttpClient\RetryableHttpClient; +use function array_merge; +use function is_null; +use function str_contains; + /** * @psalm-suppress PropertyNotSetInConstructor */ diff --git a/src/MiniApp/Decryptor.php b/src/MiniApp/Decryptor.php index 66d72dc2e..8dc38e0b3 100644 --- a/src/MiniApp/Decryptor.php +++ b/src/MiniApp/Decryptor.php @@ -4,13 +4,14 @@ namespace EasyWeChat\MiniApp; -use function base64_decode; use EasyWeChat\Kernel\Exceptions\DecryptException; use EasyWeChat\Kernel\Support\AesCbc; +use Throwable; + +use function base64_decode; use function is_array; use function json_decode; use function sprintf; -use Throwable; class Decryptor { diff --git a/src/OfficialAccount/AccessToken.php b/src/OfficialAccount/AccessToken.php index 08a594bfc..84b45669b 100644 --- a/src/OfficialAccount/AccessToken.php +++ b/src/OfficialAccount/AccessToken.php @@ -6,13 +6,9 @@ use EasyWeChat\Kernel\Contracts\RefreshableAccessToken as RefreshableAccessTokenInterface; use EasyWeChat\Kernel\Exceptions\HttpException; -use function intval; -use function is_string; use JetBrains\PhpStorm\ArrayShape; -use function json_encode; use Psr\SimpleCache\CacheInterface; use Psr\SimpleCache\InvalidArgumentException; -use function sprintf; use Symfony\Component\Cache\Adapter\FilesystemAdapter; use Symfony\Component\Cache\Psr16Cache; use Symfony\Component\HttpClient\HttpClient; @@ -23,6 +19,11 @@ use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; use Symfony\Contracts\HttpClient\HttpClientInterface; +use function intval; +use function is_string; +use function json_encode; +use function sprintf; + class AccessToken implements RefreshableAccessTokenInterface { protected HttpClientInterface $httpClient; @@ -33,8 +34,8 @@ public function __construct( protected string $appId, protected string $secret, protected ?string $key = null, - ?CacheInterface $cache = null, - ?HttpClientInterface $httpClient = null, + CacheInterface $cache = null, + HttpClientInterface $httpClient = null, protected ?bool $stable = false ) { $this->httpClient = $httpClient ?? HttpClient::create(['base_uri' => 'https://api.weixin.qq.com/']); diff --git a/src/OfficialAccount/Application.php b/src/OfficialAccount/Application.php index fc83941bf..dc6602c51 100644 --- a/src/OfficialAccount/Application.php +++ b/src/OfficialAccount/Application.php @@ -4,8 +4,6 @@ namespace EasyWeChat\OfficialAccount; -use function array_merge; -use function call_user_func; use EasyWeChat\Kernel\Contracts\AccessToken as AccessTokenInterface; use EasyWeChat\Kernel\Contracts\JsApiTicket as JsApiTicketInterface; use EasyWeChat\Kernel\Contracts\RefreshableAccessToken as RefreshableAccessTokenInterface; @@ -28,11 +26,14 @@ use Overtrue\Socialite\Contracts\ProviderInterface as SocialiteProviderInterface; use Overtrue\Socialite\Providers\WeChat; use Psr\Log\LoggerAwareTrait; -use function sprintf; -use function str_contains; use Symfony\Component\HttpClient\Response\AsyncContext; use Symfony\Component\HttpClient\RetryableHttpClient; +use function array_merge; +use function call_user_func; +use function sprintf; +use function str_contains; + class Application implements ApplicationInterface { use InteractWithConfig; diff --git a/src/OfficialAccount/JsApiTicket.php b/src/OfficialAccount/JsApiTicket.php index 3a5de5235..a6b9197f1 100644 --- a/src/OfficialAccount/JsApiTicket.php +++ b/src/OfficialAccount/JsApiTicket.php @@ -7,6 +7,7 @@ use EasyWeChat\Kernel\Contracts\JsApiTicket as JsApiTicketInterface; use EasyWeChat\Kernel\Exceptions\HttpException; use JetBrains\PhpStorm\ArrayShape; + use function sprintf; class JsApiTicket extends AccessToken implements JsApiTicketInterface diff --git a/src/OfficialAccount/Server.php b/src/OfficialAccount/Server.php index 1344c43cf..66102dd17 100644 --- a/src/OfficialAccount/Server.php +++ b/src/OfficialAccount/Server.php @@ -32,7 +32,7 @@ class Server implements ServerInterface * @throws Throwable */ public function __construct( - ?ServerRequestInterface $request = null, + ServerRequestInterface $request = null, protected ?Encryptor $encryptor = null, ) { $this->request = $request ?? RequestUtil::createDefaultServerRequest(); @@ -122,7 +122,7 @@ protected function decryptRequestMessage(array $query): Closure /** * @throws BadRequestException */ - public function getRequestMessage(?ServerRequestInterface $request = null): \EasyWeChat\Kernel\Message + public function getRequestMessage(ServerRequestInterface $request = null): \EasyWeChat\Kernel\Message { return Message::createFromRequest($request ?? $this->request); } @@ -131,7 +131,7 @@ public function getRequestMessage(?ServerRequestInterface $request = null): \Eas * @throws BadRequestException * @throws RuntimeException */ - public function getDecryptedMessage(?ServerRequestInterface $request = null): \EasyWeChat\Kernel\Message + public function getDecryptedMessage(ServerRequestInterface $request = null): \EasyWeChat\Kernel\Message { $request = $request ?? $this->request; $message = $this->getRequestMessage($request); diff --git a/src/OfficialAccount/Utils.php b/src/OfficialAccount/Utils.php index 2ca38c42a..7b31134fd 100644 --- a/src/OfficialAccount/Utils.php +++ b/src/OfficialAccount/Utils.php @@ -10,6 +10,7 @@ use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; + use function time; class Utils diff --git a/src/OpenPlatform/Application.php b/src/OpenPlatform/Application.php index abb4f7d56..79253ada8 100644 --- a/src/OpenPlatform/Application.php +++ b/src/OpenPlatform/Application.php @@ -4,7 +4,6 @@ namespace EasyWeChat\OpenPlatform; -use function array_merge; use Closure; use EasyWeChat\Kernel\Contracts\AccessToken as AccessTokenInterface; use EasyWeChat\Kernel\Contracts\Server as ServerInterface; @@ -25,18 +24,20 @@ use EasyWeChat\OpenPlatform\Contracts\Account as AccountInterface; use EasyWeChat\OpenPlatform\Contracts\Application as ApplicationInterface; use EasyWeChat\OpenPlatform\Contracts\VerifyTicket as VerifyTicketInterface; -use function is_string; -use function md5; use Overtrue\Socialite\Contracts\ProviderInterface as SocialiteProviderInterface; use Overtrue\Socialite\Providers\WeChat; use Psr\SimpleCache\InvalidArgumentException; -use function sprintf; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface; use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; +use function array_merge; +use function is_string; +use function md5; +use function sprintf; + class Application implements ApplicationInterface { use InteractWithCache; diff --git a/src/OpenPlatform/ComponentAccessToken.php b/src/OpenPlatform/ComponentAccessToken.php index 33629cdfd..eaa0cefbb 100644 --- a/src/OpenPlatform/ComponentAccessToken.php +++ b/src/OpenPlatform/ComponentAccessToken.php @@ -4,19 +4,20 @@ namespace EasyWeChat\OpenPlatform; -use function abs; use EasyWeChat\Kernel\Contracts\RefreshableAccessToken as RefreshableAccessTokenInterface; use EasyWeChat\Kernel\Exceptions\HttpException; use EasyWeChat\OpenPlatform\Contracts\VerifyTicket as VerifyTicketInterface; -use function intval; use JetBrains\PhpStorm\ArrayShape; -use function json_encode; use Psr\SimpleCache\CacheInterface; use Symfony\Component\Cache\Adapter\FilesystemAdapter; use Symfony\Component\Cache\Psr16Cache; use Symfony\Component\HttpClient\HttpClient; use Symfony\Contracts\HttpClient\HttpClientInterface; +use function abs; +use function intval; +use function json_encode; + class ComponentAccessToken implements RefreshableAccessTokenInterface { protected HttpClientInterface $httpClient; @@ -28,8 +29,8 @@ public function __construct( protected string $secret, protected VerifyTicketInterface $verifyTicket, protected ?string $key = null, - ?CacheInterface $cache = null, - ?HttpClientInterface $httpClient = null, + CacheInterface $cache = null, + HttpClientInterface $httpClient = null, ) { $this->httpClient = $httpClient ?? HttpClient::create(['base_uri' => 'https://api.weixin.qq.com/']); $this->cache = $cache ?? new Psr16Cache(new FilesystemAdapter(namespace: 'easywechat', defaultLifetime: 1500)); diff --git a/src/OpenPlatform/Server.php b/src/OpenPlatform/Server.php index 743877744..b7c7a2eae 100644 --- a/src/OpenPlatform/Server.php +++ b/src/OpenPlatform/Server.php @@ -15,11 +15,12 @@ use EasyWeChat\Kernel\Traits\DecryptXmlMessage; use EasyWeChat\Kernel\Traits\InteractWithHandlers; use EasyWeChat\Kernel\Traits\RespondXmlMessage; -use function func_get_args; use Nyholm\Psr7\Response; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; +use function func_get_args; + class Server implements ServerInterface { use InteractWithHandlers; @@ -35,7 +36,7 @@ class Server implements ServerInterface */ public function __construct( protected Encryptor $encryptor, - ?ServerRequestInterface $request = null, + ServerRequestInterface $request = null, ) { $this->request = $request ?? RequestUtil::createDefaultServerRequest(); } @@ -145,7 +146,7 @@ protected function decryptRequestMessage(): Closure /** * @throws BadRequestException */ - public function getRequestMessage(?ServerRequestInterface $request = null): \EasyWeChat\Kernel\Message + public function getRequestMessage(ServerRequestInterface $request = null): \EasyWeChat\Kernel\Message { return Message::createFromRequest($request ?? $this->request); } @@ -154,7 +155,7 @@ public function getRequestMessage(?ServerRequestInterface $request = null): \Eas * @throws BadRequestException * @throws RuntimeException */ - public function getDecryptedMessage(?ServerRequestInterface $request = null): \EasyWeChat\Kernel\Message + public function getDecryptedMessage(ServerRequestInterface $request = null): \EasyWeChat\Kernel\Message { $request = $request ?? $this->request; $message = $this->getRequestMessage($request); diff --git a/src/OpenPlatform/VerifyTicket.php b/src/OpenPlatform/VerifyTicket.php index b8bab9921..5692d7416 100644 --- a/src/OpenPlatform/VerifyTicket.php +++ b/src/OpenPlatform/VerifyTicket.php @@ -6,13 +6,14 @@ use EasyWeChat\Kernel\Exceptions\RuntimeException; use EasyWeChat\OpenPlatform\Contracts\VerifyTicket as VerifyTicketInterface; -use function is_string; use Psr\SimpleCache\CacheInterface; use Psr\SimpleCache\InvalidArgumentException; -use function sprintf; use Symfony\Component\Cache\Adapter\FilesystemAdapter; use Symfony\Component\Cache\Psr16Cache; +use function is_string; +use function sprintf; + class VerifyTicket implements VerifyTicketInterface { protected CacheInterface $cache; @@ -20,7 +21,7 @@ class VerifyTicket implements VerifyTicketInterface public function __construct( protected string $appId, protected ?string $key = null, - ?CacheInterface $cache = null, + CacheInterface $cache = null, ) { $this->cache = $cache ?? new Psr16Cache(new FilesystemAdapter(namespace: 'easywechat', defaultLifetime: 1500)); } diff --git a/src/OpenWork/Application.php b/src/OpenWork/Application.php index e00c1080c..79288478c 100644 --- a/src/OpenWork/Application.php +++ b/src/OpenWork/Application.php @@ -4,7 +4,6 @@ namespace EasyWeChat\OpenWork; -use function array_merge; use EasyWeChat\Kernel\Contracts\AccessToken as AccessTokenInterface; use EasyWeChat\Kernel\Contracts\Server as ServerInterface; use EasyWeChat\Kernel\Exceptions\HttpException; @@ -26,6 +25,8 @@ use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; +use function array_merge; + class Application implements ApplicationInterface { use InteractWithCache; @@ -219,7 +220,7 @@ public function setSuiteTicket(SuiteTicketInterface $suiteTicket): SuiteTicketIn public function getAuthorization( string $corpId, string $permanentCode, - ?AccessTokenInterface $suiteAccessToken = null + AccessTokenInterface $suiteAccessToken = null ): Authorization { $suiteAccessToken = $suiteAccessToken ?? $this->getSuiteAccessToken(); @@ -251,7 +252,7 @@ public function getAuthorization( public function getAuthorizerAccessToken( string $corpId, string $permanentCode, - ?AccessTokenInterface $suiteAccessToken = null + AccessTokenInterface $suiteAccessToken = null ): AuthorizerAccessToken { $suiteAccessToken = $suiteAccessToken ?? $this->getSuiteAccessToken(); @@ -282,7 +283,7 @@ public function createClient(): AccessTokenAwareClient * @throws DecodingExceptionInterface * @throws ClientExceptionInterface */ - public function getAuthorizerClient(string $corpId, string $permanentCode, ?AccessTokenInterface $suiteAccessToken = null): AccessTokenAwareClient + public function getAuthorizerClient(string $corpId, string $permanentCode, AccessTokenInterface $suiteAccessToken = null): AccessTokenAwareClient { return (new AccessTokenAwareClient( client: $this->getHttpClient(), @@ -300,7 +301,7 @@ public function getAuthorizerClient(string $corpId, string $permanentCode, ?Acce * @throws DecodingExceptionInterface * @throws ClientExceptionInterface */ - public function getJsApiTicket(string $corpId, string $permanentCode, ?AccessTokenInterface $suiteAccessToken = null): JsApiTicket + public function getJsApiTicket(string $corpId, string $permanentCode, AccessTokenInterface $suiteAccessToken = null): JsApiTicket { return new JsApiTicket( corpId: $corpId, @@ -311,7 +312,7 @@ public function getJsApiTicket(string $corpId, string $permanentCode, ?AccessTok public function getOAuth( string $suiteId, - ?AccessTokenInterface $suiteAccessToken = null + AccessTokenInterface $suiteAccessToken = null ): SocialiteProviderInterface { $suiteAccessToken = $suiteAccessToken ?? $this->getSuiteAccessToken(); @@ -326,7 +327,7 @@ public function getOAuth( public function getCorpOAuth( string $corpId, - ?AccessTokenInterface $suiteAccessToken = null + AccessTokenInterface $suiteAccessToken = null ): SocialiteProviderInterface { $suiteAccessToken = $suiteAccessToken ?? $this->getSuiteAccessToken(); diff --git a/src/OpenWork/AuthorizerAccessToken.php b/src/OpenWork/AuthorizerAccessToken.php index 7818600a8..229c9e56c 100644 --- a/src/OpenWork/AuthorizerAccessToken.php +++ b/src/OpenWork/AuthorizerAccessToken.php @@ -32,8 +32,8 @@ public function __construct( protected string $permanentCodeOrAccessToken, protected ?AccessTokenInterface $suiteAccessToken = null, protected ?string $key = null, - ?CacheInterface $cache = null, - ?HttpClientInterface $httpClient = null, + CacheInterface $cache = null, + HttpClientInterface $httpClient = null, ) { $this->httpClient = $httpClient ?? HttpClient::create(['base_uri' => 'https://qyapi.weixin.qq.com/']); $this->cache = $cache ?? new Psr16Cache(new FilesystemAdapter(namespace: 'easywechat', defaultLifetime: 1500)); diff --git a/src/OpenWork/JsApiTicket.php b/src/OpenWork/JsApiTicket.php index 12f82a3bc..50ae2d381 100644 --- a/src/OpenWork/JsApiTicket.php +++ b/src/OpenWork/JsApiTicket.php @@ -5,11 +5,8 @@ namespace EasyWeChat\OpenWork; use EasyWeChat\Kernel\Exceptions\HttpException; -use function intval; -use function is_string; use Psr\SimpleCache\CacheInterface; use Psr\SimpleCache\InvalidArgumentException; -use function sprintf; use Symfony\Component\Cache\Adapter\FilesystemAdapter; use Symfony\Component\Cache\Psr16Cache; use Symfony\Component\HttpClient\HttpClient; @@ -20,6 +17,10 @@ use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; use Symfony\Contracts\HttpClient\HttpClientInterface; +use function intval; +use function is_string; +use function sprintf; + class JsApiTicket { protected HttpClientInterface $httpClient; @@ -29,8 +30,8 @@ class JsApiTicket public function __construct( protected string $corpId, protected ?string $key = null, - ?CacheInterface $cache = null, - ?HttpClientInterface $httpClient = null + CacheInterface $cache = null, + HttpClientInterface $httpClient = null ) { $this->httpClient = $httpClient ?? HttpClient::create(['base_uri' => 'https://qyapi.weixin.qq.com/']); $this->cache = $cache ?? new Psr16Cache(new FilesystemAdapter(namespace: 'easywechat', defaultLifetime: 1500)); diff --git a/src/OpenWork/ProviderAccessToken.php b/src/OpenWork/ProviderAccessToken.php index 3f69d9e19..06de80d0b 100644 --- a/src/OpenWork/ProviderAccessToken.php +++ b/src/OpenWork/ProviderAccessToken.php @@ -4,17 +4,19 @@ namespace EasyWeChat\OpenWork; +use const JSON_UNESCAPED_UNICODE; + use EasyWeChat\Kernel\Contracts\RefreshableAccessToken as RefreshableAccessTokenInterface; use EasyWeChat\Kernel\Exceptions\HttpException; -use function intval; use JetBrains\PhpStorm\ArrayShape; -use const JSON_UNESCAPED_UNICODE; use Psr\SimpleCache\CacheInterface; use Symfony\Component\Cache\Adapter\FilesystemAdapter; use Symfony\Component\Cache\Psr16Cache; use Symfony\Component\HttpClient\HttpClient; use Symfony\Contracts\HttpClient\HttpClientInterface; +use function intval; + class ProviderAccessToken implements RefreshableAccessTokenInterface { protected HttpClientInterface $httpClient; @@ -25,8 +27,8 @@ public function __construct( protected string $corpId, protected string $providerSecret, protected ?string $key = null, - ?CacheInterface $cache = null, - ?HttpClientInterface $httpClient = null, + CacheInterface $cache = null, + HttpClientInterface $httpClient = null, ) { $this->httpClient = $httpClient ?? HttpClient::create(['base_uri' => 'https://qyapi.weixin.qq.com/']); $this->cache = $cache ?? new Psr16Cache(new FilesystemAdapter(namespace: 'easywechat', defaultLifetime: 1500)); diff --git a/src/OpenWork/Server.php b/src/OpenWork/Server.php index 7f46dae19..57ff28994 100644 --- a/src/OpenWork/Server.php +++ b/src/OpenWork/Server.php @@ -15,11 +15,12 @@ use EasyWeChat\Kernel\Traits\DecryptXmlMessage; use EasyWeChat\Kernel\Traits\InteractWithHandlers; use EasyWeChat\Kernel\Traits\RespondXmlMessage; -use function func_get_args; use Nyholm\Psr7\Response; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; +use function func_get_args; + class Server implements ServerInterface { use InteractWithHandlers; @@ -28,7 +29,7 @@ class Server implements ServerInterface protected ServerRequestInterface $request; - protected Closure|null $defaultSuiteTicketHandler = null; + protected ?Closure $defaultSuiteTicketHandler = null; /** * @throws \Throwable @@ -36,7 +37,7 @@ class Server implements ServerInterface public function __construct( protected Encryptor $encryptor, protected Encryptor $providerEncryptor, - ?ServerRequestInterface $request = null, + ServerRequestInterface $request = null, ) { $this->request = $request ?? RequestUtil::createDefaultServerRequest(); } @@ -260,7 +261,7 @@ protected function decryptRequestMessage(): Closure /** * @throws BadRequestException */ - public function getRequestMessage(?ServerRequestInterface $request = null): \EasyWeChat\Kernel\Message + public function getRequestMessage(ServerRequestInterface $request = null): \EasyWeChat\Kernel\Message { return Message::createFromRequest($request ?? $this->request); } @@ -269,7 +270,7 @@ public function getRequestMessage(?ServerRequestInterface $request = null): \Eas * @throws BadRequestException * @throws RuntimeException */ - public function getDecryptedMessage(?ServerRequestInterface $request = null): \EasyWeChat\Kernel\Message + public function getDecryptedMessage(ServerRequestInterface $request = null): \EasyWeChat\Kernel\Message { $request = $request ?? $this->request; $message = $this->getRequestMessage($request); diff --git a/src/OpenWork/SuiteAccessToken.php b/src/OpenWork/SuiteAccessToken.php index 11db7a17f..1735b94b2 100644 --- a/src/OpenWork/SuiteAccessToken.php +++ b/src/OpenWork/SuiteAccessToken.php @@ -4,20 +4,22 @@ namespace EasyWeChat\OpenWork; -use function abs; +use const JSON_UNESCAPED_UNICODE; + use EasyWeChat\Kernel\Contracts\RefreshableAccessToken as RefreshableAccessTokenInterface; use EasyWeChat\Kernel\Exceptions\HttpException; use EasyWeChat\OpenWork\Contracts\SuiteTicket as SuiteTicketInterface; -use function intval; use JetBrains\PhpStorm\ArrayShape; -use function json_encode; -use const JSON_UNESCAPED_UNICODE; use Psr\SimpleCache\CacheInterface; use Symfony\Component\Cache\Adapter\FilesystemAdapter; use Symfony\Component\Cache\Psr16Cache; use Symfony\Component\HttpClient\HttpClient; use Symfony\Contracts\HttpClient\HttpClientInterface; +use function abs; +use function intval; +use function json_encode; + class SuiteAccessToken implements RefreshableAccessTokenInterface { protected HttpClientInterface $httpClient; @@ -29,8 +31,8 @@ public function __construct( protected string $suiteSecret, protected ?SuiteTicketInterface $suiteTicket = null, protected ?string $key = null, - ?CacheInterface $cache = null, - ?HttpClientInterface $httpClient = null, + CacheInterface $cache = null, + HttpClientInterface $httpClient = null, ) { $this->httpClient = $httpClient ?? HttpClient::create(['base_uri' => 'https://qyapi.weixin.qq.com/']); $this->cache = $cache ?? new Psr16Cache(new FilesystemAdapter(namespace: 'easywechat', defaultLifetime: 1500)); diff --git a/src/OpenWork/SuiteTicket.php b/src/OpenWork/SuiteTicket.php index 0c10c7f76..7cdaff346 100644 --- a/src/OpenWork/SuiteTicket.php +++ b/src/OpenWork/SuiteTicket.php @@ -6,20 +6,21 @@ use EasyWeChat\Kernel\Exceptions\RuntimeException; use EasyWeChat\OpenWork\Contracts\SuiteTicket as SuiteTicketInterface; -use function is_string; use Psr\SimpleCache\CacheInterface; use Psr\SimpleCache\InvalidArgumentException; -use function sprintf; use Symfony\Component\Cache\Adapter\FilesystemAdapter; use Symfony\Component\Cache\Psr16Cache; +use function is_string; +use function sprintf; + class SuiteTicket implements SuiteTicketInterface { protected CacheInterface $cache; public function __construct( protected string $suiteId, - ?CacheInterface $cache = null, + CacheInterface $cache = null, protected ?string $key = null, ) { $this->cache = $cache ?? new Psr16Cache(new FilesystemAdapter(namespace: 'easywechat', defaultLifetime: 1500)); diff --git a/src/Pay/Client.php b/src/Pay/Client.php index 60eb15963..faa369238 100644 --- a/src/Pay/Client.php +++ b/src/Pay/Client.php @@ -18,12 +18,9 @@ use EasyWeChat\Kernel\Support\Xml; use EasyWeChat\Kernel\Traits\MockableHttpClient; use Exception; -use function is_array; -use function is_string; use Mockery; use Mockery\Mock; use Nyholm\Psr7\Uri; -use function str_starts_with; use Symfony\Component\HttpClient\DecoratorTrait; use Symfony\Component\HttpClient\HttpClient as SymfonyHttpClient; use Symfony\Component\HttpClient\HttpClientTrait; @@ -33,6 +30,10 @@ use Symfony\Contracts\HttpClient\HttpClientInterface; use Symfony\Contracts\HttpClient\ResponseInterface; +use function is_array; +use function is_string; +use function str_starts_with; + /** * @method ResponseInterface get(string $uri, array $options = []) * @method ResponseInterface post(string $uri, array $options = []) @@ -77,7 +78,7 @@ class Client implements HttpClientInterface */ public function __construct( protected Merchant $merchant, - ?HttpClientInterface $client = null, + HttpClientInterface $client = null, array $defaultOptions = [] ) { $this->throw = (bool) ($defaultOptions['throw'] ?? true); diff --git a/src/Pay/LegacySignature.php b/src/Pay/LegacySignature.php index bcc0020aa..711ee060c 100644 --- a/src/Pay/LegacySignature.php +++ b/src/Pay/LegacySignature.php @@ -4,11 +4,12 @@ namespace EasyWeChat\Pay; -use function call_user_func_array; use EasyWeChat\Kernel\Exceptions\InvalidConfigException; use EasyWeChat\Kernel\Exceptions\RuntimeException; use EasyWeChat\Kernel\Support\Str; use EasyWeChat\Pay\Contracts\Merchant as MerchantInterface; + +use function call_user_func_array; use function hash_hmac; use function http_build_query; use function is_string; diff --git a/src/Pay/Merchant.php b/src/Pay/Merchant.php index 7f9536232..ef58ee2d8 100644 --- a/src/Pay/Merchant.php +++ b/src/Pay/Merchant.php @@ -4,12 +4,13 @@ namespace EasyWeChat\Pay; -use function array_is_list; use EasyWeChat\Kernel\Exceptions\InvalidArgumentException; use EasyWeChat\Kernel\Exceptions\InvalidConfigException; use EasyWeChat\Kernel\Support\PrivateKey; use EasyWeChat\Kernel\Support\PublicKey; use EasyWeChat\Pay\Contracts\Merchant as MerchantInterface; + +use function array_is_list; use function intval; use function is_string; diff --git a/src/Pay/Message.php b/src/Pay/Message.php index 75089c069..055335e88 100644 --- a/src/Pay/Message.php +++ b/src/Pay/Message.php @@ -2,10 +2,11 @@ namespace EasyWeChat\Pay; +use RuntimeException; + use function is_array; use function is_string; use function json_decode; -use RuntimeException; /** * @property string $trade_state diff --git a/src/Pay/Server.php b/src/Pay/Server.php index cc434afbf..f6e2354e1 100644 --- a/src/Pay/Server.php +++ b/src/Pay/Server.php @@ -12,15 +12,16 @@ use EasyWeChat\Kernel\Traits\InteractWithHandlers; use EasyWeChat\Pay\Contracts\Merchant as MerchantInterface; use Exception; -use function is_array; -use function json_decode; -use function json_encode; use Nyholm\Psr7\Response; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; -use function strval; use Throwable; +use function is_array; +use function json_decode; +use function json_encode; +use function strval; + /** * @link https://pay.weixin.qq.com/wiki/doc/apiv3/wechatpay/wechatpay4_1.shtml * @link https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_5.shtml @@ -108,7 +109,7 @@ public function handleRefunded(callable $handler): static * @throws InvalidArgumentException * @throws RuntimeException */ - public function getRequestMessage(?ServerRequestInterface $request = null): \EasyWeChat\Kernel\Message|Message + public function getRequestMessage(ServerRequestInterface $request = null): \EasyWeChat\Kernel\Message|Message { $originContent = (string) ($request ?? $this->request)->getBody(); $attributes = json_decode($originContent, true); @@ -142,7 +143,7 @@ public function getRequestMessage(?ServerRequestInterface $request = null): \Eas * @throws InvalidArgumentException * @throws RuntimeException */ - public function getDecryptedMessage(?ServerRequestInterface $request = null): \EasyWeChat\Kernel\Message|Message + public function getDecryptedMessage(ServerRequestInterface $request = null): \EasyWeChat\Kernel\Message|Message { return $this->getRequestMessage($request); } diff --git a/src/Pay/Signature.php b/src/Pay/Signature.php index c55043a9d..c44cec5b5 100644 --- a/src/Pay/Signature.php +++ b/src/Pay/Signature.php @@ -4,13 +4,14 @@ namespace EasyWeChat\Pay; -use function base64_encode; use EasyWeChat\Kernel\Support\Str; use EasyWeChat\Pay\Contracts\Merchant as MerchantInterface; use Exception; +use Nyholm\Psr7\Uri; + +use function base64_encode; use function http_build_query; use function ltrim; -use Nyholm\Psr7\Uri; use function openssl_sign; use function parse_str; use function strtoupper; diff --git a/src/Pay/URLSchemeBuilder.php b/src/Pay/URLSchemeBuilder.php index 8dd140eb5..23b85af73 100644 --- a/src/Pay/URLSchemeBuilder.php +++ b/src/Pay/URLSchemeBuilder.php @@ -7,6 +7,7 @@ use EasyWeChat\Kernel\Support\Str; use EasyWeChat\Pay\Contracts\Merchant as MerchantInterface; use Exception; + use function sprintf; class URLSchemeBuilder diff --git a/src/Pay/Utils.php b/src/Pay/Utils.php index 8581a7d75..27659548f 100644 --- a/src/Pay/Utils.php +++ b/src/Pay/Utils.php @@ -2,14 +2,15 @@ namespace EasyWeChat\Pay; -use function base64_encode; -use function call_user_func_array; use EasyWeChat\Kernel\Exceptions\InvalidConfigException; use EasyWeChat\Kernel\Support\Str; use EasyWeChat\Pay\Contracts\Merchant as MerchantInterface; use Exception; -use function http_build_query; use JetBrains\PhpStorm\ArrayShape; + +use function base64_encode; +use function call_user_func_array; +use function http_build_query; use function openssl_sign; use function strtoupper; use function time; diff --git a/src/Work/AccessToken.php b/src/Work/AccessToken.php index d31511fc2..760885e97 100644 --- a/src/Work/AccessToken.php +++ b/src/Work/AccessToken.php @@ -4,16 +4,13 @@ namespace EasyWeChat\Work; +use const JSON_UNESCAPED_UNICODE; + use EasyWeChat\Kernel\Contracts\RefreshableAccessToken; use EasyWeChat\Kernel\Exceptions\HttpException; -use function intval; -use function is_string; use JetBrains\PhpStorm\ArrayShape; -use function json_encode; -use const JSON_UNESCAPED_UNICODE; use Psr\SimpleCache\CacheInterface; use Psr\SimpleCache\InvalidArgumentException; -use function sprintf; use Symfony\Component\Cache\Adapter\FilesystemAdapter; use Symfony\Component\Cache\Psr16Cache; use Symfony\Component\HttpClient\HttpClient; @@ -24,6 +21,11 @@ use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; use Symfony\Contracts\HttpClient\HttpClientInterface; +use function intval; +use function is_string; +use function json_encode; +use function sprintf; + class AccessToken implements RefreshableAccessToken { protected HttpClientInterface $httpClient; @@ -34,8 +36,8 @@ public function __construct( protected string $corpId, protected string $secret, protected ?string $key = null, - ?CacheInterface $cache = null, - ?HttpClientInterface $httpClient = null + CacheInterface $cache = null, + HttpClientInterface $httpClient = null ) { $this->httpClient = $httpClient ?? HttpClient::create(['base_uri' => 'https://qyapi.weixin.qq.com/']); $this->cache = $cache ?? new Psr16Cache(new FilesystemAdapter(namespace: 'easywechat', defaultLifetime: 1500)); diff --git a/src/Work/Application.php b/src/Work/Application.php index 3362a8d4d..a2ad06183 100644 --- a/src/Work/Application.php +++ b/src/Work/Application.php @@ -4,7 +4,6 @@ namespace EasyWeChat\Work; -use function array_merge; use EasyWeChat\Kernel\Contracts\AccessToken as AccessTokenInterface; use EasyWeChat\Kernel\Contracts\Server as ServerInterface; use EasyWeChat\Kernel\HttpClient\AccessTokenAwareClient; @@ -19,6 +18,8 @@ use Overtrue\Socialite\Contracts\ProviderInterface as SocialiteProviderInterface; use Overtrue\Socialite\Providers\WeWork; +use function array_merge; + class Application implements ApplicationInterface { use InteractWithConfig; diff --git a/src/Work/JsApiTicket.php b/src/Work/JsApiTicket.php index ac4d4ce8e..f68bb4cd4 100644 --- a/src/Work/JsApiTicket.php +++ b/src/Work/JsApiTicket.php @@ -5,12 +5,9 @@ namespace EasyWeChat\Work; use EasyWeChat\Kernel\Exceptions\HttpException; -use function intval; -use function is_string; use JetBrains\PhpStorm\ArrayShape; use Psr\SimpleCache\CacheInterface; use Psr\SimpleCache\InvalidArgumentException; -use function sprintf; use Symfony\Component\Cache\Adapter\FilesystemAdapter; use Symfony\Component\Cache\Psr16Cache; use Symfony\Component\HttpClient\HttpClient; @@ -21,6 +18,10 @@ use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; use Symfony\Contracts\HttpClient\HttpClientInterface; +use function intval; +use function is_string; +use function sprintf; + class JsApiTicket { protected HttpClientInterface $httpClient; @@ -30,8 +31,8 @@ class JsApiTicket public function __construct( protected string $corpId, protected ?string $key = null, - ?CacheInterface $cache = null, - ?HttpClientInterface $httpClient = null + CacheInterface $cache = null, + HttpClientInterface $httpClient = null ) { $this->httpClient = $httpClient ?? HttpClient::create(['base_uri' => 'https://qyapi.weixin.qq.com/']); $this->cache = $cache ?? new Psr16Cache(new FilesystemAdapter(namespace: 'easywechat', defaultLifetime: 1500)); diff --git a/src/Work/Server.php b/src/Work/Server.php index f0a59b697..4d8722a56 100644 --- a/src/Work/Server.php +++ b/src/Work/Server.php @@ -33,7 +33,7 @@ class Server implements ServerInterface */ public function __construct( protected Encryptor $encryptor, - ?ServerRequestInterface $request = null, + ServerRequestInterface $request = null, ) { $this->request = $request ?? RequestUtil::createDefaultServerRequest(); } @@ -261,7 +261,7 @@ protected function decryptRequestMessage(): Closure /** * @throws BadRequestException */ - public function getRequestMessage(?ServerRequestInterface $request = null): \EasyWeChat\Kernel\Message + public function getRequestMessage(ServerRequestInterface $request = null): \EasyWeChat\Kernel\Message { return Message::createFromRequest($request ?? $this->request); } @@ -270,7 +270,7 @@ public function getRequestMessage(?ServerRequestInterface $request = null): \Eas * @throws BadRequestException * @throws RuntimeException */ - public function getDecryptedMessage(?ServerRequestInterface $request = null): \EasyWeChat\Kernel\Message + public function getDecryptedMessage(ServerRequestInterface $request = null): \EasyWeChat\Kernel\Message { $request = $request ?? $this->request; $message = $this->getRequestMessage($request); diff --git a/src/Work/Utils.php b/src/Work/Utils.php index a784c6410..8de79e18d 100644 --- a/src/Work/Utils.php +++ b/src/Work/Utils.php @@ -11,6 +11,7 @@ use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; + use function time; class Utils