Skip to content

Commit

Permalink
Merge pull request #13 from TheNorthMemory/main
Browse files Browse the repository at this point in the history
fully strict typing source codes
  • Loading branch information
xy-peng authored Jul 8, 2021
2 parents fd752cd + 87d7a30 commit 1cbac5d
Show file tree
Hide file tree
Showing 24 changed files with 57 additions and 46 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# 变更历史

## 1.0.5 - 2021-07-08

[变更细节](../../compare/v1.0.4...v1.0.5)

- 核心代码全部转入严格类型`declare(strict_types=1)`校验模式;
- 调整 `Authorization` 头格式顺序,debug时优先展示关键信息;
- 调整 媒体文件`MediaUtil`类读取文件时,严格二进制读,避免跨平台干扰问题;
- 增加 测试用例覆盖`APIv2`版用法;

## 1.0.4 - 2021-07-05

[变更细节](../../compare/v1.0.3...v1.0.4)
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ APIv3已内置 `请求签名` 和 `应答验签` 两个middleware中间件,创

## 项目状态

当前版本为`1.0.4`测试版本。请商户的专业技术人员在使用时注意系统和软件的正确性和兼容性,以及带来的风险。
当前版本为`1.0.5`测试版本。请商户的专业技术人员在使用时注意系统和软件的正确性和兼容性,以及带来的风险。


## 环境要求
Expand Down Expand Up @@ -51,7 +51,7 @@ composer require wechatpay/wechatpay

```json
"require": {
"wechatpay/wechatpay": "^1.0.4"
"wechatpay/wechatpay": "^1.0.5"
}
```

Expand Down
2 changes: 1 addition & 1 deletion bin/CertificateDownloader.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env php
<?php
<?php declare(strict_types=1);

// load autoload.php
$possibleFiles = [__DIR__.'/../vendor/autoload.php', __DIR__.'/../../../autoload.php', __DIR__.'/../../autoload.php'];
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wechatpay/wechatpay",
"version": "1.0.4",
"version": "1.0.5",
"description": "[A]Sync Chainable WeChatPay v2&v3's OpenAPI SDK for PHP",
"type": "library",
"keywords": [
Expand Down
6 changes: 3 additions & 3 deletions src/Builder.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace WeChatPay;

Expand All @@ -25,8 +25,8 @@ final class Builder
* - certs: array<string, \OpenSSLAsymmetricKey|\OpenSSLCertificate|object|resource|string> - The wechatpay platform serial and certificate(s), `[$serial => $cert]` pair
* - secret?: string - The secret key string (optional)
* - merchant?: array{key?: string, cert?: string} - The merchant private key and certificate array. (optional)
* - merchant<?key, string> - The merchant private key(file path string). (optional)
* - merchant<?cert, string> - The merchant certificate(file path string). (optional)
* - merchant<?key, string|string[]> - The merchant private key(file path string). (optional)
* - merchant<?cert, string|string[]> - The merchant certificate(file path string). (optional)
*
* ```php
* // usage samples
Expand Down
2 changes: 1 addition & 1 deletion src/BuilderChainable.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace WeChatPay;

Expand Down
2 changes: 1 addition & 1 deletion src/BuilderTrait.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace WeChatPay;

Expand Down
2 changes: 1 addition & 1 deletion src/ClientDecorator.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace WeChatPay;

Expand Down
4 changes: 2 additions & 2 deletions src/ClientDecoratorInterface.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace WeChatPay;

Expand All @@ -14,7 +14,7 @@ interface ClientDecoratorInterface
/**
* @var string - This library version
*/
const VERSION = '1.0.4';
const VERSION = '1.0.5';

/**
* @var string - The HTTP transfer `xml` based protocol
Expand Down
2 changes: 1 addition & 1 deletion src/ClientJsonTrait.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace WeChatPay;

Expand Down
2 changes: 1 addition & 1 deletion src/ClientXmlTrait.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace WeChatPay;

Expand Down
6 changes: 3 additions & 3 deletions src/Crypto/AesEcb.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace WeChatPay\Crypto;

Expand All @@ -21,7 +21,7 @@ class AesEcb implements AesInterface
*/
public static function encrypt(string $plaintext, string $key, string $iv = ''): string
{
$ciphertext = openssl_encrypt($plaintext, static::ALGO_AES_256_ECB, $key, OPENSSL_RAW_DATA, $iv);
$ciphertext = openssl_encrypt($plaintext, static::ALGO_AES_256_ECB, $key, OPENSSL_RAW_DATA, $iv = '');

if (false === $ciphertext) {
throw new UnexpectedValueException('Encrypting the input $plaintext failed, please checking your $key and $iv whether or nor correct.');
Expand All @@ -35,7 +35,7 @@ public static function encrypt(string $plaintext, string $key, string $iv = ''):
*/
public static function decrypt(string $ciphertext, string $key, string $iv = ''): string
{
$plaintext = openssl_decrypt(base64_decode($ciphertext), static::ALGO_AES_256_ECB, $key, OPENSSL_RAW_DATA, $iv);
$plaintext = openssl_decrypt(base64_decode($ciphertext), static::ALGO_AES_256_ECB, $key, OPENSSL_RAW_DATA, $iv = '');

if (false === $plaintext) {
throw new UnexpectedValueException('Decrypting the input $ciphertext failed, please checking your $key and $iv whether or nor correct.');
Expand Down
2 changes: 1 addition & 1 deletion src/Crypto/AesGcm.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace WeChatPay\Crypto;

Expand Down
2 changes: 1 addition & 1 deletion src/Crypto/AesInterface.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace WeChatPay\Crypto;

Expand Down
2 changes: 1 addition & 1 deletion src/Crypto/Hash.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace WeChatPay\Crypto;

Expand Down
2 changes: 1 addition & 1 deletion src/Crypto/Rsa.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace WeChatPay\Crypto;

Expand Down
2 changes: 1 addition & 1 deletion src/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace WeChatPay\Exception;

Expand Down
2 changes: 1 addition & 1 deletion src/Exception/WeChatPayException.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace WeChatPay\Exception;

Expand Down
6 changes: 3 additions & 3 deletions src/Formatter.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace WeChatPay;

Expand Down Expand Up @@ -58,8 +58,8 @@ public static function timestamp(): int
public static function authorization(string $mchid, string $nonce, string $signature, string $timestamp, string $serial): string
{
return sprintf(
'WECHATPAY2-SHA256-RSA2048 mchid="%s",nonce_str="%s",signature="%s",timestamp="%s",serial_no="%s"',
$mchid, $nonce, $signature, $timestamp, $serial
'WECHATPAY2-SHA256-RSA2048 mchid="%s",serial_no="%s",timestamp="%s",nonce_str="%s",signature="%s"',
$mchid, $serial, $timestamp, $nonce, $signature
);
}

Expand Down
8 changes: 4 additions & 4 deletions src/Transformer.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace WeChatPay;

Expand Down Expand Up @@ -42,7 +42,7 @@ public static function toArray(string $xml = '<xml/>'): array

$el = simplexml_load_string(static::sanitize($xml), SimpleXMLElement::class, LIBXML_NONET | LIBXML_COMPACT | LIBXML_NOCDATA | LIBXML_NOBLANKS);

LIBXML_VERSION < 20900 && libxml_disable_entity_loader($previous); /** @phpstan-ignore-line */
LIBXML_VERSION < 20900 && isset($previous) && libxml_disable_entity_loader($previous);

return static::cast($el);
}
Expand Down Expand Up @@ -125,7 +125,7 @@ public static function toXml(array $data, bool $headless = true, bool $indent =
protected static function walk(XMLWriter &$writer, array $data, string $item): void
{
foreach ($data as $key => $value) {
$tag = static::isElementNameValid($key) ? $key : $item;
$tag = is_string($key) && static::isElementNameValid($key) ? $key : $item;
$writer->startElement($tag);
if (is_array($value) || (is_object($value) && $value instanceof Traversable)) {
static::walk($writer, (array) $value, $item);
Expand Down Expand Up @@ -180,6 +180,6 @@ protected static function isElementNameValid(string $name = ''): bool
*/
protected static function needsCdataWrapping(string $value = ''): bool
{
return 0 < preg_match('#[>&"<]#', $value);
return $value && 0 < preg_match('#[>&"<]#', $value);
}
}
2 changes: 1 addition & 1 deletion src/Util/MediaUtil.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace WeChatPay\Util;

Expand Down
2 changes: 1 addition & 1 deletion src/Util/PemUtil.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace WeChatPay\Util;

Expand Down
6 changes: 3 additions & 3 deletions tests/FormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ public function testAuthorization(): void

$pattern = '/^WECHATPAY2-SHA256-RSA2048 '
. 'mchid="[0-9A-Za-z]{1,32}",'
. 'nonce_str="[0-9A-Za-z]{16,}",'
. 'signature="[0-9A-Za-z\+\/]+={0,2}",'
. 'serial_no="[0-9A-Za-z]{8,40}",'
. 'timestamp="1[0-9]{9}",'
. 'serial_no="[0-9A-Za-z]{8,40}"$/';
. 'nonce_str="[0-9A-Za-z]{16,}",'
. 'signature="[0-9A-Za-z\+\/]+={0,2}"$/';

if (method_exists($this, 'assertMatchesRegularExpression')) {
$this->assertMatchesRegularExpression($pattern, $value);
Expand Down
24 changes: 13 additions & 11 deletions tests/Util/PemUtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
use const PHP_MAJOR_VERSION;
use const PHP_MINOR_VERSION;
use const PHP_SHLIB_SUFFIX;
use const PHP_EOL;
use const OPENSSL_KEYTYPE_RSA;
use const DIRECTORY_SEPARATOR;

use function dirname;
use function sprintf;
use function mt_rand;
use function touch;
use function unlink;
use function openssl_pkey_new;
use function openssl_csr_new;
use function openssl_csr_sign;
Expand All @@ -37,10 +39,8 @@ protected function setUp(): void
if (7 === PHP_MAJOR_VERSION && in_array(PHP_MINOR_VERSION, [2, 3]) && 'dll' === PHP_SHLIB_SUFFIX
&& '' === $certString && '' === $privString) {
$this->markTestSkipped(
'Known issues were there on the `openssl_csr_new` and `openssl_csr_sign` functions.'
. PHP_EOL
'Known issues were there about the `openssl_csr_new` and `openssl_csr_sign` functions.'
. 'Those may not works well on the Windows\'s PHP7.2 & PHP7.3 series.'
. PHP_EOL
. 'And caused the `$environment` in bad phrases.'
);
}
Expand Down Expand Up @@ -75,6 +75,9 @@ public static function setUpBeforeClass(): void
$certString = '';
$privString = '';

touch($certFile);
touch($privFile);

$csr = false !== $privateKey ? openssl_csr_new(self::$certSubject, $privateKey, $baseAlgo) : false;
$cert = false !== $csr ? openssl_csr_sign($csr, null, $privateKey, 1, $baseAlgo, $serial) : false;

Expand All @@ -89,13 +92,12 @@ public static function setUpBeforeClass(): void

public static function tearDownAfterClass(): void
{
try {
[, $certFile, , $privFile] = self::$environment;
unlink($certFile);
unlink($privFile);
} finally {
self::$environment = null;
}
[, $certFile, , $privFile] = self::$environment;

unlink($certFile);
unlink($privFile);

self::$environment = null;
}

public function testLoadCertificate(): void
Expand Down

0 comments on commit 1cbac5d

Please sign in to comment.