Skip to content

Commit 08a27c2

Browse files
Vincentv92nicolas-grekas
authored andcommitted
[String] Method toByteString conversion using iconv is unreachable
1 parent 2cd1e97 commit 08a27c2

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/Symfony/Component/String/AbstractString.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -577,8 +577,11 @@ public function toByteString(string $toEncoding = null): ByteString
577577
try {
578578
try {
579579
$b->string = mb_convert_encoding($this->string, $toEncoding, 'UTF-8');
580-
} catch (InvalidArgumentException $e) {
580+
} catch (InvalidArgumentException|\ValueError $e) {
581581
if (!\function_exists('iconv')) {
582+
if ($e instanceof \ValueError) {
583+
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
584+
}
582585
throw $e;
583586
}
584587

src/Symfony/Component/String/Tests/AbstractAsciiTestCase.php

+18
Original file line numberDiff line numberDiff line change
@@ -1583,4 +1583,22 @@ public static function provideWidth(): array
15831583
[17, "\u{007f}\u{007f}f\u{001b}[0moo\u{0001}bar\u{007f}cccïf\u{008e}cy\u{0005}1", false], // f[0moobarcccïfcy1
15841584
];
15851585
}
1586+
1587+
/**
1588+
* @dataProvider provideToByteString
1589+
*/
1590+
public function testToByteString(string $origin, string $encoding)
1591+
{
1592+
$instance = static::createFromString($origin)->toByteString($encoding);
1593+
$this->assertInstanceOf(ByteString::class, $instance);
1594+
}
1595+
1596+
public static function provideToByteString(): array
1597+
{
1598+
return [
1599+
['žsžsý', 'UTF-8'],
1600+
['žsžsý', 'windows-1250'],
1601+
['žsžsý', 'Windows-1252'],
1602+
];
1603+
}
15861604
}

0 commit comments

Comments
 (0)