From 0fb2a06c05879a17e12e80690d37a0d93ee97333 Mon Sep 17 00:00:00 2001 From: Ion Bazan Date: Fri, 25 Oct 2024 14:23:53 +0800 Subject: [PATCH] Require PHP 8.1+ --- .github/workflows/php.yml | 8 ++-- .php-cs-fixer.dist.php | 8 ++-- composer.json | 11 +++--- phpunit.xml.dist | 38 +++++++++--------- src/Guzzle/RequestSignerMiddleware.php | 6 +-- src/HttPlug/RequestSignerPlugin.php | 6 +-- src/Key.php | 27 ++++--------- src/RequestSigner.php | 23 ++++------- tests/RequestSignerTest.php | 53 ++++++++++++-------------- 9 files changed, 70 insertions(+), 110 deletions(-) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index dbe0740..f0e9927 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -9,16 +9,12 @@ jobs: matrix: dependencies: [highest] php-versions: - - '7.2' - - '7.3' - - '7.4' - - '8.0' - '8.1' - '8.2' - '8.3' - '8.4' include: - - php-versions: '7.2' + - php-versions: '8.1' dependencies: 'lowest' - php-versions: '8.5' dependencies: 'highest' @@ -41,6 +37,8 @@ jobs: run: vendor/bin/phpunit - name: Upload coverage to Codecov uses: codecov/codecov-action@v4 + with: + token: ${{ secrets.CODECOV_TOKEN }} - name: Run mutation tests if: ${{ matrix.php-versions == 8.4 }} env: diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index d155566..cd71584 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -9,17 +9,15 @@ ->setRules([ '@Symfony' => true, '@Symfony:risky' => true, - '@PHP71Migration' => true, - '@PHP71Migration:risky' => true, + '@PHP81Migration' => true, + '@PHP80Migration:risky' => true, + '@PHPUnit100Migration:risky' => true, 'ordered_imports' => true, 'global_namespace_import' => [ 'import_classes' => true, 'import_constants' => true, 'import_functions' => true, ], - 'trailing_comma_in_multiline' => [ - 'elements' => ['arrays', 'array_destructuring'], - ], ]) ->setRiskyAllowed(true) ->setFinder( diff --git a/composer.json b/composer.json index 435664e..93a3dd3 100644 --- a/composer.json +++ b/composer.json @@ -24,22 +24,23 @@ } ], "require": { - "php": ">=7.2", + "php": "^8.1", "psr/http-message": "^1.0 || ^2.0", - "ramsey/uuid": "^3.0 || ^4.0 || ^5.0" + "ramsey/uuid": "^3.0 || ^4.0 || ^5.0", + "symfony/polyfill-php82": "^1.22" }, "require-dev": { - "friendsofphp/php-cs-fixer": "^3.0", + "friendsofphp/php-cs-fixer": "^3.64", "guzzlehttp/guzzle": "^6.0 || ^7.0", "infection/infection": "~0.15", "laminas/laminas-diactoros": "^2.2 || ^3.0", "php-http/client-common": "^2.0", - "phpunit/phpunit": "^8 || ^9 || ^10 || ^11" + "phpunit/phpunit": "^10.5 || ^11" }, "suggest": { "guzzlehttp/guzzle": "^6.0|^7.0, to use Guzzle Middleware", "ion-bazan/guzzle-bundle-aliyun-signer-plugin": "to integrate with Guzzle Bundle", - "laminas/laminas-diactoros": "^2.2, to build HTTP requests", + "laminas/laminas-diactoros": "^2.2 || ^3.0, to build HTTP requests", "php-http/client-common": "^2.0, to use HttPlug plugin" }, "config": { diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 6655582..7b39353 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,23 +1,19 @@ - - - - tests - - - - - - src - - - - - - + + + + + + + + + tests + + + + + + src + + diff --git a/src/Guzzle/RequestSignerMiddleware.php b/src/Guzzle/RequestSignerMiddleware.php index d94cc2d..3bab68f 100644 --- a/src/Guzzle/RequestSignerMiddleware.php +++ b/src/Guzzle/RequestSignerMiddleware.php @@ -10,12 +10,8 @@ class RequestSignerMiddleware { - /** @var RequestSigner */ - protected $requestSigner; - - public function __construct(RequestSigner $requestSigner) + public function __construct(protected readonly RequestSigner $requestSigner) { - $this->requestSigner = $requestSigner; } public function __invoke(callable $handler): Closure diff --git a/src/HttPlug/RequestSignerPlugin.php b/src/HttPlug/RequestSignerPlugin.php index 6384887..3b3a2b2 100644 --- a/src/HttPlug/RequestSignerPlugin.php +++ b/src/HttPlug/RequestSignerPlugin.php @@ -11,12 +11,8 @@ class RequestSignerPlugin implements Plugin { - /** @var RequestSigner */ - protected $requestSigner; - - public function __construct(RequestSigner $requestSigner) + public function __construct(protected readonly RequestSigner $requestSigner) { - $this->requestSigner = $requestSigner; } public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise diff --git a/src/Key.php b/src/Key.php index 2a9a44c..c5d2c80 100644 --- a/src/Key.php +++ b/src/Key.php @@ -4,27 +4,14 @@ namespace IonBazan\AliyunSigner; +use SensitiveParameter; + class Key { - /** @var string */ - protected $id; - - /** @var string */ - protected $secret; - - public function __construct(string $id, string $secret) - { - $this->id = $id; - $this->secret = $secret; - } - - public function getId(): string - { - return $this->id; - } - - public function getSecret(): string - { - return $this->secret; + public function __construct( + public readonly string $id, + #[SensitiveParameter] + public readonly string $secret, + ) { } } diff --git a/src/RequestSigner.php b/src/RequestSigner.php index a821d8b..b2b761d 100644 --- a/src/RequestSigner.php +++ b/src/RequestSigner.php @@ -12,7 +12,6 @@ use Ramsey\Uuid\Uuid; use function sprintf; -use function strlen; class RequestSigner { @@ -26,13 +25,7 @@ class RequestSigner public const HEADER_DATE = 'Date'; public const HEADER_CONTENT_MD5 = 'Content-MD5'; - /** @var Key */ - private $key; - - /** @var Digest */ - private $digest; - - private $signatureHeaders = [ + private array $signatureHeaders = [ self::HEADER_X_CA_KEY, self::HEADER_X_CA_NONCE, self::HEADER_X_CA_SIGNATURE_METHOD, @@ -40,25 +33,23 @@ class RequestSigner self::HEADER_X_CA_STAGE, ]; - public function __construct(Key $key, ?DigestInterface $digest = null) + public function __construct(private readonly Key $key, private readonly DigestInterface $digest = new Digest()) { - $this->key = $key; - $this->digest = $digest ?? new Digest(); } public function signRequest(RequestInterface $request, ?DateTimeInterface $date = null, ?string $nonce = null): RequestInterface { - $nonce = $nonce ?? Uuid::uuid4()->toString(); + $nonce ??= Uuid::uuid4()->toString(); $timestamp = ($date ?? new DateTime())->format('Uv'); $body = $request->getBody()->getContents(); - $contentMd5 = strlen($body) ? base64_encode(md5($body, true)) : ''; + $contentMd5 = $body !== '' ? base64_encode(md5($body, true)) : ''; $request = $request->withHeader(self::HEADER_DATE, $timestamp) ->withHeader(self::HEADER_CONTENT_MD5, $contentMd5) ->withHeader(self::HEADER_X_CA_SIGNATURE_METHOD, 'HmacSHA256') ->withHeader(self::HEADER_X_CA_TIMESTAMP, $timestamp) - ->withHeader(self::HEADER_X_CA_KEY, $this->key->getId()) + ->withHeader(self::HEADER_X_CA_KEY, $this->key->id) ->withHeader(self::HEADER_X_CA_NONCE, $nonce); $headers = $this->getHeadersToSign($request); @@ -73,7 +64,7 @@ public function signRequest(RequestInterface $request, ?DateTimeInterface $date $this->getUrlToSign($request), ]); - $signature = $this->digest->sign($textToSign, $this->key->getSecret()); + $signature = $this->digest->sign($textToSign, $this->key->secret); return $request->withHeader(self::HEADER_X_CA_SIGNATURE, $signature) ->withHeader(self::HEADER_X_CA_SIGNATURE_HEADERS, implode(',', array_keys($headers))); @@ -96,7 +87,7 @@ protected function getUrlToSign(RequestInterface $request): string { $query = urldecode($request->getUri()->getQuery()); - return $request->getUri()->getPath().(strlen($query) ? '?'.$query : ''); + return $request->getUri()->getPath().($query !== '' ? '?'.$query : ''); } protected function getHeadersToSign(RequestInterface $request): array diff --git a/tests/RequestSignerTest.php b/tests/RequestSignerTest.php index 6e6e91d..8be35d1 100644 --- a/tests/RequestSignerTest.php +++ b/tests/RequestSignerTest.php @@ -10,14 +10,13 @@ use IonBazan\AliyunSigner\RequestSigner; use Laminas\Diactoros\Request; use Laminas\Diactoros\StreamFactory; +use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use Psr\Http\Message\RequestInterface; class RequestSignerTest extends TestCase { - /** - * @dataProvider requestDataProvider - */ + #[DataProvider('requestDataProvider')] public function testRequestMessageSignature(RequestInterface $request, string $nonce, string $expectedMessage, string $bodyMd5 = ''): void { $digest = $this->createMock(DigestInterface::class); @@ -52,18 +51,17 @@ public static function requestDataProvider(): array ), 'test-nonce', << [ new Request( @@ -74,18 +72,17 @@ public static function requestDataProvider(): array ), 'test-nonce', <<assertSame($signature, $request->getHeaderLine('X-Ca-Signature')); $this->assertTrue($request->hasHeader('X-Ca-Nonce'));