Skip to content

Commit

Permalink
Merge pull request #3 from paxal/patch/php81
Browse files Browse the repository at this point in the history
Add PHP 8.1 support
  • Loading branch information
ericnorris authored Dec 5, 2022
2 parents 641d401 + 0aa616e commit 2758e00
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 41 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:

strategy:
matrix:
php-version: ['7.0', '7.1', '7.2']
php-version: ['8.1']

steps:
- uses: actions/checkout@v2
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/vendor/
composer.lock
.DS_STORE
/.phpunit.result.cache
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand All @@ -13,7 +13,8 @@
}
],
"require": {
"php": ">=7.0"
"php": "8.1.x",
"ext-mbstring": "*"
},
"autoload": {
"classmap": ["src/"]
Expand Down
34 changes: 15 additions & 19 deletions src/MultibyteStringStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;

Expand Down Expand Up @@ -93,21 +89,21 @@ 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,
$this->from_encoding
);
}

public static function registerStreamFilter() {
public static function registerStreamFilter(): void {
stream_filter_register('convert.mbstring.*', __CLASS__);
}

Expand Down
34 changes: 17 additions & 17 deletions tests/MultibyteStringStreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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';

Expand All @@ -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';

Expand All @@ -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';

Expand All @@ -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';

Expand All @@ -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';

Expand Down Expand Up @@ -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';

Expand All @@ -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';

Expand All @@ -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');
Expand All @@ -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 = '';
Expand All @@ -219,7 +219,7 @@ public function parseUcmFile($charset_filepath) {
}
}

return array($unicode_string, $charset_filename, $charset_string);
return [$unicode_string, $charset_filename, $charset_string];
}

}
2 changes: 0 additions & 2 deletions tests/data/SJIS.ucm
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@
<U0059> \x59
<U005A> \x5A
<U005B> \x5B
<U005C> \x5C
<U005D> \x5D
<U005E> \x5E
<U005F> \x5F
Expand Down Expand Up @@ -129,7 +128,6 @@
<U007B> \x7B
<U007C> \x7C
<U007D> \x7D
<U007E> \x7E
<U007F> \x7F
<U00A2> \x81\x91
<U00A3> \x81\x92
Expand Down

0 comments on commit 2758e00

Please sign in to comment.