Skip to content

Commit

Permalink
[fix] 解决response headers如果为空,max函数报错问题 (#2736)
Browse files Browse the repository at this point in the history
* 解决response不存在header的时候,max报错的问题

* 按照预定义代码风格格式化代码
  • Loading branch information
reatang committed Aug 18, 2023
1 parent 3bbc0dd commit 24e8c93
Show file tree
Hide file tree
Showing 53 changed files with 254 additions and 183 deletions.
1 change: 1 addition & 0 deletions src/Kernel/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
use EasyWeChat\Kernel\Support\Arr;
use JetBrains\PhpStorm\Pure;

use function strval;

/**
Expand Down
16 changes: 9 additions & 7 deletions src/Kernel/Encryptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down
26 changes: 14 additions & 12 deletions src/Kernel/Form/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand All @@ -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();
Expand Down Expand Up @@ -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());

Check failure on line 77 in src/Kernel/Form/File.php

View workflow job for this annotation

GitHub Actions / PHPStan

Parameter #1 $contents of static method EasyWeChat\Kernel\Form\File::fromContents() expects string, mixed given.

Check failure on line 77 in src/Kernel/Form/File.php

View workflow job for this annotation

GitHub Actions / PHPStan

Parameter #1 $contents of static method EasyWeChat\Kernel\Form\File::fromContents() expects string, mixed given.
}
Expand Down
5 changes: 3 additions & 2 deletions src/Kernel/HttpClient/AccessTokenAwareClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -15,6 +14,8 @@
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;

use function array_merge;

/**
* Class AccessTokenAwareClient.
*
Expand All @@ -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
Expand Down
16 changes: 9 additions & 7 deletions src/Kernel/HttpClient/RequestUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/**
Expand Down
3 changes: 2 additions & 1 deletion src/Kernel/HttpClient/RequestWithPresets.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
36 changes: 19 additions & 17 deletions src/Kernel/HttpClient/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<array-key, mixed>
*
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -141,15 +143,15 @@ 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);
}

/**
* {@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);
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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]);
}
Expand All @@ -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;
Expand All @@ -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;
Expand Down
30 changes: 17 additions & 13 deletions src/Kernel/ServerResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -187,16 +189,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);
}
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/Kernel/Support/AesCbc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
6 changes: 4 additions & 2 deletions src/Kernel/Support/AesEcb.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit 24e8c93

Please sign in to comment.