Skip to content

Commit 0ff9ed4

Browse files
bug #52491 [String] Method toByteString conversion using iconv is unreachable (Vincentv92)
This PR was squashed before being merged into the 5.4 branch. Discussion ---------- [String] Method toByteString conversion using iconv is unreachable | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Issues | Fix #52489 <!-- prefix each issue number with "Fix #", no need to create an issue if none exists, explain below instead --> | License | MIT <!-- Replace this notice by a description of your feature/bugfix. This will help reviewers and should be a good start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - Bug fixes must be submitted against the lowest maintained branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against the latest branch. - For new features, provide some code snippets to help understand usage. - Changelog entry should follow https://symfony.com/doc/current/contributing/code/conventions.html#writing-a-changelog-entry - Never break backward compatibility (see https://symfony.com/bc). --> Originating from original issue: >It is possible that nobody noticed that PHP 8.0 added ValueError exception https://www.php.net/manual/en/function.mb-convert-encoding so the code is not compatible with php >= 8.0 That seems indeed to be the case Commits ------- 08a27c2 [String] Method toByteString conversion using iconv is unreachable
2 parents 2cd1e97 + 08a27c2 commit 0ff9ed4

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/Symfony/Component/String/AbstractString.php

Lines changed: 4 additions & 1 deletion
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

Lines changed: 18 additions & 0 deletions
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy