Throw InvalidArgumentException instead of TypeError for decoding an empty string when using Base62::decode. (#6415)

Co-authored-by: Lu Fei <52o@qq52o.cn>
Co-authored-by: 李铭昕 <715557344@qq.com>
This commit is contained in:
guandeng 2023-12-25 14:07:48 +08:00 committed by GitHub
parent 5f3d52f3e3
commit c1b7d6ae42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 1 deletions

View File

@ -1,5 +1,9 @@
# v3.1.4 - TBD
## Optimized
- [#6415](https://github.com/hyperf/hyperf/pull/6415) Throw `InvalidArgumentException` instead of `TypeError` for decoding an empty string when using `Base62::decode`.
# v3.1.3 - 2023-12-21
## Fixed

View File

@ -32,7 +32,7 @@ class Base62
public static function decode(string $data): int
{
if (strlen($data) !== strspn($data, self::CHARS)) {
if ($data === '' || strlen($data) !== strspn($data, self::CHARS)) {
throw new InvalidArgumentException('The decode data contains content outside of CHARS.');
}
return array_reduce(array_map(function ($character) {

View File

@ -33,5 +33,10 @@ class Base62Test extends TestCase
} catch (Throwable $exception) {
$this->assertInstanceOf(InvalidArgumentException::class, $exception);
}
try {
Base62::decode('');
} catch (Throwable $exception) {
$this->assertInstanceOf(InvalidArgumentException::class, $exception);
}
}
}