From cdf868deec237d7724908b045b5af696763e0eda Mon Sep 17 00:00:00 2001 From: Anyon Date: Fri, 8 May 2026 11:33:26 +0800 Subject: [PATCH] =?UTF-8?q?style(header):=20=E7=BB=9F=E4=B8=80=E6=A0=B8?= =?UTF-8?q?=E5=BF=83=E5=A5=91=E7=BA=A6=E7=BC=93=E5=AD=98=E5=92=8C=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E6=96=87=E4=BB=B6=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 按 PHP-CS-Fixer 规则补齐核心契约、异常、缓存和 XML 工具文件头。 - 规范导入、PHPDoc 类型顺序与注释格式,保持项目头信息不丢失。 - 同步整理缓存键、缓存实现、XML 编解码相关测试和 bootstrap 格式。 --- src/Contract/ConfigInterface.php | 6 ++++-- src/Contract/StoreCacheInterface.php | 6 ++++-- src/Contract/StoreTokenInterface.php | 6 ++++-- src/Exception/ApiException.php | 6 ++++-- src/Exception/SignatureException.php | 6 ++++-- src/Exception/WechatException.php | 13 ++++++------- src/Support/CacheKey.php | 10 ++++++---- src/Support/FileCacheStore.php | 8 +++++--- src/Support/NullCacheStore.php | 6 ++++-- src/Support/PsrSimpleCacheStore.php | 6 ++++-- src/Support/TokenCacheKey.php | 10 +++++++--- src/Support/Xml.php | 13 +++++++------ tests/CacheKeyTest.php | 7 +++++-- tests/CacheStoreTest.php | 12 +++++++----- tests/XmlTest.php | 7 +++++-- tests/bootstrap.php | 7 ++++--- 16 files changed, 80 insertions(+), 49 deletions(-) diff --git a/src/Contract/ConfigInterface.php b/src/Contract/ConfigInterface.php index 1364658..21149a9 100644 --- a/src/Contract/ConfigInterface.php +++ b/src/Contract/ConfigInterface.php @@ -1,9 +1,11 @@ */ namespace We\Contract; diff --git a/src/Contract/StoreCacheInterface.php b/src/Contract/StoreCacheInterface.php index 316d227..81477f2 100644 --- a/src/Contract/StoreCacheInterface.php +++ b/src/Contract/StoreCacheInterface.php @@ -1,9 +1,11 @@ */ namespace We\Contract; diff --git a/src/Contract/StoreTokenInterface.php b/src/Contract/StoreTokenInterface.php index ff82309..a33ad40 100644 --- a/src/Contract/StoreTokenInterface.php +++ b/src/Contract/StoreTokenInterface.php @@ -1,9 +1,11 @@ */ namespace We\Contract; diff --git a/src/Exception/ApiException.php b/src/Exception/ApiException.php index c8ce67b..7131c04 100644 --- a/src/Exception/ApiException.php +++ b/src/Exception/ApiException.php @@ -1,9 +1,11 @@ */ namespace We\Exception; diff --git a/src/Exception/SignatureException.php b/src/Exception/SignatureException.php index 31209e2..79957e7 100644 --- a/src/Exception/SignatureException.php +++ b/src/Exception/SignatureException.php @@ -1,9 +1,11 @@ */ namespace We\Exception; diff --git a/src/Exception/WechatException.php b/src/Exception/WechatException.php index 6bc4cd5..4bbb42b 100644 --- a/src/Exception/WechatException.php +++ b/src/Exception/WechatException.php @@ -1,22 +1,21 @@ */ namespace We\Exception; -use RuntimeException; -use Throwable; - /** * SDK 基础异常。 * * 通过 context() 暴露平台响应、验签明细等上下文,便于业务侧记录日志和排查问题。 */ -class WechatException extends RuntimeException +class WechatException extends \RuntimeException { /** * 创建 SDK 异常并保存可选上下文。 @@ -26,7 +25,7 @@ class WechatException extends RuntimeException public function __construct( string $message, int $code = 0, - ?Throwable $previous = null, + ?\Throwable $previous = null, private readonly array $context = [] ) { parent::__construct($message, $code, $previous); diff --git a/src/Support/CacheKey.php b/src/Support/CacheKey.php index 02a1c9a..e2382d8 100644 --- a/src/Support/CacheKey.php +++ b/src/Support/CacheKey.php @@ -1,9 +1,11 @@ */ namespace We\Support; @@ -18,9 +20,9 @@ final class CacheKey /** * 组合缓存完整键。 * - * @param string $prefix 根 Client 通用命名段,不得为空,用于区分部署、租户或应用。 + * @param string $prefix 根 Client 通用命名段,不得为空,用于区分部署、租户或应用 * @param string $channel 通道段,如 `wechat.platform`,必须与 Client 通道标识一致。 - * @param string $logicalKey 业务逻辑键,如 TokenCacheKey 生成的 access_token 键。 + * @param string $logicalKey 业务逻辑键,如 TokenCacheKey 生成的 access_token 键 */ public static function compose(string $prefix, string $channel, string $logicalKey): string { diff --git a/src/Support/FileCacheStore.php b/src/Support/FileCacheStore.php index d4ca71e..578e1f5 100644 --- a/src/Support/FileCacheStore.php +++ b/src/Support/FileCacheStore.php @@ -1,9 +1,11 @@ */ namespace We\Support; @@ -60,7 +62,7 @@ final class FileCacheStore implements StoreCacheInterface return $default; } - /** @var array{expires_at?:int,value?:mixed}|null $payload */ + /** @var null|array{expires_at?:int,value?:mixed} $payload */ $payload = json_decode($raw, true); if (!is_array($payload) || !array_key_exists('expires_at', $payload) || !array_key_exists('value', $payload)) { return $default; diff --git a/src/Support/NullCacheStore.php b/src/Support/NullCacheStore.php index e3a8e27..3698ec9 100644 --- a/src/Support/NullCacheStore.php +++ b/src/Support/NullCacheStore.php @@ -1,9 +1,11 @@ */ namespace We\Support; diff --git a/src/Support/PsrSimpleCacheStore.php b/src/Support/PsrSimpleCacheStore.php index ece44dd..811986b 100644 --- a/src/Support/PsrSimpleCacheStore.php +++ b/src/Support/PsrSimpleCacheStore.php @@ -1,9 +1,11 @@ */ namespace We\Support; diff --git a/src/Support/TokenCacheKey.php b/src/Support/TokenCacheKey.php index 1802e3c..b8136bf 100644 --- a/src/Support/TokenCacheKey.php +++ b/src/Support/TokenCacheKey.php @@ -1,18 +1,22 @@ */ namespace We\Support; +use We\Client; + /** * 微信接口调用凭据缓存逻辑键统一按「微信侧 appid 优先」分段,便于 Redis 等存储按应用前缀扫描和清理。 * * 约定前缀:`wechat:app:{appid}:...`;若 Config 带 `storageScope`(业务隔离),追加 `:scope:{值}`。 - * 经根 {@see \We\Client} 与各通道写入存储时,作为第三段与通用前缀、平台段经 {@see CacheKey::compose} 拼成固定三段键。 + * 经根 {@see Client} 与各通道写入存储时,作为第三段与通用前缀、平台段经 {@see CacheKey::compose} 拼成固定三段键。 */ final class TokenCacheKey { diff --git a/src/Support/Xml.php b/src/Support/Xml.php index 196fe71..65d6d29 100644 --- a/src/Support/Xml.php +++ b/src/Support/Xml.php @@ -1,14 +1,15 @@ */ namespace We\Support; -use SimpleXMLElement; use We\Exception\WechatException; /** @@ -31,9 +32,9 @@ final class Xml } $previous = libxml_use_internal_errors(true); - $element = simplexml_load_string($xml, SimpleXMLElement::class, LIBXML_NOCDATA); + $element = simplexml_load_string($xml, \SimpleXMLElement::class, LIBXML_NOCDATA); libxml_use_internal_errors($previous); - if (!$element instanceof SimpleXMLElement) { + if (!$element instanceof \SimpleXMLElement) { throw new WechatException('微信 XML 格式无效'); } @@ -89,7 +90,7 @@ final class Xml * * @return array */ - private static function normalize(SimpleXMLElement $element): array + private static function normalize(\SimpleXMLElement $element): array { $result = []; foreach ($element->children() as $key => $value) { diff --git a/tests/CacheKeyTest.php b/tests/CacheKeyTest.php index 9ccc74a..ecddf45 100644 --- a/tests/CacheKeyTest.php +++ b/tests/CacheKeyTest.php @@ -1,9 +1,11 @@ */ namespace We\Tests; @@ -16,6 +18,7 @@ use We\Support\TokenCacheKey; /** * SDK 缓存键生成规则测试用例。 + * @internal */ #[CoversClass(CacheKey::class)] final class CacheKeyTest extends TestCase diff --git a/tests/CacheStoreTest.php b/tests/CacheStoreTest.php index cf31a0e..a84c9db 100644 --- a/tests/CacheStoreTest.php +++ b/tests/CacheStoreTest.php @@ -1,14 +1,15 @@ */ namespace We\Tests; -use DateInterval; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; use Psr\SimpleCache\CacheInterface; @@ -19,6 +20,7 @@ use We\Support\PsrSimpleCacheStore; /** * SDK 缓存存储实现测试用例。 + * @internal */ #[CoversClass(FileCacheStore::class)] #[CoversClass(NullCacheStore::class)] @@ -146,7 +148,7 @@ final class ArraySimpleCache implements CacheInterface /** * 写入缓存值。 */ - public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool + public function set(string $key, mixed $value, \DateInterval|int|null $ttl = null): bool { $this->values[$key] = $value; @@ -186,7 +188,7 @@ final class ArraySimpleCache implements CacheInterface /** * 批量写入测试缓存值。 */ - public function setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool + public function setMultiple(iterable $values, \DateInterval|int|null $ttl = null): bool { foreach ($values as $key => $value) { $this->set((string)$key, $value, $ttl); diff --git a/tests/XmlTest.php b/tests/XmlTest.php index 4974c56..c5e38b5 100644 --- a/tests/XmlTest.php +++ b/tests/XmlTest.php @@ -1,9 +1,11 @@ */ namespace We\Tests; @@ -14,6 +16,7 @@ use We\Support\Xml; /** * 微信 XML 编解码工具测试用例。 + * @internal */ #[CoversClass(Xml::class)] final class XmlTest extends TestCase diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 861a38e..d791133 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,11 +1,12 @@ */ - $autoloadCandidates = [ dirname(__DIR__) . '/vendor/autoload.php', dirname(__DIR__, 2) . '/vendor/autoload.php',