Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [3.0.0] – unreleased

- Mb: Make encoding detection stricter.


## [2.0.0] – 2023-03-07

- Iconv: Fix warning on PHP 8.2 when passing `null` as source encoding.
Expand All @@ -23,5 +28,6 @@ The project has been revived and is now available under the name [`fossar/transc
- Added Nix expression for easier development and sharing the environment with CI.
- Switched to GitHub Actions for CI and added more PHP versions.

[2.0.0]: https://github.com/fossar/transcoder/compare/1.0.1...v2.0.0
[3.0.0]: https://github.com/fossar/transcoder/compare/v2.0.0...v3.0.0
[2.0.0]: https://github.com/fossar/transcoder/compare/v1.0.1...v2.0.0
[1.0.1]: https://github.com/fossar/transcoder/compare/1.0.0...v1.0.1
33 changes: 15 additions & 18 deletions src/MbTranscoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,34 +55,31 @@ public function transcode(string $string, $from = null, ?string $to = null): str
} else {
$this->assertSupported($from);
}
} else {
$from = 'auto';
}

if ($to) {
$this->assertSupported($to, false);
}

$handleErrors = !$from || 'auto' === $from;
if ($handleErrors) {
set_error_handler(
function ($no, $warning) use ($string): void {
throw new UndetectableEncodingException($string, $warning);
},
E_WARNING
);
if ($from === 'auto') {
$from = mb_detect_encoding($string, 'auto', true);
}

try {
$result = mb_convert_encoding(
$string,
$to ?: $this->defaultEncoding,
$from ?: 'auto'
);
} finally {
if ($handleErrors) {
restore_error_handler();
}
if ($from === false) {
throw new UndetectableEncodingException($string, 'Unable to detect character encoding');
}

$result = mb_convert_encoding(
$string,
$to ?: $this->defaultEncoding,
$from
);

// For PHPStan: We check the encoding is valid.
assert($result !== false);

return $result;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/MbTranscoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function testUndetectableEncoding(): void
$this->expectException(\Ddeboer\Transcoder\Exception\UndetectableEncodingException::class);
$this->expectExceptionMessage('is undetectable');
$result = $this->transcoder->transcode(
'‘curly quotes make this incompatible with 1252’',
'‘Windows-1252 encodes curly quotes as 0x91 and 0x92, which are indistinguishable from any other single-byte encoding’',
null,
'windows-1252'
);
Expand Down
Loading