From 9eb2c0b1634ec15d77fac19e043525dd742cae76 Mon Sep 17 00:00:00 2001 From: "Alexander M. Turek" Date: Tue, 17 Sep 2024 07:25:44 +0200 Subject: [PATCH] Bugfix: mbstring polyfills must not raise value errors in PHP 7 --- src/Mbstring/Mbstring.php | 60 ++++++++++++++++++++++++++------------ src/Mbstring/bootstrap.php | 12 ++++---- 2 files changed, 48 insertions(+), 24 deletions(-) diff --git a/src/Mbstring/Mbstring.php b/src/Mbstring/Mbstring.php index 3d45c9d9a..c5c143c7b 100644 --- a/src/Mbstring/Mbstring.php +++ b/src/Mbstring/Mbstring.php @@ -834,19 +834,32 @@ public static function mb_ord($s, $encoding = null) return $code; } - public static function mb_str_pad(string $string, int $length, string $pad_string = ' ', int $pad_type = \STR_PAD_RIGHT, ?string $encoding = null): string + /** @return string|false */ + public static function mb_str_pad(string $string, int $length, string $pad_string = ' ', int $pad_type = \STR_PAD_RIGHT, ?string $encoding = null) { if (!\in_array($pad_type, [\STR_PAD_RIGHT, \STR_PAD_LEFT, \STR_PAD_BOTH], true)) { + if (\PHP_VERSION_ID < 80000) { + trigger_error('mb_str_pad(): Argument #4 ($pad_type) must be STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH', \E_USER_WARNING); + + return false; + } + throw new \ValueError('mb_str_pad(): Argument #4 ($pad_type) must be STR_PAD_LEFT, STR_PAD_RIGHT, or STR_PAD_BOTH'); } if (null === $encoding) { $encoding = self::mb_internal_encoding(); - } else { - self::assertEncoding($encoding, 'mb_str_pad(): Argument #5 ($encoding) must be a valid encoding, "%s" given'); + } elseif (!self::assertEncoding($encoding, 'mb_str_pad(): Argument #5 ($encoding) must be a valid encoding, "%s" given')) { + return false; } if (self::mb_strlen($pad_string, $encoding) <= 0) { + if (\PHP_VERSION_ID < 80000) { + trigger_error('mb_str_pad(): Argument #3 ($pad_string) must be a non-empty string', \E_USER_WARNING); + + return false; + } + throw new \ValueError('mb_str_pad(): Argument #3 ($pad_string) must be a non-empty string'); } @@ -869,12 +882,13 @@ public static function mb_str_pad(string $string, int $length, string $pad_strin } } - public static function mb_ucfirst(string $string, ?string $encoding = null): string + /** @return string|false */ + public static function mb_ucfirst(string $string, ?string $encoding = null) { if (null === $encoding) { $encoding = self::mb_internal_encoding(); - } else { - self::assertEncoding($encoding, 'mb_ucfirst(): Argument #2 ($encoding) must be a valid encoding, "%s" given'); + } elseif (!self::assertEncoding($encoding, 'mb_ucfirst(): Argument #2 ($encoding) must be a valid encoding, "%s" given')) { + return false; } $firstChar = mb_substr($string, 0, 1, $encoding); @@ -883,12 +897,13 @@ public static function mb_ucfirst(string $string, ?string $encoding = null): str return $firstChar.mb_substr($string, 1, null, $encoding); } - public static function mb_lcfirst(string $string, ?string $encoding = null): string + /** @return string|false */ + public static function mb_lcfirst(string $string, ?string $encoding = null) { if (null === $encoding) { $encoding = self::mb_internal_encoding(); - } else { - self::assertEncoding($encoding, 'mb_lcfirst(): Argument #2 ($encoding) must be a valid encoding, "%s" given'); + } elseif (!self::assertEncoding($encoding, 'mb_lcfirst(): Argument #2 ($encoding) must be a valid encoding, "%s" given')) { + return false; } $firstChar = mb_substr($string, 0, 1, $encoding); @@ -971,27 +986,31 @@ private static function getEncoding($encoding) return $encoding; } - public static function mb_trim(string $string, ?string $characters = null, ?string $encoding = null): string + /** @return string|false */ + public static function mb_trim(string $string, ?string $characters = null, ?string $encoding = null) { return self::mb_internal_trim('{^[%s]+|[%1$s]+$}Du', $string, $characters, $encoding, __FUNCTION__); } - public static function mb_ltrim(string $string, ?string $characters = null, ?string $encoding = null): string + /** @return string|false */ + public static function mb_ltrim(string $string, ?string $characters = null, ?string $encoding = null) { return self::mb_internal_trim('{^[%s]+}Du', $string, $characters, $encoding, __FUNCTION__); } - public static function mb_rtrim(string $string, ?string $characters = null, ?string $encoding = null): string + /** @return string|false */ + public static function mb_rtrim(string $string, ?string $characters = null, ?string $encoding = null) { return self::mb_internal_trim('{[%s]+$}D', $string, $characters, $encoding, __FUNCTION__); } - private static function mb_internal_trim(string $regex, string $string, ?string $characters, ?string $encoding, string $function): string + /** @return string|false */ + private static function mb_internal_trim(string $regex, string $string, ?string $characters, ?string $encoding, string $function) { if (null === $encoding) { $encoding = self::mb_internal_encoding(); - } else { - self::assertEncoding($encoding, $function.'(): Argument #3 ($encoding) must be a valid encoding, "%s" given'); + } elseif (!self::assertEncoding($encoding, $function.'(): Argument #3 ($encoding) must be a valid encoding, "%s" given')) { + return false; } if ('' === $characters) { @@ -1029,7 +1048,7 @@ private static function mb_internal_trim(string $regex, string $string, ?string return iconv('UTF-8', $encoding.'//IGNORE', $string); } - private static function assertEncoding(string $encoding, string $errorFormat): void + private static function assertEncoding(string $encoding, string $errorFormat): bool { try { $validEncoding = @self::mb_check_encoding('', $encoding); @@ -1037,9 +1056,14 @@ private static function assertEncoding(string $encoding, string $errorFormat): v throw new \ValueError(sprintf($errorFormat, $encoding)); } - // BC for PHP 7.3 and lower if (!$validEncoding) { - throw new \ValueError(sprintf($errorFormat, $encoding)); + if (\PHP_VERSION_ID >= 80000) { + throw new \ValueError(sprintf($errorFormat, $encoding)); + } + + trigger_error(sprintf($errorFormat, $encoding), E_USER_WARNING); } + + return $validEncoding; } } diff --git a/src/Mbstring/bootstrap.php b/src/Mbstring/bootstrap.php index ff51ae079..08146134c 100644 --- a/src/Mbstring/bootstrap.php +++ b/src/Mbstring/bootstrap.php @@ -133,27 +133,27 @@ function mb_str_split($string, $length = 1, $encoding = null) { return p\Mbstrin } if (!function_exists('mb_str_pad')) { - function mb_str_pad(string $string, int $length, string $pad_string = ' ', int $pad_type = STR_PAD_RIGHT, ?string $encoding = null): string { return p\Mbstring::mb_str_pad($string, $length, $pad_string, $pad_type, $encoding); } + function mb_str_pad(string $string, int $length, string $pad_string = ' ', int $pad_type = STR_PAD_RIGHT, ?string $encoding = null) { return p\Mbstring::mb_str_pad($string, $length, $pad_string, $pad_type, $encoding); } } if (!function_exists('mb_ucfirst')) { - function mb_ucfirst(string $string, ?string $encoding = null): string { return p\Mbstring::mb_ucfirst($string, $encoding); } + function mb_ucfirst(string $string, ?string $encoding = null) { return p\Mbstring::mb_ucfirst($string, $encoding); } } if (!function_exists('mb_lcfirst')) { - function mb_lcfirst(string $string, ?string $encoding = null): string { return p\Mbstring::mb_lcfirst($string, $encoding); } + function mb_lcfirst(string $string, ?string $encoding = null) { return p\Mbstring::mb_lcfirst($string, $encoding); } } if (!function_exists('mb_trim')) { - function mb_trim(string $string, ?string $characters = null, ?string $encoding = null): string { return p\Mbstring::mb_trim($string, $characters, $encoding); } + function mb_trim(string $string, ?string $characters = null, ?string $encoding = null) { return p\Mbstring::mb_trim($string, $characters, $encoding); } } if (!function_exists('mb_ltrim')) { - function mb_ltrim(string $string, ?string $characters = null, ?string $encoding = null): string { return p\Mbstring::mb_ltrim($string, $characters, $encoding); } + function mb_ltrim(string $string, ?string $characters = null, ?string $encoding = null) { return p\Mbstring::mb_ltrim($string, $characters, $encoding); } } if (!function_exists('mb_rtrim')) { - function mb_rtrim(string $string, ?string $characters = null, ?string $encoding = null): string { return p\Mbstring::mb_rtrim($string, $characters, $encoding); } + function mb_rtrim(string $string, ?string $characters = null, ?string $encoding = null) { return p\Mbstring::mb_rtrim($string, $characters, $encoding); } }