From 6bab185d08f08cc7c356b7c9ddd29612ae2e071b Mon Sep 17 00:00:00 2001 From: Cyril PASCAL Date: Wed, 23 Feb 2022 12:01:56 +0100 Subject: [PATCH 1/2] Add PHP 8.1 compatibility --- .github/workflows/ci.yml | 2 +- .gitignore | 1 + composer.json | 5 +++-- src/MultibyteStringStream.php | 34 +++++++++++++---------------- tests/MultibyteStringStreamTest.php | 34 ++++++++++++++--------------- 5 files changed, 37 insertions(+), 39 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 99fb444..3ad528d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -12,7 +12,7 @@ jobs: strategy: matrix: - php-version: ['7.0', '7.1', '7.2'] + php-version: ['8.1'] steps: - uses: actions/checkout@v2 diff --git a/.gitignore b/.gitignore index 16d1853..9ce4e9d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /vendor/ composer.lock .DS_STORE +/.phpunit.result.cache diff --git a/composer.json b/composer.json index 2afc899..a95221c 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "description": "An mbstring stream conversion filter.", "type": "library", "require-dev": { - "phpunit/phpunit": ">=6.3 <8" + "phpunit/phpunit": "^9.5" }, "license": "MIT", "authors": [ @@ -13,7 +13,8 @@ } ], "require": { - "php": ">=7.0" + "php": "8.1.x", + "ext-mbstring": "*" }, "autoload": { "classmap": ["src/"] diff --git a/src/MultibyteStringStream.php b/src/MultibyteStringStream.php index 48abf98..2b6c55f 100644 --- a/src/MultibyteStringStream.php +++ b/src/MultibyteStringStream.php @@ -4,28 +4,22 @@ class MultibyteStringStream extends php_user_filter { const NON_CHARACTER = "\xFF\xFF"; - // @var string - public $filtername; + public string $filtername; - // @var mixed - public $params; + public mixed $params; - // @var resource + /** @var resource */ public $stream; - // @var string - private $to_encoding; + private string $to_encoding; - // @var string - private $from_encoding; + private string $from_encoding; - // @var int - private $prev_mb_substitute_character; + private int $prev_mb_substitute_character; - // @var string - private $buffer; + private ?string $buffer; - public function onCreate() { + public function onCreate(): bool { $conversion_part = substr($this->filtername, 17); $conversion_part = explode('/', $conversion_part); @@ -51,13 +45,15 @@ public function onCreate() { $this->to_encoding = $to_encoding; $this->from_encoding = $from_encoding; + + return true; } - public function onClose() { + public function onClose(): void { mb_substitute_character($this->prev_mb_substitute_character); } - public function filter($in, $out, &$consumed, $closing) { + public function filter($in, $out, &$consumed, $closing): int { $buffer = &$this->buffer; $pass_on = false; @@ -93,13 +89,13 @@ public function filter($in, $out, &$consumed, $closing) { return $pass_on ? PSFS_PASS_ON : PSFS_FEED_ME; } - private function truncateInvalidCharacters($data) { + private function truncateInvalidCharacters(string $data): string { $padded_data = $data . self::NON_CHARACTER; return mb_strcut($padded_data, 0, strlen($data), $this->from_encoding); } - private function convert($data) { + private function convert(string $data): string { return mb_convert_encoding( $data, $this->to_encoding, @@ -107,7 +103,7 @@ private function convert($data) { ); } - public static function registerStreamFilter() { + public static function registerStreamFilter(): void { stream_filter_register('convert.mbstring.*', __CLASS__); } diff --git a/tests/MultibyteStringStreamTest.php b/tests/MultibyteStringStreamTest.php index bc451db..10f8e70 100644 --- a/tests/MultibyteStringStreamTest.php +++ b/tests/MultibyteStringStreamTest.php @@ -2,15 +2,15 @@ class MultibyteStringStreamTest extends \PHPUnit\Framework\TestCase { - public function setUp() { + public function setUp(): void { MultibyteStringStream::registerStreamFilter(); } - public function testStreamFilterWasRegistered() { + public function testStreamFilterWasRegistered(): void { $this->assertContains('convert.mbstring.*', stream_get_filters()); } - public function testValidConversionParams() { + public function testValidConversionParams(): void { $stream = fopen('data://text/plain;base64,', 'r'); $filtername = 'convert.mbstring.US-ASCII/UTF-8'; @@ -22,7 +22,7 @@ public function testValidConversionParams() { ); } - public function testInvalidConversionParams() { + public function testInvalidConversionParams(): void { $stream = fopen('data://text/plain;base64,', 'r'); $filtername = 'convert.mbstring.FAKE/UTF-8'; @@ -34,7 +34,7 @@ public function testInvalidConversionParams() { ); } - public function testDefaultConversionParam() { + public function testDefaultConversionParam(): void { $stream = fopen('data://text/plain;base64,', 'r'); $filtername = 'convert.mbstring.UTF-8'; @@ -46,7 +46,7 @@ public function testDefaultConversionParam() { ); } - public function testDefaultReplacementCharacterParam() { + public function testDefaultReplacementCharacterParam(): void { $stream = fopen('data://text/plain;base64,Zvxy', 'r'); $filtername = 'convert.mbstring.UTF-8/UTF-8'; @@ -62,7 +62,7 @@ public function testDefaultReplacementCharacterParam() { ); } - public function testReplacementCharacterParam() { + public function testReplacementCharacterParam(): void { $stream = fopen('data://text/plain;base64,Zvxy', 'r'); $filtername = 'convert.mbstring.UTF-8/UTF-8'; @@ -78,7 +78,7 @@ public function testReplacementCharacterParam() { ); } - public function testMultibyteEdgeHandling() { + public function testMultibyteEdgeHandling(): void { $output = fopen('php://memory', 'w+'); $filtername = 'convert.mbstring.UTF-8/UTF-8'; @@ -118,7 +118,7 @@ public function testMultibyteEdgeHandling() { ); } - public function testMultibyteEdgeHandlingAfterBucketWrite() { + public function testMultibyteEdgeHandlingAfterBucketWrite(): void { $output = fopen('php://memory', 'w+'); $filtername = 'convert.mbstring.UTF-8/UTF-8'; @@ -142,7 +142,7 @@ public function testMultibyteEdgeHandlingAfterBucketWrite() { ); } - public function testCloseInvalidData() { + public function testCloseInvalidData(): void { $output = fopen('php://output', 'w'); $filtername = 'convert.mbstring.UTF-8/UTF-8'; @@ -166,9 +166,9 @@ public function testCloseInvalidData() { /** * @dataProvider unicodeMappingProvider */ - public function testCharsetConversion($unicode_string, - $charset, - $charset_string) { + public function testCharsetConversion(string $unicode_string, + string $charset, + string $charset_string): void { $input = base64_encode($charset_string); $stream = fopen('data://text/plain;base64,' . $input, 'r'); @@ -186,16 +186,16 @@ public function testCharsetConversion($unicode_string, ); } - public function unicodeMappingProvider() { + public function unicodeMappingProvider(): array { $ucm_files = glob(__DIR__ . '/data/*.ucm'); return array_combine($ucm_files, array_map( - array($this, 'parseUcmFile'), + $this->parseUcmFile(...), $ucm_files )); } - public function parseUcmFile($charset_filepath) { + public function parseUcmFile(string $charset_filepath): array { $charset_filename = basename($charset_filepath, '.ucm'); $unicode_string = ''; $charset_string = ''; @@ -219,7 +219,7 @@ public function parseUcmFile($charset_filepath) { } } - return array($unicode_string, $charset_filename, $charset_string); + return [$unicode_string, $charset_filename, $charset_string]; } } From 0aa616e0ce2d7a9426f22e47bf8def133b388e4c Mon Sep 17 00:00:00 2001 From: Cyril PASCAL Date: Wed, 23 Feb 2022 12:29:45 +0100 Subject: [PATCH 2/2] Remove ~ and \ from SJIS as those characters cause issues See implementation details at https://en.wikipedia.org/wiki/JIS_X_0201 --- tests/data/SJIS.ucm | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/data/SJIS.ucm b/tests/data/SJIS.ucm index c468feb..188f74f 100644 --- a/tests/data/SJIS.ucm +++ b/tests/data/SJIS.ucm @@ -95,7 +95,6 @@ \x59 \x5A \x5B - \x5C \x5D \x5E \x5F @@ -129,7 +128,6 @@ \x7B \x7C \x7D - \x7E \x7F \x81\x91 \x81\x92