From 87b930ae67100939e0c797bc3da95facd6171043 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Thu, 27 Aug 2026 13:09:34 +0200 Subject: [PATCH] Throw when list formatting fails --- extra/intl-extra/IntlExtension.php | 7 ++++++- extra/intl-extra/Tests/IntlExtensionTest.php | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/extra/intl-extra/IntlExtension.php b/extra/intl-extra/IntlExtension.php index 7bdc3e89d..901e18ea2 100644 --- a/extra/intl-extra/IntlExtension.php +++ b/extra/intl-extra/IntlExtension.php @@ -451,7 +451,12 @@ final class IntlExtension extends AbstractExtension throw new RuntimeError('The "format_list" filter requires the "IntlListFormatter" class, which is available since PHP 8.5.'); } - return $this->createListFormatter($locale, $type, $width)->format($strings); + $formatter = $this->createListFormatter($locale, $type, $width); + if (false === $ret = $formatter->format($strings)) { + throw new RuntimeError(\sprintf('Unable to format the given list: %s', $formatter->getErrorMessage())); + } + + return $ret; } private function createDateFormatter(?string $locale, ?string $dateFormat, ?string $timeFormat, string $pattern, ?\DateTimeZone $timezone, ?string $calendar): \IntlDateFormatter diff --git a/extra/intl-extra/Tests/IntlExtensionTest.php b/extra/intl-extra/Tests/IntlExtensionTest.php index 4d306c7c3..0fef4b817 100644 --- a/extra/intl-extra/Tests/IntlExtensionTest.php +++ b/extra/intl-extra/Tests/IntlExtensionTest.php @@ -13,6 +13,7 @@ namespace Twig\Extra\Intl\Tests; use PHPUnit\Framework\TestCase; use Twig\Environment; +use Twig\Error\RuntimeError; use Twig\Extension\CoreExtension; use Twig\Extra\Intl\IntlExtension; use Twig\Loader\ArrayLoader; @@ -223,4 +224,22 @@ class IntlExtensionTest extends TestCase $this->assertLessThanOrEqual(100, \count($cache)); $this->assertGreaterThan(1, \count($cache)); } + + public function testFormatListThrowsOnFailure(): void + { + if (!class_exists('IntlListFormatter')) { + $this->markTestSkipped('IntlListFormatter is not available.'); + } + + $strings = ['Alice', "\xB1\x31"]; + $formatter = new \IntlListFormatter('en', \IntlListFormatter::TYPE_AND, \IntlListFormatter::WIDTH_WIDE); + if (false !== $formatter->format($strings)) { + $this->markTestSkipped('IntlListFormatter accepts the malformed UTF-8 input.'); + } + + $this->expectException(RuntimeError::class); + $this->expectExceptionMessage('Unable to format the given list: '.$formatter->getErrorMessage()); + + (new IntlExtension())->formatList($strings, locale: 'en'); + } }